/* */ #ifndef D_PEER_CONNECTION_H #define D_PEER_CONNECTION_H #include "common.h" #include #include "SharedHandle.h" #include "SocketBuffer.h" #include "Command.h" namespace aria2 { class Peer; class SocketCore; class ARC4Encryptor; class ARC4Decryptor; // The maximum length of payload. Messages beyond that length are // dropped. #define MAX_PAYLOAD_LEN (16*1024+128) class PeerConnection { private: cuid_t cuid_; SharedHandle peer_; SharedHandle socket_; unsigned char* resbuf_; size_t resbufLength_; size_t currentPayloadLength_; unsigned char lenbuf_[4]; size_t lenbufLength_; SocketBuffer socketBuffer_; bool encryptionEnabled_; SharedHandle encryptor_; SharedHandle decryptor_; bool prevPeek_; void readData(unsigned char* data, size_t& length, bool encryption); ssize_t sendData(const unsigned char* data, size_t length, bool encryption); public: PeerConnection (cuid_t cuid, const SharedHandle& peer, const SharedHandle& socket); ~PeerConnection(); // Pushes data into send buffer. After this call, this object gets // ownership of data, so caller must not delete or alter it. void pushBytes(unsigned char* data, size_t len); void pushStr(const std::string& data); bool receiveMessage(unsigned char* data, size_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, size_t& dataLength, bool peek = false); void enableEncryption(const SharedHandle& encryptor, const SharedHandle& decryptor); void presetBuffer(const unsigned char* data, size_t length); bool sendBufferIsEmpty() const; ssize_t sendPendingData(); const unsigned char* getBuffer() const { return resbuf_; } size_t getBufferLength() const { return resbufLength_; } unsigned char* detachBuffer(); }; typedef SharedHandle PeerConnectionHandle; } // namespace aria2 #endif // D_PEER_CONNECTION_H