forked from haithun/CoD4X17a_testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet_game.c
188 lines (148 loc) · 5.26 KB
/
net_game.c
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
/*
===========================================================================
Copyright (C) 2010-2013 Ninja and TheKelm of the IceOps-Team
This file is part of CoD4X17a-Server source code.
CoD4X17a-Server source code is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
CoD4X17a-Server source code 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
===========================================================================
*/
#include "q_shared.h"
#include "qcommon.h"
#include "qcommon_io.h"
#include "msg.h"
#include "sys_net.h"
#include "server.h"
#include "net_game.h"
#include "net_game_conf.h"
void NET_UDPPacketEvent(netadr_t* from, void* data, int len)
{
msg_t msg;
msg.data = data;
msg.cursize = len;
msg.maxsize = len;
msg.readcount = 0;
msg.bit = 0;
msg.readonly = qtrue;
msg.overflowed = qfalse;
SV_PacketEvent(from, &msg);
}
unsigned int NET_TimeGetTime()
{
return (unsigned int)com_frameTime;
}
/*
=================
NET_ReadTcpPackets
=================
*/
#define MAX_TCPEVENTS 4
typedef struct
{
int serviceId;
tcpclientstate_t (*tcpauthevent)(netadr_t* from, msg_t* msg, int socketfd, int *connectionId);
qboolean (*tcpevent)(netadr_t* from, msg_t* msg, int socketfd, int connectionId);
void (*tcpconncloseevent)(netadr_t* from, int socketfd, int connectionId);
}tcpevent_t;
tcpevent_t tcpevents[MAX_TCPEVENTS];
void NET_TCPConnectionClosed(netadr_t* adr, int socketfd, int connectionId, int serviceId)
{
int i;
for(i = 0; i < MAX_TCPEVENTS; i++)
{
if(tcpevents[i].serviceId == serviceId)
{
if(tcpevents[i].tcpconncloseevent != NULL)
tcpevents[i].tcpconncloseevent(adr, socketfd, connectionId);
return;
}
}
}
tcpclientstate_t NET_TCPAuthPacketEvent(netadr_t* from, byte* bufData, int len, int socketfd, int* connectionId, int *serviceId)
{
int i;
msg_t msg;
tcpclientstate_t ret;
msg.data = bufData;
msg.cursize = len;
msg.maxsize = len;
msg.readcount = 0;
msg.bit = 0;
msg.readonly = qtrue;
msg.overflowed = qfalse;
Com_DPrintf("Packet event from: %s\n", NET_AdrToString(from));
for(i = 0; i < MAX_TCPEVENTS; i++)
{
ret = tcpevents[i].tcpauthevent(from, &msg, socketfd, connectionId);
if(ret != TCP_AUTHNOTME)
{
*serviceId = tcpevents[i].serviceId;
return ret;
}
}
Com_DPrintf("^5Bad TCP-Packet from: %s\n", NET_AdrToString(from));
return TCP_AUTHBAD; //Close connection
}
void NET_TCPPacketEvent(netadr_t* from, byte* bufData, int len, int socketfd, int connectionId, int serviceId)
{
int i;
msg_t msg;
for(i = 0; i < MAX_TCPEVENTS; i++)
{
if(tcpevents[i].serviceId == serviceId)
{
msg.data = bufData;
msg.cursize = len;
msg.maxsize = len;
msg.readcount = 0;
msg.bit = 0;
msg.readonly = qtrue;
msg.overflowed = qfalse;
if(tcpevents[i].tcpevent(from, &msg, socketfd, connectionId))
{
NET_TcpCloseSocket(socketfd);
}
return;
}
}
Com_PrintError("NET_TCPPacketEvent: Bad serviceId: %d\n", serviceId);
NET_TcpCloseSocket(socketfd);
return; //Close connection
}
void NET_TCPAddEventType(
qboolean (*tcpevent)(netadr_t* from, msg_t* msg, int socketfd, int connectionId),
tcpclientstate_t (*tcpauthevent)(netadr_t* from, msg_t* msg, int socketfd, int *connectionId),
void (*tcpconncloseevent)(netadr_t* from, int socketfd, int connectionId),
int serviceId
){
int i;
if(tcpevent == NULL || tcpauthevent == NULL)
{
Com_Error(ERR_FATAL, "NET_TCPAddEventType: NULL tcpevent handler or NULL tcpauthevent handler");
return;
}
for(i = 0; i < MAX_TCPEVENTS; i++)
{
if(tcpevents[i].tcpevent == tcpevent)
{
Com_Error(ERR_FATAL, "NET_TCPAddEventType: Attempt to add an already defined redirect function twice.");
return;
}
if(tcpevents[i].tcpevent == NULL)
{
tcpevents[i].tcpevent = tcpevent;
tcpevents[i].tcpauthevent = tcpauthevent;
tcpevents[i].tcpconncloseevent = tcpconncloseevent;
tcpevents[i].serviceId = serviceId;
return;
}
}
Com_Error(ERR_FATAL, "NET_TCPAddEventType: Out of redirect handles. Increase MAX_TCPEVENTS to add more redirect destinations");
}