编程语言
首页 > 编程语言> > c – Poco :: Net服务器和客户端TCP连接事件处理程序

c – Poco :: Net服务器和客户端TCP连接事件处理程序

作者:互联网

我正在开始一个新项目,同时刚刚发现了Poco图书馆,我发现它非常棒.但是我有点迷失,因为例子并不多.

我有一个ServerApplication-> TCPServer-> ServerSocket TCPServerConnectionFactory-> TCPServerconnection方法,如示例所示.我按照指示从PocoNet类继承.现在我可以将我的服务器作为服务运行,&接收传入的连接.

我想采用以下事件处理方法:在每个连接(或每个客户端)的基础上,处理事件,例如可在客户端套接字上读取的数据,客户端套接字上发生的错误(断开连接或超时),发送客户端套接字上没有错误的数据

我该怎么做呢?
Poco / Foundation / Events是我正在寻找的,还是在Poco :: Net中实现了一些机制?

我已经看过Poco :: Net :: NetExpections但是当netcat连接关闭时,它们似乎不会被我的TCPServerConnection派生类抛出.

解决方法:

我最终使用的是一种不同的方法,因为TCPServer是一个完全不同的野兽.在发布here的示例之后,我最终得到了一个继承自ServerApplication的类,以及一个基本上由SocketReactor成为连接处理程序的类.

Deamonizer标题:

class Daemon : public ServerApplication
{
  public:
    Daemon();
    /// @Brief The main loop of the daemon, everything must take place here
    int main();
};

Deamonizer实施:

int Daemon::main()
{
  // Server Socket
  ServerSocket svs(2222);
  // Reactor-Notifier
  SocketReactor reactor;
  Poco::Timespan timeout(2000000); // 2Sec
  reactor.setTimeout(timeout);
  // Server-Acceptor
  SocketAcceptor<ConnectionHandler> acceptor(svs, reactor);
  // Threaded Reactor
  Thread thread;
  thread.start(reactor);
  // Wait for CTRL+C
  waitForTerminationRequest();
  // Stop Reactor
  reactor.stop();
  thread.join();
  return Application::EXIT_OK;  
}

处理程序类可以是任何东西,只要它具有符合的构造函数(请参阅Poco :: Net文档).
在我的情况下,标题看起来像这样:

class ConnectionHandler
{
  public:

    /**
     * @Brief Constructor of the Connection Handler
     * @Note Each object is unique to an accepted connection
     * @Param SteamSocket is the socket accepting the connections
     * @See SocketAcceptor http://pocoproject.org/docs/Poco.Net.SocketAcceptor.html
     * @Param SocketReactor is the reacting engine (threaded) which creates notifications about the socket
     */
    ConnectionHandler(StreamSocket &, SocketReactor &);

    /**
     * @Brief Destructor
     */
    ~ConnectionHandler();

    /**
    * @Brief Event Handler when Socket becomes Readable, i.e: there is data waiting to be read
    */
    void onSocketReadable(const AutoPtr<ReadableNotification>& pNf);

    /**
    * @Brief Event Handler when Socket was written, i.e: confirmation of data sent away (not received by client)
    */
    void onSocketWritable(const AutoPtr<WritableNotification>& pNf);

    /**
    * @Brief Event Handler when Socket was shutdown on the remote/peer side
    */
    void onSocketShutdown(const AutoPtr<ShutdownNotification>& pNf);

    /**
    * @Brief Event Handler when Socket throws an error
    */
    void onSocketError(const AutoPtr<ErrorNotification>& pNf);

    /**
    * @Brief Event Handler when Socket times-out
    */
    void onSocketTimeout(const AutoPtr<TimeoutNotification>& pNf);

  private:

    /**
     * @Brief Read bytes from the socket, depending on available bytes on socket
     */
    void readBytes();

    /**
     * @Brief Send message to the socket
     * @Param std::string is the message (null terminated)
     */
    void sendMessage(std::string);

    /// Stream Socket
    StreamSocket _socket;

    /// Socket Reactor-Notifier
    SocketReactor& _reactor;

    /// Received Data Buffer
    std::vector<char *> in_buffer;
};

如何实现处理程序取决于您,只要您需要注意处理事件的类方法,如下所示:

  _reactor.addEventHandler(_socket,NObserver<ConnectionHandler, ReadableNotification>(*this, &ConnectionHandler::onSocketReadable));
  _reactor.addEventHandler(_socket,NObserver<ConnectionHandler, ShutdownNotification>(*this, &ConnectionHandler::onSocketShutdown));
  _reactor.addEventHandler(_socket,NObserver<ConnectionHandler, ErrorNotification>(*this, &ConnectionHandler::onSocketError));
  _reactor.addEventHandler(_socket,NObserver<ConnectionHandler, TimeoutNotification>(*this, &ConnectionHandler::onSocketTimeout));

总而言之,两个类,几行代码,简单而干净.绝对开始喜欢Poco图书馆!

标签:c,networking,network-programming,poco-libraries
来源: https://codeday.me/bug/20191004/1852098.html