My Project
NPLNetServer.h
1 #pragma once
2 #include <boost/thread.hpp>
3 #include <boost/asio.hpp>
4 #include <boost/noncopyable.hpp>
5 #include <boost/scoped_ptr.hpp>
6 #include <boost/asio/steady_timer.hpp>
7 #include "NPLConnectionManager.h"
8 #include "NPLDispatcher.h"
9 
10 namespace NPL
11 {
16  class CNPLNetServer : private boost::noncopyable
17  {
18  public:
19  CNPLNetServer();
20  ~CNPLNetServer();
21 
27  void start(const char* server=NULL, const char* port=NULL);
28 
30  void stop();
31 
33  void Cleanup();
34 
38  CNPLDispatcher& GetDispatcher(){return m_msg_dispatcher;};
39 
42  CNPLConnectionManager& GetConnectionManager(){return m_connection_manager;};
43 
50  NPLConnection_ptr CreateConnection(NPLRuntimeAddress_ptr pAddress);
51 
66  void SetTCPKeepAlive(bool bEnable);
67 
71  bool IsTCPKeepAliveEnabled();
72 
77  void SetKeepAlive(bool bEnable);
78  bool IsKeepAliveEnabled();
79 
86  void EnableIdleTimeout(bool bEnable);
87  bool IsIdleTimeoutEnabled();
88 
90  void SetIdleTimeoutPeriod(int nMilliseconds);
91  int GetIdleTimeoutPeriod();
92 
96  void EnableAnsiMode(bool bEnable);
97  bool IsAnsiMode();
98 
100  int GetMaxPendingConnections() const;
101  void SetMaxPendingConnections(int val);
102  public:
104  std::string GetExternalIP();
105 
107  virtual const std::string& GetHostPort();
109  virtual const std::string& GetHostIP();
111  virtual bool IsServerStarted();
112  private:
114  void handle_resolve_local(const boost::system::error_code& err, boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
115 
117  void handle_accept(const boost::system::error_code& e);
118 
120  void handle_stop();
121 
123  void handle_idle_timeout(const boost::system::error_code& err);
124 
126  boost::asio::io_service m_io_service_dispatcher;
127 
129  boost::scoped_ptr<boost::thread> m_dispatcherThread;
130 
132  boost::asio::ip::tcp::resolver m_resolver;
133 
135  boost::asio::ip::tcp::acceptor m_acceptor;
136 
138  CNPLConnectionManager m_connection_manager;
139 
141  NPLConnection_ptr m_new_connection;
142 
144  string m_strServer;
146  string m_strPort;
147 
149  bool m_bTCPKeepAlive;
150 
152  bool m_bKeepAlive;
153 
155  bool m_bEnableIdleTimeout;
156 
158  bool m_bIsServerStarted;
159 
161  int m_nIdleTimeoutMS;
162 
164  int m_nMaxPendingConnections;
165 
167  typedef boost::asio::basic_waitable_timer<boost::chrono::steady_clock> timer_type;
168  timer_type m_idle_timer;
169 
170 
173  boost::scoped_ptr<boost::asio::io_service::work> m_work_lifetime;
174 
178  CNPLDispatcher m_msg_dispatcher;
179  };
180 }
int GetMaxPendingConnections() const
queue size of the acceptor&#39;s queue.
Definition: NPLNetServer.cpp:393
void EnableAnsiMode(bool bEnable)
whether the first line of the NPL protocol is in ansi code page.
Definition: NPLNetServer.cpp:383
void SetKeepAlive(bool bEnable)
enable application level keep alive.
Definition: NPLNetServer.cpp:67
virtual const std::string & GetHostIP()
get the host IP of this NPL runtime
Definition: NPLNetServer.cpp:354
define this to enable debugging of NPL code in visual studio
Definition: INPL.h:9
void start(const char *server=NULL, const char *port=NULL)
start the server&#39;s io_service loop.
Definition: NPLNetServer.cpp:121
void SetTCPKeepAlive(bool bEnable)
System level Enable/disable SO_KEEPALIVE.
Definition: NPLNetServer.cpp:50
this class serves as an interface between the low level socket interface and NPL message queues...
Definition: NPLDispatcher.h:23
virtual bool IsServerStarted()
whether the NPL runtime&#39;s http server is started.
Definition: NPLNetServer.cpp:359
CNPLConnectionManager & GetConnectionManager()
get the connection object in this server
Definition: NPLNetServer.h:42
bool IsTCPKeepAliveEnabled()
whether SO_KEEPALIVE is enabled.
Definition: NPLNetServer.cpp:61
void SetIdleTimeoutPeriod(int nMilliseconds)
how many milliseconds of inactivity to assume this connection should be timed out.
Definition: NPLNetServer.cpp:92
Current NPL configuration settings.
Definition: NPLNetServer.h:16
CNPLDispatcher & GetDispatcher()
NPLDispatcher serves as an interface between the low level socket interface and NPL message queues...
Definition: NPLNetServer.h:38
virtual const std::string & GetHostPort()
get the host port of this NPL runtime
Definition: NPLNetServer.cpp:349
NPLConnection_ptr CreateConnection(NPLRuntimeAddress_ptr pAddress)
Create a new connection with a remote server and immediately connect and start the connection...
Definition: NPLNetServer.cpp:369
void Cleanup()
clean up all resources
Definition: NPLNetServer.cpp:364
std::string GetExternalIP()
get extern IP address of this computer.
Definition: NPLNetServer.cpp:325
void EnableIdleTimeout(bool bEnable)
Enable idle timeout.
Definition: NPLNetServer.cpp:82
void stop()
Stop the server.
Definition: NPLNetServer.cpp:185
All incoming and outgoing connection sessions.
Definition: NPLConnectionManager.h:13