/* */ #ifndef _D_PEER_CONNECTION_H_ #define _D_PEER_CONNECTION_H_ #include "Option.h" #include "Socket.h" #include "Logger.h" #include "common.h" // we assume maximum length of incoming message is "piece" message with 16KB // data. Messages beyond that size are dropped. #define MAX_PAYLOAD_LEN (9+16*1024) class PeerConnection { private: int32_t cuid; SocketHandle socket; const Option* option; const Logger* logger; char resbuf[MAX_PAYLOAD_LEN]; int32_t resbufLength; int32_t currentPayloadLength; unsigned char lenbuf[4]; int32_t lenbufLength; public: PeerConnection(int32_t cuid, const SocketHandle& socket, const Option* op); ~PeerConnection(); // Returns the number of bytes written int32_t sendMessage(const unsigned char* data, int32_t dataLength); bool receiveMessage(unsigned char* data, int32_t& dataLength); /** * Returns true if a handshake message is fully received, otherwise returns * false. * In both cases, 'msg' is filled with received bytes and the filled length * is assigned to 'length'. */ bool receiveHandshake(unsigned char* data, int32_t& dataLength); }; typedef SharedHandle PeerConnectionHandle; typedef WeakHandle PeerConnectionWeakHandle; #endif // _D_PEER_CONNECTION_H_