当人们运行TCP服务器时,我看到许多SocketReactor与SocketAcceptor一起使用的例子。
但是,我想要连接到现有的TCP服务器作为客户端,但希望能够处理SocketReactor公开的事件,例如onSocketReadable,onSocketWritable,onSocketShutdown,onSocketError和onSocketTimeout。 / p>
POCO库可以实现吗?我把以下代码放在一起但没有触发任何事件。 如果这种方法不起作用,还有其他建议吗? 基本上我将从服务器接收实时的tcp消息流,我还会将消息发送回服务器以执行某些任务。
class ITCHProvider
{
private:
Poco::Net::SocketAddress _sa;
Poco::Net::StreamSocket _sock;
Poco::Net::SocketStream _stream;
Poco::Net::SocketReactor _reactor;
public:
ITCHProvider() :
_sa("host", 1234),
_sock(),
_stream(_sock),
_reactor()
{
_reactor.addEventHandler(_sock, Poco::NObserver<ITCHProvider, Poco::Net::ReadableNotification>(*this, &ITCHProvider::onSocketReadable));
_reactor.addEventHandler(_sock, Poco::NObserver<ITCHProvider, Poco::Net::WritableNotification>(*this, &ITCHProvider::onSocketWritable));
_reactor.addEventHandler(_sock, Poco::NObserver<ITCHProvider, Poco::Net::ShutdownNotification>(*this, &ITCHProvider::onSocketShutdown));
_reactor.addEventHandler(_sock, Poco::NObserver<ITCHProvider, Poco::Net::ErrorNotification>(*this, &ITCHProvider::onSocketError));
_reactor.addEventHandler(_sock, Poco::NObserver<ITCHProvider, Poco::Net::TimeoutNotification>(*this, &ITCHProvider::onSocketTimeout));
_sock.connect(_sa);
}
~ITCHProvider()
{
close();
}
void onSocketReadable(const Poco::AutoPtr<Poco::Net::ReadableNotification>& pNf)
{
LOG(INFO) << "READable !!";
}
void onSocketWritable(const Poco::AutoPtr<Poco::Net::WritableNotification>& pNf)
{
LOG(INFO) << "WRITEable !!";
}
void onSocketShutdown(const Poco::AutoPtr<Poco::Net::ShutdownNotification>& pNf)
{
LOG(INFO) << "SHUTDOWN!!!!!!!!!!!!";
}
void onSocketError(const Poco::AutoPtr<Poco::Net::ErrorNotification>& pNf)
{
LOG(INFO) << "Error!!";
}
void onSocketTimeout(const Poco::AutoPtr<Poco::Net::TimeoutNotification>& pNf)
{
LOG(INFO) << "Timeout!!";
}
// Close down the connection properly.
void close() {
try {
_sock.shutdown();
}
catch (...) {
LOG(INFO) << "closing failed.";
}
}
};
答案 0 :(得分:1)
可以做到,但请注意(a)在实际运行反应堆之前不会发生任何事情,并且(b)反应堆将旋转直到明确停止,因此最好在单独的线程中运行它;
这是一个简单的例子:
// server-side handler
class EchoServiceHandler {
public:
EchoServiceHandler(StreamSocket& socket, SocketReactor& reactor): _socket(socket), _reactor(reactor) {
_reactor.addEventHandler(_socket, Observer<EchoServiceHandler, ReadableNotification>(*this, &EchoServiceHandler::onReadable));
}
~EchoServiceHandler() {
_reactor.removeEventHandler(_socket, Observer<EchoServiceHandler, ReadableNotification>(*this, &EchoServiceHandler::onReadable));
}
void onReadable(ReadableNotification* pNf) {
pNf->release();
char buffer[8];
int n = _socket.receiveBytes(buffer, sizeof(buffer));
if (n > 0) {
_socket.sendBytes(buffer, n);
}
else {
_socket.shutdownSend();
delete this;
}
}
private:
StreamSocket _socket;
SocketReactor& _reactor;
};
原始代码,略有修改:
class ITCHProvider : public Poco::Runnable
{
private:
Poco::Net::StreamSocket _sock;
Poco::Net::SocketReactor _reactor;
public:
ITCHProvider(const SocketAddress& sa) : _sock(sa) {
_reactor.addEventHandler(_sock, Poco::NObserver<ITCHProvider, Poco::Net::ReadableNotification>(*this, &ITCHProvider::onSocketReadable));
_reactor.addEventHandler(_sock, Poco::NObserver<ITCHProvider, Poco::Net::WritableNotification>(*this, &ITCHProvider::onSocketWritable));
_reactor.addEventHandler(_sock, Poco::NObserver<ITCHProvider, Poco::Net::ShutdownNotification>(*this, &ITCHProvider::onSocketShutdown));
_reactor.addEventHandler(_sock, Poco::NObserver<ITCHProvider, Poco::Net::ErrorNotification>(*this, &ITCHProvider::onSocketError));
_reactor.addEventHandler(_sock, Poco::NObserver<ITCHProvider, Poco::Net::TimeoutNotification>(*this, &ITCHProvider::onSocketTimeout));
std::string data = "Hello reactor world!";
_sock.sendBytes(data.data(), (int)data.length());
}
~ITCHProvider() { close(); }
void run() { _reactor.run(); }
void stop() { _reactor.stop(); }
void onSocketReadable(const Poco::AutoPtr<Poco::Net::ReadableNotification>& pNf) {
std::cout << "READable !!" << std::endl;
char data[1025] = { 0 };
if (_sock.receiveBytes(data, 1024) > 0) {
std::cout << data << std::endl;
}
}
void onSocketWritable(const Poco::AutoPtr<Poco::Net::WritableNotification>& pNf) {
std::cout << "WRITEable !!" << std::endl;
}
void onSocketShutdown(const Poco::AutoPtr<Poco::Net::ShutdownNotification>& pNf) {
std::cout << "SHUTDOWN!!!!!!!!!!!!" << std::endl;
}
void onSocketError(const Poco::AutoPtr<Poco::Net::ErrorNotification>& pNf) {
std::cout << "Error!!" << std::endl;
}
void onSocketTimeout(const Poco::AutoPtr<Poco::Net::TimeoutNotification>& pNf) {
std::cout << "Timeout!!" << std::endl;
}
void close() {
try {
_sock.shutdown();
}
catch (...) {
std::cout << "closing failed." << std::endl;
}
}
};
现在,让我们运行以上内容:
SocketAddress ssa;
ServerSocket ss(ssa);
SocketReactor reactor;
SocketAcceptor<EchoServiceHandler> acceptor(ss, reactor);
Thread server;
server.start(reactor);
ITCHProvider provider(SocketAddress("127.0.0.1", ss.address().port()));
Thread client;
client.start(provider);
Thread::sleep(1000);
reactor.stop();
provider.stop();
server.join();
client.join();
输出(为清晰起见,可写和超时通知静音):
READable !!
Hello reactor world!
SHUTDOWN!!!!!!!!!!!!