Connection.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /******************************************************************************
  2. Use 'Connection' to handle safe client/server TCP based connections.
  3. Use 'ConnectionServer' to handle server managing multiple client connections.
  4. /******************************************************************************/
  5. enum CONNECT_STATE : Byte
  6. {
  7. CONNECT_INVALID , // not connected or connection lost
  8. CONNECT_CONNECTING , // connection is in progress
  9. CONNECT_AWAIT_GREET , // awaiting to receive greeting
  10. CONNECT_VERSION_CONFLICT, // server/client use different software version
  11. CONNECT_GREETED , // greeted
  12. };
  13. /******************************************************************************/
  14. struct Connection // reliable TCP based client/server connection with automatic data encryption and boundaries management, data always reaches the target, multiple data packets are always received in the same order as they were sent, received data size will always be the same as when it was sent
  15. {
  16. File data; // received data from the external connection, use it for reading incoming data
  17. // manage
  18. Connection& del ( );
  19. Connection& clientConnectToServer(C SockAddr &server_addr);
  20. Bool serverAcceptClient ( Socket &server );
  21. // get
  22. CONNECT_STATE state ()C {return _state;} // get connection state
  23. C SockAddr& address ()C {return _address;} // get destination address
  24. Long received()C {return _in_offset;} // get total amount of received data
  25. Long sent ()C {return _out_offset;} // get total amount of sent data
  26. Int queued ()C {return (Int)_out.left();} // get queued amount of bytes currently awaiting to be sent
  27. UInt life ()C; // how long the connection is alive (in milliseconds), 0 if not yet created
  28. Bool receive (Int timeout); // wait up to 'timeout' milliseconds to receive data, false if no data is available, this method automatically calls 'updateState'
  29. Bool updateState(Int timeout); // this method verifies current connection state, and performs necessary steps needed for connection to reach the CONNECT_GREETED state, 'timeout'=how long (in milliseconds) to wait for connection to reach CONNECT_GREETED state in this step, true is returned if connection has reached CONNECT_GREETED state, false is returned on fail (if connection failed or is still in progress)
  30. // io
  31. Bool send(CPtr buf, Int size , Bool flush=true); // 'flush'=if automatically call 'flush' method to send this message immediately, the method will always fail if connection state is not CONNECT_GREETED
  32. Bool send(File &f , Int size=-1, Bool flush=true); // 'size'=size to send (-1=all remaining in file), 'flush'=if automatically call 'flush' method to send this message immediately, the method will always fail if connection state is not CONNECT_GREETED
  33. Bool flush(Int timeout=-1); // flush all queued commands for sending within 'timeout' milliseconds (use <0 for unlimited amount of time), false on timeout (when timeout occurs then all remaining data will be sent next time)
  34. // operations
  35. Bool tcpNoDelay(Bool on) {return _socket.tcpNoDelay(on);} // set TCP_NODELAY option, false on fail
  36. Connection() {_state=CONNECT_INVALID; _msg_size_progress=0; _msg_size=_birth=0; _in_offset=_out_offset=0;}
  37. ~Connection() {del();}
  38. #if !EE_PRIVATE
  39. private:
  40. #endif
  41. CONNECT_STATE _state;
  42. Byte _msg_size_progress;
  43. UInt _msg_size, _birth;
  44. Long _in_offset, _out_offset;
  45. File _out;
  46. Socket _socket;
  47. SockAddr _address;
  48. Cipher1 _cipher;
  49. #if EE_PRIVATE
  50. Bool greet ();
  51. Bool flushEx (Int timeout); // wait 'timeout' until all data has been sent
  52. Bool updateEx(Int timeout, Bool read);
  53. #endif
  54. };
  55. /******************************************************************************/
  56. struct FastConnection // fast but unreliable UDP based connection, data is not guaranteed to reach the target, multiple data packets may not always be received in the same order as they were sent, received data size will always be the same as when it was sent
  57. {
  58. // manage
  59. void del ( ); // delete
  60. Bool create( Int port=-1); // create using automatic address at specified 'port' (-1=autodetect), false on fail
  61. Bool create(C SockAddr &addr ); // create using custom address , false on fail
  62. // get
  63. Int port ()C {return _socket.port();} // get port at which connection was created
  64. Long sent ()C {return _sent ;} // get total amount of sent data
  65. Long received()C {return _received ;} // get total amount of received data
  66. // io
  67. Bool send(C SockAddr &addr, CPtr data, Int size, Cipher *cipher=null); // send 'size' amount of 'data' to 'addr' address , if 'cipher' is given then it will be used to encrypt the data, this method supports sending up to 64KB data, false on fail
  68. Bool send(C SockAddr &addr, File &f , Cipher *cipher=null); // send 'f' file data, from its current position to its end, if 'cipher' is given then it will be used to encrypt the data, this method supports sending up to 64KB data, false on fail
  69. Int receive(SockAddr &addr, Byte (&data)[65536]); // receive data, 'data'=custom buffer which will receive data, if data was received successfully, the 'addr' will contain the address of the sender, and method will return size of received data, -1 if there was no data
  70. FastConnection() {_sent=_received=0;}
  71. private:
  72. Long _sent, _received;
  73. Socket _socket;
  74. };
  75. /******************************************************************************/
  76. struct ConnectionServer // connection server managing multiple client connections using 'Connection' class
  77. {
  78. struct Client
  79. {
  80. Connection connection;
  81. virtual void create(ConnectionServer &server) {} // called upon creation of the client with 'server' ConnectionServer responsible for the connection
  82. virtual Bool update( ); // override this method and manually process the client update: check if super::update() is true, check and process incoming connection data, return false if client wants to be removed and true if wants to live
  83. };
  84. Map<SockAddr, Client> clients; // map of clients
  85. // manage
  86. void del ( ); // delete manually
  87. Bool create( Int port=-1); // create server using automatic address, 'port'=port for creating the socket server (-1=autodetect), false on fail
  88. Bool create(C SockAddr &addr ); // create server using custom address, 'addr'=address on which the server should be created , false on fail
  89. // get
  90. Bool is ()C {return _server.is ();} // if server is created
  91. Int port ()C {return _server.port();} // get server port
  92. Str localAddressName()C; // get server local address using Computer Name format
  93. SockAddr localAddress ()C; // get server local address
  94. SockAddr globalAddress ()C; // get server global address
  95. // operations
  96. void update(); // update server, call this continuously to accept new clients and process existing ones
  97. ~ConnectionServer() {del();}
  98. ConnectionServer();
  99. private:
  100. Socket _server;
  101. };
  102. /******************************************************************************/