/* */ #ifndef _D_PEER_CONNECTION_H_ #define _D_PEER_CONNECTION_H_ #include "Option.h" #include "Socket.h" #include "Logger.h" #include "TorrentMan.h" #include "PeerMessage.h" #include "HandshakeMessage.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: int cuid; const Socket* socket; const Option* option; const Logger* logger; char resbuf[MAX_PAYLOAD_LEN]; int resbufLength; int currentPayloadLength; char lenbuf[4]; int lenbufLength; public: PeerConnection(int cuid, const Socket* socket, const Option* op); ~PeerConnection(); // Returns the number of bytes written int sendMessage(const char* msg, int length); bool receiveMessage(char* msg, int& length); /** * 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(char* msg, int& length); }; #endif // _D_PEER_CONNECTION_H_