Upstream tarball 9882
[amule.git] / src / EMSocket.h
blob4be544eef753a24766c50e9fcdc60bfd035aa5c9
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2008 Merkur ( devs@emule-project.net / http://www.emule-project.net )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #ifndef EMSOCKET_H
27 #define EMSOCKET_H
29 #include "EncryptedStreamSocket.h" // Needed for CEncryptedStreamSocket
31 #include "ThrottledSocket.h" // Needed for ThrottledFileSocket
33 class CPacket;
35 #define ERR_WRONGHEADER 0x01
36 #define ERR_TOOBIG 0x02
38 #define ES_DISCONNECTED 0xFF
39 #define ES_NOTCONNECTED 0x00
40 #define ES_CONNECTED 0x01
43 const sint32 PACKET_HEADER_SIZE = 6;
46 class CEMSocket : public CEncryptedStreamSocket, public ThrottledFileSocket
48 public:
49 CEMSocket(const CProxyData *ProxyData = NULL);
50 virtual ~CEMSocket();
52 virtual void SendPacket(CPacket* packet, bool delpacket = true, bool controlpacket = true, uint32 actualPayloadSize = 0);
53 bool HasQueues();
54 bool IsConnected() { return byConnected==ES_CONNECTED;};
55 uint8 GetConState() {return byConnected;}
56 void SetDownloadLimit(uint32 limit);
57 void DisableDownloadLimit();
59 virtual uint32 GetTimeOut() const;
60 virtual void SetTimeOut(uint32 uTimeOut);
62 uint32 GetLastCalledSend() { return lastCalledSend; }
64 uint64 GetSentBytesCompleteFileSinceLastCallAndReset();
65 uint64 GetSentBytesPartFileSinceLastCallAndReset();
66 uint64 GetSentBytesControlPacketSinceLastCallAndReset();
67 uint64 GetSentPayloadSinceLastCallAndReset();
68 void TruncateQueues();
70 virtual SocketSentBytes SendControlData(uint32 maxNumberOfBytesToSend, uint32 minFragSize) { return Send(maxNumberOfBytesToSend, minFragSize, true); };
71 virtual SocketSentBytes SendFileAndControlData(uint32 maxNumberOfBytesToSend, uint32 minFragSize) { return Send(maxNumberOfBytesToSend, minFragSize, false); };
73 uint32 GetNeededBytes();
75 void Destroy();
76 bool OnDestroy() { return DoingDestroy; };
78 //protected:
79 // these functions are public on our code because of the amuleDlg::socketHandler
80 virtual void OnError(int WXUNUSED(nErrorCode)) { };
81 virtual void OnSend(int nErrorCode);
82 virtual void OnReceive(int nErrorCode);
84 protected:
86 virtual bool PacketReceived(CPacket* WXUNUSED(packet)) { return false; };
87 virtual void OnClose(int nErrorCode);
89 uint8 byConnected;
90 uint32 m_uTimeOut;
92 private:
93 virtual SocketSentBytes Send(uint32 maxNumberOfBytesToSend, uint32 minFragSize, bool onlyAllowedToSendControlPacket);
94 void ClearQueues();
96 uint32 GetNextFragSize(uint32 current, uint32 minFragSize);
97 bool HasSent() { return m_hasSent; }
99 // Download (pseudo) rate control
100 uint32 downloadLimit;
101 bool downloadLimitEnable;
102 bool pendingOnReceive;
104 // Download partial header
105 // actually, this holds only 'PACKET_HEADER_SIZE-1' bytes.
106 byte pendingHeader[PACKET_HEADER_SIZE];
107 uint32 pendingHeaderSize;
109 // Download partial packet
110 CPacket* pendingPacket;
111 uint32 pendingPacketSize;
113 // Upload control
114 byte* sendbuffer;
115 uint32 sendblen;
116 uint32 sent;
118 typedef std::list<CPacket*> CPacketQueue;
119 CPacketQueue m_control_queue;
121 struct StandardPacketQueueEntry
123 uint32 actualPayloadSize;
124 CPacket* packet;
127 typedef std::list<StandardPacketQueueEntry> CStdPacketQueue;
128 CStdPacketQueue m_standard_queue;
130 bool m_currentPacket_is_controlpacket;
132 wxMutex m_sendLocker;
134 uint64 m_numberOfSentBytesCompleteFile;
135 uint64 m_numberOfSentBytesPartFile;
136 uint64 m_numberOfSentBytesControlPacket;
137 bool m_currentPackageIsFromPartFile;
139 bool m_bAccelerateUpload;
140 uint32 lastCalledSend;
141 uint32 lastSent;
142 uint32 lastFinishedStandard;
144 uint32 m_actualPayloadSize;
145 uint32 m_actualPayloadSizeSent;
147 bool m_bBusy;
148 bool m_hasSent;
150 bool DoingDestroy;
154 #endif // EMSOCKET_H
155 // File_checked_for_headers