Fix: Data races on cursor state in OpenGL backends
[openttd-github.git] / src / network / core / tcp.cpp
bloba51913d84349de612009798fc6ce3bd90d1b70da
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD 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, version 2.
4 * OpenTTD 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.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /**
9 * @file tcp.cpp Basic functions to receive and send TCP packets.
12 #include "../../stdafx.h"
13 #include "../../debug.h"
15 #include "tcp.h"
17 #include "../../safeguards.h"
19 /**
20 * Construct a socket handler for a TCP connection.
21 * @param s The just opened TCP connection.
23 NetworkTCPSocketHandler::NetworkTCPSocketHandler(SOCKET s) :
24 NetworkSocketHandler(),
25 packet_queue(nullptr), packet_recv(nullptr),
26 sock(s), writable(false)
30 NetworkTCPSocketHandler::~NetworkTCPSocketHandler()
32 this->CloseConnection();
34 if (this->sock != INVALID_SOCKET) closesocket(this->sock);
35 this->sock = INVALID_SOCKET;
38 NetworkRecvStatus NetworkTCPSocketHandler::CloseConnection(bool error)
40 this->writable = false;
41 NetworkSocketHandler::CloseConnection(error);
43 /* Free all pending and partially received packets */
44 while (this->packet_queue != nullptr) {
45 Packet *p = this->packet_queue->next;
46 delete this->packet_queue;
47 this->packet_queue = p;
49 delete this->packet_recv;
50 this->packet_recv = nullptr;
52 return NETWORK_RECV_STATUS_OKAY;
55 /**
56 * This function puts the packet in the send-queue and it is send as
57 * soon as possible. This is the next tick, or maybe one tick later
58 * if the OS-network-buffer is full)
59 * @param packet the packet to send
61 void NetworkTCPSocketHandler::SendPacket(Packet *packet)
63 Packet *p;
64 assert(packet != nullptr);
66 packet->PrepareToSend();
68 /* Reallocate the packet as in 99+% of the times we send at most 25 bytes and
69 * keeping the other 1400+ bytes wastes memory, especially when someone tries
70 * to do a denial of service attack! */
71 packet->buffer = ReallocT(packet->buffer, packet->size);
73 /* Locate last packet buffered for the client */
74 p = this->packet_queue;
75 if (p == nullptr) {
76 /* No packets yet */
77 this->packet_queue = packet;
78 } else {
79 /* Skip to the last packet */
80 while (p->next != nullptr) p = p->next;
81 p->next = packet;
85 /**
86 * Sends all the buffered packets out for this client. It stops when:
87 * 1) all packets are send (queue is empty)
88 * 2) the OS reports back that it can not send any more
89 * data right now (full network-buffer, it happens ;))
90 * 3) sending took too long
91 * @param closing_down Whether we are closing down the connection.
92 * @return \c true if a (part of a) packet could be sent and
93 * the connection is not closed yet.
95 SendPacketsState NetworkTCPSocketHandler::SendPackets(bool closing_down)
97 ssize_t res;
98 Packet *p;
100 /* We can not write to this socket!! */
101 if (!this->writable) return SPS_NONE_SENT;
102 if (!this->IsConnected()) return SPS_CLOSED;
104 p = this->packet_queue;
105 while (p != nullptr) {
106 res = send(this->sock, (const char*)p->buffer + p->pos, p->size - p->pos, 0);
107 if (res == -1) {
108 int err = GET_LAST_ERROR();
109 if (err != EWOULDBLOCK) {
110 /* Something went wrong.. close client! */
111 if (!closing_down) {
112 DEBUG(net, 0, "send failed with error %d", err);
113 this->CloseConnection();
115 return SPS_CLOSED;
117 return SPS_PARTLY_SENT;
119 if (res == 0) {
120 /* Client/server has left us :( */
121 if (!closing_down) this->CloseConnection();
122 return SPS_CLOSED;
125 p->pos += res;
127 /* Is this packet sent? */
128 if (p->pos == p->size) {
129 /* Go to the next packet */
130 this->packet_queue = p->next;
131 delete p;
132 p = this->packet_queue;
133 } else {
134 return SPS_PARTLY_SENT;
138 return SPS_ALL_SENT;
142 * Receives a packet for the given client
143 * @return The received packet (or nullptr when it didn't receive one)
145 Packet *NetworkTCPSocketHandler::ReceivePacket()
147 ssize_t res;
149 if (!this->IsConnected()) return nullptr;
151 if (this->packet_recv == nullptr) {
152 this->packet_recv = new Packet(this);
155 Packet *p = this->packet_recv;
157 /* Read packet size */
158 if (p->pos < sizeof(PacketSize)) {
159 while (p->pos < sizeof(PacketSize)) {
160 /* Read the size of the packet */
161 res = recv(this->sock, (char*)p->buffer + p->pos, sizeof(PacketSize) - p->pos, 0);
162 if (res == -1) {
163 int err = GET_LAST_ERROR();
164 if (err != EWOULDBLOCK) {
165 /* Something went wrong... (104 is connection reset by peer) */
166 if (err != 104) DEBUG(net, 0, "recv failed with error %d", err);
167 this->CloseConnection();
168 return nullptr;
170 /* Connection would block, so stop for now */
171 return nullptr;
173 if (res == 0) {
174 /* Client/server has left */
175 this->CloseConnection();
176 return nullptr;
178 p->pos += res;
181 /* Read the packet size from the received packet */
182 p->ReadRawPacketSize();
184 if (p->size > SEND_MTU) {
185 this->CloseConnection();
186 return nullptr;
190 /* Read rest of packet */
191 while (p->pos < p->size) {
192 res = recv(this->sock, (char*)p->buffer + p->pos, p->size - p->pos, 0);
193 if (res == -1) {
194 int err = GET_LAST_ERROR();
195 if (err != EWOULDBLOCK) {
196 /* Something went wrong... (104 is connection reset by peer) */
197 if (err != 104) DEBUG(net, 0, "recv failed with error %d", err);
198 this->CloseConnection();
199 return nullptr;
201 /* Connection would block */
202 return nullptr;
204 if (res == 0) {
205 /* Client/server has left */
206 this->CloseConnection();
207 return nullptr;
210 p->pos += res;
213 /* Prepare for receiving a new packet */
214 this->packet_recv = nullptr;
216 p->PrepareToRead();
217 return p;
221 * Check whether this socket can send or receive something.
222 * @return \c true when there is something to receive.
223 * @note Sets #writable if more data can be sent.
225 bool NetworkTCPSocketHandler::CanSendReceive()
227 fd_set read_fd, write_fd;
228 struct timeval tv;
230 FD_ZERO(&read_fd);
231 FD_ZERO(&write_fd);
233 FD_SET(this->sock, &read_fd);
234 FD_SET(this->sock, &write_fd);
236 tv.tv_sec = tv.tv_usec = 0; // don't block at all.
237 if (select(FD_SETSIZE, &read_fd, &write_fd, nullptr, &tv) < 0) return false;
239 this->writable = !!FD_ISSET(this->sock, &write_fd);
240 return FD_ISSET(this->sock, &read_fd) != 0;