source: abuse/branches/win32/Abuse32/scratch.txt @ 636

Last change on this file since 636 was 100, checked in by Sam Hocevar, 15 years ago
  • Importing an old Win32 port by Jeremy "Marauder" Scott.
  • Property svn:keywords set to Id
File size: 14.0 KB
Line 
1/* ---------------------------------------------------------------------------
2 -------------------------------------------------------------------------
3 DP_socket method definitions
4 -------------------------------------------------------------------------
5 ------------------------------------------------------------------------- /**/
6void DP_socket::SendAckPacket(int PacketID)
7{
8    if (!Remote_ID || !BlockingReads)
9        return;
10    DPPACKET PacketAck;
11    PacketAck.DataSize = 0;
12    PacketAck.MessageNum = -1;
13    PacketAck.Port = SocketPort;
14    PacketAck.MessageType = PACKET_ACK;
15    PacketAck.Reserved = PacketID;
16//    OutQueue.AddPacketToQueue(&PacketAck);
17    HRESULT hRet;
18    hRet = g_pDP->Send(Local_ID, Remote_ID, 0, &PacketAck, PacketSize(&PacketAck));
19    _RPT3(_CRT_WARN, "DP_socket::SendAckPacket -- Sent acknowledgement of message #%d from player 0x%x to player 0x%x\n", PacketID, Local_ID, Remote_ID);
20}
21
22/* ---------------------------------------------------------------------------
23 ------------------------------------------------------------------------- /**/
24void DP_socket::AddConnection(DPPACKET* Packet, DWORD Size)
25{
26    DP_socket *OldSock = DP->FindSocket(Packet->Reserved);
27    if (OldSock) {
28        OldSock->SendAckPacket(Packet->MessageNum);
29        return;
30    }
31   
32    HRESULT hRet;
33    DP_address NewAddr;
34    NewAddr.Port = -1;
35    NewAddr.Remote_ID = Packet->Reserved;
36    DPNAME Name;
37    memset(&Name, 0, sizeof(Name));
38    Name.dwSize = sizeof(Name);
39    hRet = g_pDP->CreatePlayer(&NewAddr.Local_ID, &Name, NULL, NULL, 0, 0);
40    if (hRet != DP_OK)
41        return;
42    DP_socket *NewSock;
43    NewSock = new DP_socket(-1, &NewAddr);
44    NewSock->NextConnect = ConnectRequests;
45    ConnectRequests = NewSock;
46    NewSock->Remote_Fast_Socket = *((int*) &Packet->Data);
47    NewSock->SendAckPacket(Packet->MessageNum);
48    NewSock->SendConnectAck();
49    NewSock->InQueue.Serial = 1;
50}
51
52/* ---------------------------------------------------------------------------
53 ------------------------------------------------------------------------- /**/
54void DP_socket::SendConnectAck()
55{
56    DPPACKET ConnectAck;
57    ConnectAck.DataSize = 0;
58    ConnectAck.MessageNum = -1;
59    ConnectAck.Port = SocketPort;
60    ConnectAck.MessageType = PACKET_CONNECT_ACK;
61    ConnectAck.Reserved = Local_ID;
62    OutQueue.AddPacketToQueue(&ConnectAck);
63    Send();
64/*    HRESULT hRet;
65    hRet = g_pDP->Send(Local_ID, Remote_ID, 0, &ConnectAck, PacketSize(&ConnectAck));
66    _RPT2(_CRT_WARN, "DP_socket::write -- Sent connection acknowledgement from player 0x%x to player 0x%x\n", Local_ID, Remote_ID); */
67}
68
69/* ---------------------------------------------------------------------------
70 ------------------------------------------------------------------------- /**/
71void DP_socket::SendConnectRequest()
72{
73    DPPACKET ConnectRequest;
74    ConnectRequest.DataSize = 4;
75    ConnectRequest.Port = SocketPort;
76    ConnectRequest.MessageType = PACKET_CONNECT;
77    ConnectRequest.Reserved = Local_ID;
78    *((unsigned long*) ConnectRequest.Data) = FastID;
79    OutQueue.AddPacketToQueue(&ConnectRequest);
80}
81
82/* ---------------------------------------------------------------------------
83 ------------------------------------------------------------------------- /**/
84void DP_socket::Send() {
85    if (!OutQueue.DataSize())
86        return;
87    HRESULT hRet;
88    hRet = g_pDP->Send(Local_ID, Remote_ID, 0, OutQueue.Data, OutQueue.DataSize());
89    _RPT3(_CRT_WARN, "DP_socket::Send -- Sent %d bytes from player 0x%x to player 0x%x\n", OutQueue.DataSize(), Local_ID, Remote_ID);
90}
91
92/* ---------------------------------------------------------------------------
93 ------------------------------------------------------------------------- /**/
94int DP_socket::write(void* buf, int size, net_address *addr)
95{
96    DPPACKET Packet;
97    Packet.DataSize = size;
98    Packet.MessageType = PACKET_DATA;
99    Packet.Port = SocketPort;
100    Packet.Reserved = FastID;
101    memcpy(Packet.Data, buf, size);
102    if (!BlockingReads) {
103/*        OutQueue.Acknowledge(((DPPACKET*) OutQueue.Data)->MessageNum);
104        OutQueue.AddPacketToQueue(&Packet); */
105        HRESULT hRet;
106        if (!addr) {
107            hRet = g_pDP->Send(Local_ID, Remote_ID, 0, &Packet, PacketSize(&Packet));
108            _RPT3(_CRT_WARN, "DP_socket::write (fast) -- Sent %d bytes from player 0x%x to player 0x%x\n", OutQueue.DataSize(), Local_ID, Remote_ID);
109        }
110        else {
111            hRet = g_pDP->Send(Local_ID, ((DP_address*) addr)->Remote_ID, 0, &Packet, PacketSize(&Packet));
112            _RPT3(_CRT_WARN, "DP_socket::write (fast addr) -- Sent %d bytes from player 0x%x to player 0x%x\n", OutQueue.DataSize(), Local_ID, ((DP_address*) addr)->Remote_ID);
113        }
114    }
115    else {
116        OutQueue.AddPacketToQueue(&Packet);
117        Send();
118    }
119    return size;
120}
121
122/* ---------------------------------------------------------------------------
123 ------------------------------------------------------------------------- /**/
124int DP_socket::read(void *buf, int size, net_address **addr)
125{
126    DWORD BytesRead = 0;
127    time_marker Start, Now;
128    do {
129        while (!ready_to_read() && BlockingReads && Now.diff_time(&Start) < DPREAD_TIMEOUT) {
130            DP->select(0);
131        }
132        BytesRead += read_backup(buf, size);
133     } while (size && Now.diff_time(&Start) < DPREAD_TIMEOUT && BlockingReads);
134    _RPT2(_CRT_WARN, "DP_socket::read -- Read %d bytes from player 0x%x\n", BytesRead, Remote_ID);
135    if (addr) {
136        DP_address *NewAddr = new DP_address();
137        NewAddr->Local_ID = Local_ID;
138        NewAddr->Remote_ID = Last_Received_ID;
139        NewAddr->Port = -1;
140        *addr = NewAddr;
141    }
142    return BytesRead;
143}
144
145/* ---------------------------------------------------------------------------
146 ------------------------------------------------------------------------- /**/
147DP_socket::~DP_socket()
148{
149    _RPT2(_CRT_WARN, "Deleting socket from player 0x%x to player 0x%x\n", Local_ID, Remote_ID);
150    if (backup)
151        jfree(backup);
152    DP->remove_socket_from_list(this);
153    if (Local_ID == FastID)
154        FastID = 0;
155    if (Local_ID != ServerPlayer)
156        g_pDP->DestroyPlayer(Local_ID);
157}
158
159/* ---------------------------------------------------------------------------
160 ------------------------------------------------------------------------- /**/
161DP_socket::DP_socket(int port, DP_address *dest)
162
163    Connecting = 0;
164    ConnectRequests = NULL;
165    backup = NULL;
166    backup_size = backup_end = backup_start = 0;
167    BlockingReads = 1;
168
169    status = INIT;
170    DP->add_socket_to_list(this);
171    // setup an outgoing packet structure
172    if (dest) {
173        if (port != -1)
174            SocketPort = port;
175        else
176            SocketPort = dest->Port;
177        Remote_ID = dest->Remote_ID;
178        Local_ID = dest->Local_ID;
179    }
180    else {
181        SocketPort = port;
182        Remote_ID = DPID_ALLPLAYERS;
183        Local_ID = 0;
184    }
185    _ASSERT(SocketPort != 0xcdcdcdcd);
186        _RPT1(_CRT_WARN, "Creating new socket on port %d\n", SocketPort);
187}
188
189/* ---------------------------------------------------------------------------
190 ------------------------------------------------------------------------- /**/
191void DP_socket::send_ping_packet()
192{
193    //send_data(0);   // send a 0 length data packet to remote host
194}
195
196/* ---------------------------------------------------------------------------
197 ------------------------------------------------------------------------- /**/
198void DP_socket::clear()
199{
200}
201
202/* ---------------------------------------------------------------------------
203 ------------------------------------------------------------------------- /**/
204int DP_socket::ready_to_write() {
205    return 1;
206}
207
208/* ---------------------------------------------------------------------------
209 ------------------------------------------------------------------------- /**/
210int DP_socket::ProcessPacket()
211{
212    DPPACKET Packet;
213    InQueue.Read(&Packet);
214    switch (Packet.MessageType) {
215        case PACKET_ACK:
216            OutQueue.Acknowledge(Packet.Reserved);
217            _RPT1(_CRT_WARN, "Got acknowledgement for packet #%d\n", Packet.Reserved);
218            return 0;
219        case PACKET_CONNECT:
220            // Pitch it, we're getting a dupe connect from this player
221            if (BlockingReads)
222                SendAckPacket(Packet.MessageNum);
223            _RPT1(_CRT_WARN, "Got duplicate connect request from 0x%x\n", Packet.Reserved);
224            Send();
225            break;
226        case PACKET_CONNECT_ACK:
227            if (Remote_ID == ServerPlayer) {
228                Remote_ID = Packet.Reserved;
229                Connecting = 0;
230                _RPT1(_CRT_WARN, "Got connect acknowledgement from 0x%x\n", Packet.Reserved);
231//                SendAckPacket(Packet.MessageNum);
232            }
233            SendAckPacket(Packet.MessageNum);
234            break;
235        case PACKET_DATA:
236            if (BlockingReads)
237                SendAckPacket(Packet.MessageNum);
238            SendAckPacket(Packet.MessageNum);
239            add_backup((unsigned char*) &Packet.Data, Packet.DataSize);
240            _RPT3(_CRT_WARN, "Read %d bytes from packet 0x%x:%d\n", Packet.DataSize, Local_ID, Packet.MessageNum);
241            return Packet.DataSize;
242            break;
243        default:
244            break;
245    }
246    return 0;
247}
248
249/* ---------------------------------------------------------------------------
250 ------------------------------------------------------------------------- /**/
251int DP_socket::ready_to_read()     
252{
253    static time_marker Last;
254    time_marker Now;
255    if (backup_end - backup_start > 0) {
256        return backup_end - backup_start;
257    }
258/*    if (ConnectRequests)
259        return 1; */
260
261    if (BlockingReads)
262        if (OutQueue.DataSize() && Now.diff_time(&Last) > DPRESEND_TIMEOUT) {
263            Send();
264            Last.get_time();
265        }
266
267    DPID Source_ID, *From;
268    DWORD flags;
269    if (Remote_ID == DPID_ALLPLAYERS || Connecting) {
270        flags = DPRECEIVE_TOPLAYER;
271        From = &Source_ID;
272    }
273    else {
274        flags = DPRECEIVE_FROMPLAYER | DPRECEIVE_TOPLAYER;
275        From = &Remote_ID;
276    }
277    HRESULT hRet = DP_OK;
278//    while (hRet == DP_OK) {
279        DWORD DataSize = OVERSIZE_PACKET_SIZE + PACKET_HEADER_SIZE;
280        DPPACKET *Pack = (DPPACKET*) &BigPacket;
281        hRet = g_pDP->Receive(From, &Local_ID, flags, Pack, &DataSize);
282        if (hRet == DPERR_BUFFERTOOSMALL) {
283            Pack = (DPPACKET*) malloc(DataSize);
284            hRet = g_pDP->Receive(From, &Local_ID, flags, Pack, &DataSize);
285        }
286        if (hRet == DP_OK) {
287            Last_Received_ID = *From;
288            if (*From == Local_ID || *From == 0 || DataSize < PACKET_HEADER_SIZE) {
289                _RPT0(_CRT_WARN, "Useless network packet, tossed out...\n");
290//                return 0;
291            }
292            switch (Pack->MessageType) {
293                case PACKET_CONNECT:
294                    DP->HandlePacket(Pack, DataSize);
295                    break;
296                case PACKET_DATA:
297                case PACKET_ACK:
298                case PACKET_CONNECT_ACK:
299                    InQueue.AddPackets(Pack, DataSize);
300                    do {
301                        ProcessPacket();
302                    } while (InQueue.Count()); // && (backup_end - backup_start <= 0));
303                    break;
304            }
305        }
306        if (Pack != (DPPACKET*) &BigPacket)
307            delete Pack;
308//    }
309    if (backup_end - backup_start > 0) {
310        return backup_end - backup_start;
311    }
312    if (ConnectRequests)
313        return 1;
314    return 0;
315}
316
317/* ---------------------------------------------------------------------------
318 ------------------------------------------------------------------------- /**/
319int DP_socket::read_backup(void *&buf, int &size)    // read previously buffered data
320{
321    int s = backup_end - backup_start;
322    if (s && size) {
323        if (size >= s) {
324            memcpy(buf, backup + backup_start, s);
325            buf = (void*) ((uchar*) buf + s);
326            backup_start = backup_end = 0;
327            size -= s;
328            return s;
329        }
330        else {
331            memcpy(buf, backup + backup_start, size);
332            buf = (void*) ((uchar*) + size);
333            int ret = size;
334            backup_start += size;
335            size = 0;
336            return ret;
337        }
338    } else return 0;
339}
340
341/* ---------------------------------------------------------------------------
342 ------------------------------------------------------------------------- /**/
343void DP_socket::add_backup(uchar *buf, int size)
344{
345    if (size) {
346        if (backup_size - backup_end >= size) {
347            memcpy(backup + backup_end, buf, size);
348            backup_end += size;
349        }
350        else {
351            backup_size += backup_end + size - backup_size;
352            backup = (uchar*) jrealloc(backup, backup_size, "backup buffer");
353            add_backup(buf, size);
354        }
355    }
356}
357
358/* ---------------------------------------------------------------------------
359 ------------------------------------------------------------------------- /**/
360DP_address *DP_socket::GetAddress()
361{
362    DP_address *NewAddr = new DP_address;
363    NewAddr->Local_ID = Local_ID;
364    NewAddr->Remote_ID = Remote_Fast_Socket;
365    NewAddr->Port = SocketPort;
366    NewAddr->SessionGUID = GUID_NULL;
367    return NewAddr;
368}
369
370/* ---------------------------------------------------------------------------
371 ------------------------------------------------------------------------- /**/
372net_socket *DP_socket::accept(net_address *&from)
373{
374    if (ConnectRequests) {
375        DP_socket *last = NULL, *ret = ConnectRequests;
376        while (ret->NextConnect) {
377            last = ret;
378            ret = ret->NextConnect;
379        }
380        from = ret->GetAddress();
381        if (last)
382            last->NextConnect = NULL;
383        else
384            ConnectRequests = NULL;
385        return ret;
386    }
387    else
388        return NULL;
389}
390
Note: See TracBrowser for help on using the repository browser.