poco_echo.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // @see poco/Net/samples/EchoServer
  2. #include "Poco/Net/SocketReactor.h"
  3. #include "Poco/Net/SocketAcceptor.h"
  4. #include "Poco/Net/SocketNotification.h"
  5. #include "Poco/Net/StreamSocket.h"
  6. #include "Poco/Net/ServerSocket.h"
  7. #include "Poco/NObserver.h"
  8. #include "Poco/Exception.h"
  9. #include "Poco/Thread.h"
  10. #include "Poco/FIFOBuffer.h"
  11. #include "Poco/Delegate.h"
  12. #include "Poco/Util/ServerApplication.h"
  13. #include "Poco/Util/Option.h"
  14. #include "Poco/Util/OptionSet.h"
  15. #include "Poco/Util/HelpFormatter.h"
  16. #include <iostream>
  17. using Poco::Net::SocketReactor;
  18. using Poco::Net::SocketAcceptor;
  19. using Poco::Net::ReadableNotification;
  20. using Poco::Net::WritableNotification;
  21. using Poco::Net::ShutdownNotification;
  22. using Poco::Net::ServerSocket;
  23. using Poco::Net::StreamSocket;
  24. using Poco::NObserver;
  25. using Poco::AutoPtr;
  26. using Poco::Thread;
  27. using Poco::FIFOBuffer;
  28. using Poco::delegate;
  29. using Poco::Util::ServerApplication;
  30. using Poco::Util::Application;
  31. using Poco::Util::Option;
  32. using Poco::Util::OptionSet;
  33. using Poco::Util::HelpFormatter;
  34. class EchoServiceHandler
  35. /// I/O handler class. This class (un)registers handlers for I/O based on
  36. /// data availability. To ensure non-blocking behavior and alleviate spurious
  37. /// socket writability callback triggering when no data to be sent is available,
  38. /// FIFO buffers are used. I/O FIFOBuffer sends notifications on transitions
  39. /// from [1] non-readable (i.e. empty) to readable, [2] writable to non-writable
  40. /// (i.e. full) and [3] non-writable (i.e. full) to writable.
  41. /// Based on these notifications, the handler member functions react by
  42. /// enabling/disabling respective reactor framework notifications.
  43. {
  44. public:
  45. EchoServiceHandler(StreamSocket& socket, SocketReactor& reactor):
  46. _socket(socket),
  47. _reactor(reactor),
  48. _fifoIn(BUFFER_SIZE, true),
  49. _fifoOut(BUFFER_SIZE, true)
  50. {
  51. _reactor.addEventHandler(_socket, NObserver<EchoServiceHandler, ReadableNotification>(*this, &EchoServiceHandler::onSocketReadable));
  52. _reactor.addEventHandler(_socket, NObserver<EchoServiceHandler, ShutdownNotification>(*this, &EchoServiceHandler::onSocketShutdown));
  53. _fifoOut.readable += delegate(this, &EchoServiceHandler::onFIFOOutReadable);
  54. _fifoIn.writable += delegate(this, &EchoServiceHandler::onFIFOInWritable);
  55. }
  56. ~EchoServiceHandler()
  57. {
  58. _reactor.removeEventHandler(_socket, NObserver<EchoServiceHandler, ReadableNotification>(*this, &EchoServiceHandler::onSocketReadable));
  59. _reactor.removeEventHandler(_socket, NObserver<EchoServiceHandler, WritableNotification>(*this, &EchoServiceHandler::onSocketWritable));
  60. _reactor.removeEventHandler(_socket, NObserver<EchoServiceHandler, ShutdownNotification>(*this, &EchoServiceHandler::onSocketShutdown));
  61. _fifoOut.readable -= delegate(this, &EchoServiceHandler::onFIFOOutReadable);
  62. _fifoIn.writable -= delegate(this, &EchoServiceHandler::onFIFOInWritable);
  63. }
  64. void onFIFOOutReadable(bool& b)
  65. {
  66. if (b)
  67. _reactor.addEventHandler(_socket, NObserver<EchoServiceHandler, WritableNotification>(*this, &EchoServiceHandler::onSocketWritable));
  68. else
  69. _reactor.removeEventHandler(_socket, NObserver<EchoServiceHandler, WritableNotification>(*this, &EchoServiceHandler::onSocketWritable));
  70. }
  71. void onFIFOInWritable(bool& b)
  72. {
  73. if (b)
  74. _reactor.addEventHandler(_socket, NObserver<EchoServiceHandler, ReadableNotification>(*this, &EchoServiceHandler::onSocketReadable));
  75. else
  76. _reactor.removeEventHandler(_socket, NObserver<EchoServiceHandler, ReadableNotification>(*this, &EchoServiceHandler::onSocketReadable));
  77. }
  78. void onSocketReadable(const AutoPtr<ReadableNotification>& pNf)
  79. {
  80. try
  81. {
  82. int len = _socket.receiveBytes(_fifoIn);
  83. if (len > 0)
  84. {
  85. _fifoIn.drain(_fifoOut.write(_fifoIn.buffer(), _fifoIn.used()));
  86. }
  87. else
  88. {
  89. delete this;
  90. }
  91. }
  92. catch (Poco::Exception& exc)
  93. {
  94. Application& app = Application::instance();
  95. app.logger().log(exc);
  96. delete this;
  97. }
  98. }
  99. void onSocketWritable(const AutoPtr<WritableNotification>& pNf)
  100. {
  101. try
  102. {
  103. _socket.sendBytes(_fifoOut);
  104. }
  105. catch (Poco::Exception& exc)
  106. {
  107. Application& app = Application::instance();
  108. app.logger().log(exc);
  109. delete this;
  110. }
  111. }
  112. void onSocketShutdown(const AutoPtr<ShutdownNotification>& pNf)
  113. {
  114. delete this;
  115. }
  116. private:
  117. enum
  118. {
  119. BUFFER_SIZE = 1024
  120. };
  121. StreamSocket _socket;
  122. SocketReactor& _reactor;
  123. FIFOBuffer _fifoIn;
  124. FIFOBuffer _fifoOut;
  125. };
  126. class EchoServer: public Poco::Util::ServerApplication
  127. {
  128. public:
  129. EchoServer()
  130. {
  131. }
  132. ~EchoServer()
  133. {
  134. }
  135. protected:
  136. void initialize(Application& self)
  137. {
  138. ServerApplication::initialize(self);
  139. }
  140. void uninitialize()
  141. {
  142. ServerApplication::uninitialize();
  143. }
  144. int main(const std::vector<std::string>& args)
  145. {
  146. if (args.size() < 1) {
  147. printf("Usage: cmd port\n");
  148. return -10;
  149. }
  150. int port = atoi(args[0].c_str());
  151. // set-up a server socket
  152. ServerSocket svs(port);
  153. // set-up a SocketReactor...
  154. SocketReactor reactor;
  155. // ... and a SocketAcceptor
  156. SocketAcceptor<EchoServiceHandler> acceptor(svs, reactor);
  157. // run the reactor in its own thread so that we can wait for
  158. // a termination request
  159. Thread thread;
  160. thread.start(reactor);
  161. // wait for CTRL-C or kill
  162. waitForTerminationRequest();
  163. // Stop the SocketReactor
  164. reactor.stop();
  165. thread.join();
  166. return Application::EXIT_OK;
  167. }
  168. };
  169. int main(int argc, char** argv)
  170. {
  171. EchoServer app;
  172. return app.run(argc, argv);
  173. }