12

私は新しいプロジェクトを開始していますが、同時に Poco Library を発見しました。これは本当に素晴らしいと思います。ただし、例が十分ではないため、少し迷っています。

例に示されているように、ServerApplication->TCPServer->ServerSocket + TCPServerConnectionFactory->TCPServerconnection アプローチがあります。指示通りPocoNetのクラスを継承しています。現在、サーバーをサービスとして実行し、着信接続を受信できます。

次のイベント処理アプローチを取りたい: 接続ごと (またはクライアントごと) に、クライアント ソケットで読み取り可能なデータ、クライアント ソケットで発生したエラー (切断またはタイムアウト)、送信などのイベントを処理するクライアントソケットでエラーのないデータ。

どうすればそれを行うことができますか?私が探しているのは Poco/Foundation/Events ですか、それとも Poco::Net に何らかのメカニズムが実装されていますか?

Poco::Net::NetExpections を見たことがありますが、netcat 接続が閉じるときに TCPServerConnection 派生クラスでスローされないようです。

4

2 に答える 2

12

TCPServerはまったく別の獣であるため、私が最終的に使用したのは別のアプローチです。ここに投稿された例に従って、ServerApplicationから継承するクラスと、本質的にSocketReactorによる接続ハンドラーになるクラスになりました。

Deamonizer ヘッダー:

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

デモナイザーの実装:

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));

全体として、2 つのクラス、数行のコード、シンプルでクリーンです。Poco ライブラリが大好きになりました!:)

于 2013-02-04T20:53:18.400 に答える
10

これを試してください:

#include <iostream>
#include "Poco/Net/TCPServer.h"
#include "Poco/Net/TCPServerParams.h"
#include "Poco/Net/TCPServerConnectionFactory.h"
#include "Poco/Net/TCPServerConnection.h"
#include "Poco/Net/Socket.h"
using namespace std;

class newConnection: public Poco::Net::TCPServerConnection {
public:
    newConnection(const Poco::Net::StreamSocket& s) :
        Poco::Net::TCPServerConnection(s) {
    }

    void run() {
        cout << "New connection from: " << socket().peerAddress().host().toString() <<  endl << flush;
        bool isOpen = true;
        Poco::Timespan timeOut(10,0);
        unsigned char incommingBuffer[1000];
        while(isOpen){
            if (socket().poll(timeOut,Poco::Net::Socket::SELECT_READ) == false){
                cout << "TIMEOUT!" << endl << flush;
            }
            else{
                cout << "RX EVENT!!! ---> "   << flush;
                int nBytes = -1;

                try {
                    nBytes = socket().receiveBytes(incommingBuffer, sizeof(incommingBuffer));
                }
                catch (Poco::Exception& exc) {
                    //Handle your network errors.
                    cerr << "Network error: " << exc.displayText() << endl;
                    isOpen = false;
                }


                if (nBytes==0){
                    cout << "Client closes connection!" << endl << flush;
                    isOpen = false;
                }
                else{
                    cout << "Receiving nBytes: " << nBytes << endl << flush;
                }
            }
        }
        cout << "Connection finished!" << endl << flush;
    }
};

int main(int argc, char** argv) {

    //Create a server socket to listen.
    Poco::Net::ServerSocket svs(1234);

    //Configure some server params.
    Poco::Net::TCPServerParams* pParams = new Poco::Net::TCPServerParams();
    pParams->setMaxThreads(4);
    pParams->setMaxQueued(4);
    pParams->setThreadIdleTime(100);

    //Create your server
    Poco::Net::TCPServer myServer(new Poco::Net::TCPServerConnectionFactoryImpl<newConnection>(), svs, pParams);
    myServer.start();

    while(1);

    return 0;
}
于 2013-02-01T12:47:38.453 に答える