1//
2// This file is part of the aMule Project.
3//
4// Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5// Copyright (c) 2002-2011 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.
10//
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.
15//
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
24//
25
26#ifndef EMSOCKET_H
27#define EMSOCKET_H
28
29#include "EncryptedStreamSocket.h"				// Needed for CEncryptedStreamSocket
30
31#include "ThrottledSocket.h"	// Needed for ThrottledFileSocket
32
33class CPacket;
34
35#define ERR_WRONGHEADER		0x01
36#define ERR_TOOBIG			0x02
37
38#define	ES_DISCONNECTED		0xFF
39#define	ES_NOTCONNECTED		0x00
40#define	ES_CONNECTED		0x01
41
42
43const uint32 PACKET_HEADER_SIZE	= 6;
44
45
46class CEMSocket : public CEncryptedStreamSocket, public ThrottledFileSocket
47{
48public:
49	CEMSocket(const CProxyData *ProxyData = NULL);
50	virtual ~CEMSocket();
51
52	virtual void 	SendPacket(CPacket* packet, bool delpacket = true, bool controlpacket = true, uint32 actualPayloadSize = 0);
53	bool	IsConnected() { return byConnected==ES_CONNECTED;};
54	uint8	GetConState()	{return byConnected;}
55	void	SetDownloadLimit(uint32 limit);
56	void	DisableDownloadLimit();
57
58	virtual uint32	GetTimeOut() const;
59	virtual void	SetTimeOut(uint32 uTimeOut);
60
61    uint32	GetLastCalledSend() { return lastCalledSend; }
62
63    uint64	GetSentBytesCompleteFileSinceLastCallAndReset();
64    uint64	GetSentBytesPartFileSinceLastCallAndReset();
65    uint64	GetSentBytesControlPacketSinceLastCallAndReset();
66    uint64	GetSentPayloadSinceLastCallAndReset();
67    void	TruncateQueues();
68
69    virtual SocketSentBytes SendControlData(uint32 maxNumberOfBytesToSend, uint32 minFragSize) { return Send(maxNumberOfBytesToSend, minFragSize, true); };
70    virtual SocketSentBytes SendFileAndControlData(uint32 maxNumberOfBytesToSend, uint32 minFragSize) { return Send(maxNumberOfBytesToSend, minFragSize, false); };
71
72    uint32	GetNeededBytes();
73
74	void	Destroy();
75	bool OnDestroy() { return DoingDestroy; };
76
77	//protected:
78	// these functions are public on our code because of the amuleDlg::socketHandler
79	virtual void	OnError(int WXUNUSED(nErrorCode)) { };
80	virtual void	OnSend(int nErrorCode);
81	virtual void	OnReceive(int nErrorCode);
82
83protected:
84
85	virtual bool	PacketReceived(CPacket* WXUNUSED(packet)) { return false; };
86	virtual void	OnClose(int nErrorCode);
87
88	uint8	byConnected;
89	uint32	m_uTimeOut;
90
91private:
92    virtual SocketSentBytes Send(uint32 maxNumberOfBytesToSend, uint32 minFragSize, bool onlyAllowedToSendControlPacket);
93	void	ClearQueues();
94
95    uint32	GetNextFragSize(uint32 current, uint32 minFragSize);
96    bool    HasSent() { return m_hasSent; }
97
98	// Download (pseudo) rate control
99	uint32	downloadLimit;
100	bool	downloadLimitEnable;
101	bool	pendingOnReceive;
102
103	// Download partial header
104	byte	pendingHeader[PACKET_HEADER_SIZE];
105	uint32	pendingHeaderSize;
106
107	// Download partial packet
108	byte*	pendingPacket;
109	uint32  pendingPacketSize;
110
111	// Upload control
112	byte*	sendbuffer;
113	uint32	sendblen;
114	uint32	sent;
115
116	typedef std::list<CPacket*> CPacketQueue;
117	CPacketQueue m_control_queue;
118
119	struct StandardPacketQueueEntry
120	{
121		uint32 actualPayloadSize;
122		CPacket* packet;
123	};
124
125	typedef	std::list<StandardPacketQueueEntry> CStdPacketQueue;
126	CStdPacketQueue m_standard_queue;
127
128    bool m_currentPacket_is_controlpacket;
129
130	wxMutex	m_sendLocker;
131
132	uint64 m_numberOfSentBytesCompleteFile;
133    uint64 m_numberOfSentBytesPartFile;
134    uint64 m_numberOfSentBytesControlPacket;
135    bool m_currentPackageIsFromPartFile;
136
137	bool	m_bAccelerateUpload;
138	uint32	lastCalledSend;
139    uint32	lastSent;
140	uint32	lastFinishedStandard;
141
142    uint32 m_actualPayloadSize;
143    uint32 m_actualPayloadSizeSent;
144
145    bool m_bBusy;
146    bool m_hasSent;
147
148	bool DoingDestroy;
149};
150
151
152#endif // EMSOCKET_H
153// File_checked_for_headers
154