/******************************************************************************/
struct Connection // reliable TCP based client/server connection with automatic data encryption and boundaries management, data always reaches the target, data is always received in the same order as it was sent, received data size will always be the same as when it was sent
{
File data; // received data from the external connection, use it for reading incoming data
// manage
void del ( );
Bool serverAcceptClient (Socket &server );
Bool clientConnectToServer(SockAddr &server_addr);
// get
CONNECT_STATE state () {return _state ;} // get connection state
C SockAddr& address () {return _address ;} // get destination address
Int expectedDataSize() {return _expected_data_size;} // expected size of streamed data, -1=unknown
Int received() {return _in_offset ;} // get total amount of received data
Int sent () {return _out_offset ;} // get total amount of sent data
Int queued () {return _out.size() ;} // get queued amount of bytes currently awaiting to be sent
UInt life (); // how long the connection is alive (in milliseconds)
Bool receive(Int timeout); // wait up to 'timeout' milliseconds to receive data, false if no data is available
// send commands
Bool bye ( ); // send goodbye message, requesting connection termination
Bool dataFull (CPtr buf, Int size, Bool send=true); // 'send'=if automatically send command instead of adding to the queue
Bool dataFull (File &f , Int size, Bool send=true); // 'send'=if automatically send command instead of adding to the queue
Bool dataStreamStart (Int total_size=-1, Bool send=true); // -1=unknown, 'send'=if automatically send command instead of adding to the queue
Bool dataStreamContinue(CPtr buf, Int size, Bool send=true); // 'send'=if automatically send command instead of adding to the queue
Bool dataStreamContinue(File &f , Int size, Bool send=true); // 'send'=if automatically send command instead of adding to the queue
Bool dataStreamFinished( Bool send=true); // 'send'=if automatically send command instead of adding to the queue
Bool dataStreamAbort ( Bool send=true); // 'send'=if automatically send command instead of adding to the queue
Bool send(); // send all queued commands
// operations
Bool tcpNoDelay(Bool on) {return _socket.tcpNoDelay(on);} // set TCP_NODELAY option, false on fail
Connection() {_expected_data_size=-1; _state=CONNECT_INVALID; _packet_size_progress=_in_offset=_out_offset=_expected_packet_size=_birth=0;}
~Connection() {del();}
private:
CONNECT_STATE _state;
Int _packet_size_progress, _in_offset, _out_offset;
UInt _expected_data_size, _expected_packet_size, _birth;
File _in, _out;
Socket _socket;
SockAddr _address;
Secure _secure;
};
/******************************************************************************/