2 #include "NPLMessage.h" 6 #include "NPLMsgIn_parser.h" 7 #include "NPLMessageQueue.h" 8 #include "WebSocket/WebSocketReader.h" 9 #include "WebSocket/WebSocketWriter.h" 11 #include <boost/asio.hpp> 12 #include <boost/array.hpp> 13 #include <boost/noncopyable.hpp> 14 #include <boost/shared_ptr.hpp> 15 #include <boost/enable_shared_from_this.hpp> 31 public boost::enable_shared_from_this<CNPLConnection>,
32 private boost::noncopyable
43 typedef std::map<std::string, int> StringMap_Type;
52 boost::asio::ip::tcp::socket&
socket();
64 void stop(
bool bRemoveConnection =
true,
int nReason = 0);
95 NPLReturnCode
SendMessage(
const char* sCommandName,
const char* sCommandData);
123 const string&
GetNID()
const;
137 bool SetNID(
const char* sNID);
166 int GetCompressionLevel();
173 int GetCompressionThreshold();
187 bool IsKeepAliveEnabled();
196 bool IsIdleTimeoutEnabled();
200 int GetIdleTimeoutPeriod();
230 void SetProtocol(ProtocolType protocolType = ProtocolType::NPL);
263 bool handle_websocket_data(
int bytes_transferred);
265 bool handle_tcp_custom_data(
int bytes_transferred);
271 void handle_read(
const boost::system::error_code& e,std::size_t bytes_transferred);
274 void handle_write(
const boost::system::error_code& e);
280 void handle_resolve(
const boost::system::error_code& err, boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
283 void handle_connect(
const boost::system::error_code&
error, boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
286 boost::asio::ip::tcp::socket m_socket;
289 typedef boost::array<char, 8192> Buffer_Type;
300 NPLRuntimeAddress_ptr m_address;
303 Buffer_Type m_buffer;
312 RingBuffer_Type m_queueOutput;
321 StringMap_Type m_filename_id_map;
324 uint32 m_totalBytesIn;
326 uint32 m_totalBytesOut;
329 bool m_bDebugConnection;
337 int32 m_nCompressionLevel;
342 int32 m_nCompressionThreshold;
348 bool m_bEnableIdleTimeout;
353 uint32 m_nFinishedCount;
356 bool m_bCloseAfterSend;
359 uint32 m_nIdleTimeoutMS;
362 uint32 m_nLastActiveTime;
372 std::vector<byte> m_websocket_input_data;
373 std::vector<byte> m_websocket_out_data;
375 ProtocolType m_protocolType;
382 bool operator()(
const NPLConnection_ptr& _Left,
const NPLConnection_ptr& _Right)
const 384 return (_Left.get()<_Right.get());
void SetProtocol(ProtocolType protocolType=ProtocolType::NPL)
set transmission protocol, default value is 0.
Definition: NPLConnection.cpp:952
void SetUseCompression(bool bUseCompression)
set whether to use compression for this connection.
Definition: NPLConnection.cpp:132
void TickReceive()
update the last receive or connect time.
Definition: NPLConnection.cpp:180
const string & GetNID() const
Get the NPL runtime address ID if any.
Definition: NPLConnection.cpp:108
define this to enable debugging of NPL code in visual studio
Definition: INPL.h:9
void connect()
connect to the remote NPL runtime address.
Definition: NPLConnection.cpp:561
bool HasUnsentData()
whether there is unsent data.
Definition: NPLConnection.cpp:215
this class serves as an interface between the low level socket interface and NPL message queues...
Definition: NPLDispatcher.h:23
Parser for incoming requests.
Definition: NPLMsgIn_parser.h:118
NPLConnectionState
This describes the possible states of a NPLConnection object.
Definition: NPLTypes.h:80
int GetLogLevel()
get global log level.
Definition: NPLConnection.cpp:220
string GetPort()
Get the TCP IP port if any.
Definition: NPLConnection.cpp:124
void EnableIdleTimeout(bool bEnable)
Enable idle timeout.
Definition: NPLConnection.cpp:82
virtual void handleDisconnect(int reason)
This connection is called when e.g.
Definition: NPLConnection.cpp:728
void TickSend()
update the last send time.
Definition: NPLConnection.cpp:175
void SetKeepAlive(bool bEnable)
enable application level keep alive.
Definition: NPLConnection.cpp:72
A incoming or outgoing connection.
Definition: NPLConnection.h:30
a globally unique name of a NPL file name instance.
Definition: NPLCommon.h:26
void SetTCPKeepAlive(bool bEnable)
set the TCP protocol level keep alive.
Definition: NPLConnection.cpp:64
void SetIdleTimeoutPeriod(int nMilliseconds)
how many milliseconds of inactivity to assume this connection should be timed out.
Definition: NPLConnection.cpp:92
boost::asio::ip::tcp::socket & socket()
Get the socket associated with the connection.
Definition: NPLConnection.cpp:59
A NPL msg received from a socket.
Definition: NPLMsgIn.h:8
CNPLConnectionManager & GetConnectionManager()
return the connection manager.
Definition: NPLConnection.h:74
void SetCompressionLevel(int nLevel)
default to 0, which means no compression.
Definition: NPLConnection.cpp:150
void SetNPLRuntimeAddress(NPLRuntimeAddress_ptr runtime_address)
set the NPL runtime address that this connection connects to.
Definition: NPLConnection.cpp:103
virtual bool handleMessageIn()
handle m_input_msg.
Definition: NPLConnection.cpp:869
void SetCompressionThreshold(int nThreshold)
when the message size is bigger than this number of bytes, we will use m_nCompressionLevel for compre...
Definition: NPLConnection.cpp:160
void stop(bool bRemoveConnection=true, int nReason=0)
Stop all asynchronous operations associated with the connection.
Definition: NPLConnection.cpp:325
unsigned int GetLastActiveTime()
get the last time in milliseconds GetTickCount(), that a send/receive/connect message is transmitted ...
Definition: NPLConnection.cpp:170
CNPLConnection(boost::asio::io_service &io_service, CNPLConnectionManager &manager, CNPLDispatcher &msg_dispatcher)
Construct a connection with the given io_service.
Definition: NPLConnection.cpp:39
bool IsConnected() const
whether the connection is established or not.
Definition: NPLConnection.cpp:937
Definition: inftrees.h:24
a compare class connection ptr
Definition: NPLConnection.h:380
Definition: WebSocketReader.h:9
an NPL message in the message queue.
Definition: NPLMessage.h:32
virtual void handleConnect()
This function is called when e.g.
Definition: NPLConnection.cpp:723
void start()
Start the first asynchronous operation for the connection.
Definition: NPLConnection.cpp:225
bool IsUseCompression()
get whether to use compression for this connection.
Definition: NPLConnection.cpp:145
bool SetNID(const char *sNID)
set nid of this connection.
Definition: NPLConnection.cpp:942
string GetIP()
Get the TCP IP address if any.
Definition: NPLConnection.cpp:116
int CheckIdleTimeout(unsigned int nCurTime)
if any of the connection should be timed out.
Definition: NPLConnection.cpp:185
void CloseAfterSend()
close/stop the connection when all data has been sent
Definition: NPLConnection.cpp:310
cross platform mutex
Definition: mutex.h:95
virtual void GetStatistics(int &totalIn, int &totalOut)
Get statistics about this connection.
Definition: NPLConnection.cpp:580
virtual bool handleReceivedData(int bytes_transferred)
This function is called whenever some data is received from the underlying transport.
Definition: NPLConnection.cpp:804
NPLConnectionState GetState() const
Returns the current connection state.
Definition: NPLConnection.h:118
bool IsAuthenticated() const
whether this connection is authenticated.
Definition: NPLConnection.cpp:932
All incoming and outgoing connection sessions.
Definition: NPLConnectionManager.h:13
void SetAuthenticated(bool bAuthenticated)
set whether this connection is authenticated.
Definition: NPLConnection.cpp:913
Definition: WebSocketWriter.h:9
NPLReturnCode SendMessage(const NPLFileName &file_name, const char *code=NULL, int nLength=0, int priority=0)
Send a message via this connection.
Definition: NPLConnection.cpp:587