forked from irwir/eMule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMSocket.h
190 lines (172 loc) · 4.62 KB
/
MMSocket.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//this file is part of eMule
//Copyright (C)2003 Merkur ( [email protected] / http://www.emule-project.net )
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#pragma once
#include "SafeFile.h"
class CMMServer;
//****** Outgoing Packets Class
class CMMPacket
{
public:
CMMPacket(uint8 byOpcode) {m_pBuffer = new CMemFile; m_pBuffer->Write(&byOpcode,1); m_bSpecialHeader = false;}
~CMMPacket() {delete m_pBuffer;}
void WriteByte(uint8 write) {m_pBuffer->Write(&write,1);}
void WriteShort(uint16 write) {m_pBuffer->Write(&write,2);}
void WriteInt(uint32 write) {m_pBuffer->Write(&write,4);}
void WriteInt64(uint64 write) {m_pBuffer->Write(&write,8);}
void WriteString(CStringA write){
uint8 len = (write.GetLength() > 255) ? (uint8)255 : (uint8)write.GetLength();
WriteByte(len);
m_pBuffer->Write(const_cast<LPSTR>((LPCSTR)write), len);
}
void WriteString(CString write){
CStringA strA(write);
WriteString(strA);
}
CMemFile* m_pBuffer;
bool m_bSpecialHeader;
};
//****** Incoming Packets Class
class CMMData: public CSafeMemFile
{
public:
CMMData(char* pData,uint32 nSize):CSafeMemFile((BYTE*)pData,nSize) {}
uint8 ReadByte(){
uint8 buf;
Read(&buf,1);
return buf;
}
uint16 ReadShort(){
uint16 buf;
Read(&buf,2);
return buf;
}
uint32 ReadInt(){
uint32 buf;
Read(&buf,4);
return buf;
}
uint64 ReadInt64(){
uint64 buf;
Read(&buf,8);
return buf;
}
CString ReadString(){
uint8 buf;
char str[256];
buf = ReadByte();
Read(str,buf);
return CString(str,buf);
}
};
//****** Socket
class CMMSocket: public CAsyncSocket
{
public:
CMMSocket(CMMServer* pOwner);
~CMMSocket(void);
bool SendPacket(CMMPacket* packet, bool bQueueFirst = false);
bool m_bClosed;
uint32 m_dwTimedShutdown;
protected:
void OnReceive(int nErrorCode);
void OnClose(int nErrorCode);
void Close();
void OnRequestReceived(char* pHeader, DWORD dwHeaderLen, char* pData, DWORD dwDataLen);
void OnSend(int nErrorCode);
void CheckForClosing();
private:
char* m_pBuf;
DWORD m_dwRecv;
DWORD m_dwBufSize;
DWORD m_dwHttpHeaderLen;
DWORD m_dwHttpContentLen;
CMMServer* m_pOwner;
char* m_pSendBuffer;
uint32 m_nSendLen;
uint32 m_nSent;
CTypedPtrList<CPtrList, CMMPacket*> m_PacketQueue;
};
//****** Listening Socket
class CListenMMSocket: public CAsyncSocket
{
public:
CListenMMSocket(CMMServer* pOwner);
~CListenMMSocket(void);
bool Create();
void Process();
virtual void OnAccept(int nErrorCode);
protected:
void DeleteClosedSockets();
private:
CMMServer* m_pOwner;
CTypedPtrList<CPtrList, CMMSocket*> m_socket_list;
};
//opcodes
#define MMP_HELLO 0x01
#define MMP_HELLOANS 0x02
#define MMP_INVALIDID 0x03
#define MMP_GENERALERROR 0x04
#define MMP_STATUSREQ 0x05
#define MMP_STATUSANSWER 0x06
#define MMP_FILELISTREQ 0x07
#define MMP_FILELISTANS 0x08
#define MMP_FILECOMMANDREQ 0x09
#define MMP_FILECOMMANDANS 0x10
#define MMP_FILEDETAILREQ 0x11
#define MMP_FILEDETAILANS 0x12
#define MMP_COMMANDREQ 0x13
#define MMP_COMMANDANS 0x14
#define MMP_SEARCHREQ 0x15
#define MMP_SEARCHANS 0x16
#define MMP_DOWNLOADREQ 0x17
#define MMP_DOWNLOADANS 0x18
#define MMP_PREVIEWREQ 0x19
#define MMP_PREVIEWANS 0x20
#define MMP_FINISHEDREQ 0x21
#define MMP_FINISHEDANS 0x22
#define MMP_CHANGELIMIT 0x23
#define MMP_CHANGELIMITANS 0x24
#define MMP_STATISTICSREQ 0x25
#define MMP_STATISTICSANS 0x26
// tags
#define MMT_OK 0x01
#define MMT_WRONGVERSION 0x02
#define MMT_WRONGPASSWORD 0x03
#define MMT_FAILED 0x00
#define MMT_PAUSED 0x00
#define MMT_WAITING 0x01
#define MMT_DOWNLOADING 0x02
#define MMT_PAUSE 0x03
#define MMT_RESUME 0x02
#define MMT_CANCEL 0x01
#define MMT_SDEMULE 0x01
#define MMT_SDPC 0x02
#define MMT_SERVERCONNECT 0x03
#define MMT_SEARCH 0x30
#define MMT_PREVIEW 0x40
// OK = 0x01
#define MMT_NOTCONNECTED 0x02
#define MMT_TIMEDOUT 0x03
#define MMT_NORESULTS 0x04
// failed = 0x00
// OK = 0x01
#define MMT_NOTAVAILABLE 0x02
#define MMT_NOTSUPPORTED 0x03
#define MMT_PARTFILFE 0x01
#define MMT_FINISHEDFILE 0x02
#define MM_VERSION 0x9B
#define MM_STRVERSION "0.9x"