author | celestar |
Thu, 11 Jan 2007 13:16:26 +0000 | |
branch | custombridgeheads |
changeset 5648 | 1608018c5ff2 |
parent 5643 | 3778051e8095 |
permissions | -rw-r--r-- |
5642 | 1 |
/* $Id$ */ |
2 |
||
3 |
#ifdef ENABLE_NETWORK |
|
4 |
||
5 |
#include "../../stdafx.h" |
|
6 |
#include "../../debug.h" |
|
7 |
#include "../../openttd.h" |
|
8 |
#include "../../variables.h" |
|
5643
3778051e8095
(svn r7790) [cbh] -Sync: execute the same script for /branches/custombridgeheads as was done for /trunk in r7759.
rubidium
parents:
5642
diff
changeset
|
9 |
#include "table/strings.h" |
5642 | 10 |
#include "../../functions.h" |
11 |
||
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
12 |
#include "../network_data.h" |
5642 | 13 |
#include "packet.h" |
14 |
#include "tcp.h" |
|
15 |
||
16 |
/** |
|
17 |
* @file tcp.c Basic functions to receive and send TCP packets. |
|
18 |
*/ |
|
19 |
||
20 |
/** |
|
21 |
* Functions to help NetworkRecv_Packet/NetworkSend_Packet a bit |
|
22 |
* A socket can make errors. When that happens this handles what to do. |
|
23 |
* For clients: close connection and drop back to main-menu |
|
24 |
* For servers: close connection and that is it |
|
25 |
* @param cs the client to close the connection of |
|
26 |
* @return the new status |
|
27 |
*/ |
|
28 |
NetworkRecvStatus CloseConnection(NetworkClientState *cs) |
|
29 |
{ |
|
30 |
NetworkCloseClient(cs); |
|
31 |
||
32 |
/* Clients drop back to the main menu */ |
|
33 |
if (!_network_server && _networking) { |
|
34 |
_switch_mode = SM_MENU; |
|
35 |
_networking = false; |
|
36 |
_switch_mode_errorstr = STR_NETWORK_ERR_LOSTCONNECTION; |
|
37 |
||
38 |
return NETWORK_RECV_STATUS_CONN_LOST; |
|
39 |
} |
|
40 |
||
41 |
return NETWORK_RECV_STATUS_OKAY; |
|
42 |
} |
|
43 |
||
44 |
/** |
|
45 |
* Whether the client has quit or not (used in packet.c) |
|
46 |
* @param cs the client to check |
|
47 |
* @return true if the client has quit |
|
48 |
*/ |
|
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
49 |
bool HasClientQuit(const NetworkClientState *cs) |
5642 | 50 |
{ |
51 |
return cs->has_quit; |
|
52 |
} |
|
53 |
||
54 |
/** |
|
55 |
* This function puts the packet in the send-queue and it is send as |
|
56 |
* soon as possible. This is the next tick, or maybe one tick later |
|
57 |
* if the OS-network-buffer is full) |
|
58 |
* @param packet the packet to send |
|
59 |
* @param cs the client to send to |
|
60 |
*/ |
|
61 |
void NetworkSend_Packet(Packet *packet, NetworkClientState *cs) |
|
62 |
{ |
|
63 |
Packet *p; |
|
64 |
assert(packet != NULL); |
|
65 |
||
66 |
packet->pos = 0; |
|
67 |
packet->next = NULL; |
|
68 |
||
69 |
NetworkSend_FillPacketSize(packet); |
|
70 |
||
71 |
/* Locate last packet buffered for the client */ |
|
72 |
p = cs->packet_queue; |
|
73 |
if (p == NULL) { |
|
74 |
/* No packets yet */ |
|
75 |
cs->packet_queue = packet; |
|
76 |
} else { |
|
77 |
/* Skip to the last packet */ |
|
78 |
while (p->next != NULL) p = p->next; |
|
79 |
p->next = packet; |
|
80 |
} |
|
81 |
} |
|
82 |
||
83 |
/** |
|
84 |
* Sends all the buffered packets out for this client. It stops when: |
|
85 |
* 1) all packets are send (queue is empty) |
|
86 |
* 2) the OS reports back that it can not send any more |
|
87 |
* data right now (full network-buffer, it happens ;)) |
|
88 |
* 3) sending took too long |
|
89 |
* @param cs the client to send the packets for |
|
90 |
*/ |
|
91 |
bool NetworkSend_Packets(NetworkClientState *cs) |
|
92 |
{ |
|
93 |
ssize_t res; |
|
94 |
Packet *p; |
|
95 |
||
96 |
/* We can not write to this socket!! */ |
|
97 |
if (!cs->writable) return false; |
|
98 |
if (cs->socket == INVALID_SOCKET) return false; |
|
99 |
||
100 |
p = cs->packet_queue; |
|
101 |
while (p != NULL) { |
|
102 |
res = send(cs->socket, p->buffer + p->pos, p->size - p->pos, 0); |
|
103 |
if (res == -1) { |
|
104 |
int err = GET_LAST_ERROR(); |
|
105 |
if (err != EWOULDBLOCK) { |
|
106 |
/* Something went wrong.. close client! */ |
|
107 |
DEBUG(net, 0, "send failed with error %d", err); |
|
108 |
CloseConnection(cs); |
|
109 |
return false; |
|
110 |
} |
|
111 |
return true; |
|
112 |
} |
|
113 |
if (res == 0) { |
|
114 |
/* Client/server has left us :( */ |
|
115 |
CloseConnection(cs); |
|
116 |
return false; |
|
117 |
} |
|
118 |
||
119 |
p->pos += res; |
|
120 |
||
121 |
/* Is this packet sent? */ |
|
122 |
if (p->pos == p->size) { |
|
123 |
/* Go to the next packet */ |
|
124 |
cs->packet_queue = p->next; |
|
125 |
free(p); |
|
126 |
p = cs->packet_queue; |
|
127 |
} else { |
|
128 |
return true; |
|
129 |
} |
|
130 |
} |
|
131 |
||
132 |
return true; |
|
133 |
} |
|
134 |
||
135 |
/** |
|
136 |
* Receives a packet for the given client |
|
137 |
* @param cs the client to (try to) receive a packet for |
|
138 |
* @param status the variable to store the status into |
|
139 |
* @return the received packet (or NULL when it didn't receive one) |
|
140 |
*/ |
|
141 |
Packet *NetworkRecv_Packet(NetworkClientState *cs, NetworkRecvStatus *status) |
|
142 |
{ |
|
143 |
ssize_t res; |
|
144 |
Packet *p; |
|
145 |
||
146 |
*status = NETWORK_RECV_STATUS_OKAY; |
|
147 |
||
148 |
if (cs->socket == INVALID_SOCKET) return NULL; |
|
149 |
||
150 |
if (cs->packet_recv == NULL) { |
|
151 |
cs->packet_recv = malloc(sizeof(Packet)); |
|
152 |
if (cs->packet_recv == NULL) error("Failed to allocate packet"); |
|
153 |
/* Set pos to zero! */ |
|
154 |
cs->packet_recv->pos = 0; |
|
155 |
cs->packet_recv->size = 0; // Can be ommited, just for safety reasons |
|
156 |
} |
|
157 |
||
158 |
p = cs->packet_recv; |
|
159 |
||
160 |
/* Read packet size */ |
|
161 |
if (p->pos < sizeof(PacketSize)) { |
|
162 |
while (p->pos < sizeof(PacketSize)) { |
|
163 |
/* Read the size of the packet */ |
|
164 |
res = recv(cs->socket, p->buffer + p->pos, sizeof(PacketSize) - p->pos, 0); |
|
165 |
if (res == -1) { |
|
166 |
int err = GET_LAST_ERROR(); |
|
167 |
if (err != EWOULDBLOCK) { |
|
168 |
/* Something went wrong... (104 is connection reset by peer) */ |
|
169 |
if (err != 104) DEBUG(net, 0, "recv failed with error %d", err); |
|
170 |
*status = CloseConnection(cs); |
|
171 |
return NULL; |
|
172 |
} |
|
173 |
/* Connection would block, so stop for now */ |
|
174 |
return NULL; |
|
175 |
} |
|
176 |
if (res == 0) { |
|
177 |
/* Client/server has left */ |
|
178 |
*status = CloseConnection(cs); |
|
179 |
return NULL; |
|
180 |
} |
|
181 |
p->pos += res; |
|
182 |
} |
|
183 |
||
184 |
NetworkRecv_ReadPacketSize(p); |
|
185 |
||
186 |
if (p->size > SEND_MTU) { |
|
187 |
*status = CloseConnection(cs); |
|
188 |
return NULL; |
|
189 |
} |
|
190 |
} |
|
191 |
||
192 |
/* Read rest of packet */ |
|
193 |
while (p->pos < p->size) { |
|
194 |
res = recv(cs->socket, p->buffer + p->pos, p->size - p->pos, 0); |
|
195 |
if (res == -1) { |
|
196 |
int err = GET_LAST_ERROR(); |
|
197 |
if (err != EWOULDBLOCK) { |
|
198 |
/* Something went wrong... (104 is connection reset by peer) */ |
|
199 |
if (err != 104) DEBUG(net, 0, "recv failed with error %d", err); |
|
200 |
*status = CloseConnection(cs); |
|
201 |
return NULL; |
|
202 |
} |
|
203 |
/* Connection would block */ |
|
204 |
return NULL; |
|
205 |
} |
|
206 |
if (res == 0) { |
|
207 |
/* Client/server has left */ |
|
208 |
*status = CloseConnection(cs); |
|
209 |
return NULL; |
|
210 |
} |
|
211 |
||
212 |
p->pos += res; |
|
213 |
} |
|
214 |
||
215 |
/* We have a complete packet, return it! */ |
|
216 |
p->pos = 2; |
|
217 |
p->next = NULL; // Should not be needed, but who knows... |
|
218 |
||
219 |
/* Prepare for receiving a new packet */ |
|
220 |
cs->packet_recv = NULL; |
|
221 |
||
222 |
return p; |
|
223 |
} |
|
224 |
||
225 |
#endif /* ENABLE_NETWORK */ |