OpenTTD Source  12.0-beta2
tcp.cpp
Go to the documentation of this file.
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  */
7 
12 #include "../../stdafx.h"
13 #include "../../debug.h"
14 
15 #include "tcp.h"
16 
17 #include "../../safeguards.h"
18 
25  packet_queue(nullptr), packet_recv(nullptr),
26  sock(s), writable(false)
27 {
28 }
29 
30 NetworkTCPSocketHandler::~NetworkTCPSocketHandler()
31 {
32  this->EmptyPacketQueue();
33  this->CloseSocket();
34 }
35 
40 {
41  while (this->packet_queue != nullptr) {
42  delete Packet::PopFromQueue(&this->packet_queue);
43  }
44  delete this->packet_recv;
45  this->packet_recv = nullptr;
46 }
47 
54 {
55  if (this->sock != INVALID_SOCKET) closesocket(this->sock);
56  this->sock = INVALID_SOCKET;
57 }
58 
66 {
67  this->MarkClosed();
68  this->writable = false;
69 
70  this->EmptyPacketQueue();
71 
73 }
74 
82 {
83  assert(packet != nullptr);
84 
85  packet->PrepareToSend();
86  Packet::AddToQueue(&this->packet_queue, packet);
87 }
88 
100 {
101  ssize_t res;
102  Packet *p;
103 
104  /* We can not write to this socket!! */
105  if (!this->writable) return SPS_NONE_SENT;
106  if (!this->IsConnected()) return SPS_CLOSED;
107 
108  while ((p = this->packet_queue) != nullptr) {
109  res = p->TransferOut<int>(send, this->sock, 0);
110  if (res == -1) {
112  if (!err.WouldBlock()) {
113  /* Something went wrong.. close client! */
114  if (!closing_down) {
115  Debug(net, 0, "Send failed: {}", err.AsString());
116  this->CloseConnection();
117  }
118  return SPS_CLOSED;
119  }
120  return SPS_PARTLY_SENT;
121  }
122  if (res == 0) {
123  /* Client/server has left us :( */
124  if (!closing_down) this->CloseConnection();
125  return SPS_CLOSED;
126  }
127 
128  /* Is this packet sent? */
129  if (p->RemainingBytesToTransfer() == 0) {
130  /* Go to the next packet */
131  delete Packet::PopFromQueue(&this->packet_queue);
132  } else {
133  return SPS_PARTLY_SENT;
134  }
135  }
136 
137  return SPS_ALL_SENT;
138 }
139 
145 {
146  ssize_t res;
147 
148  if (!this->IsConnected()) return nullptr;
149 
150  if (this->packet_recv == nullptr) {
151  this->packet_recv = new Packet(this, TCP_MTU);
152  }
153 
154  Packet *p = this->packet_recv;
155 
156  /* Read packet size */
157  if (!p->HasPacketSizeData()) {
158  while (p->RemainingBytesToTransfer() != 0) {
159  res = p->TransferIn<int>(recv, this->sock, 0);
160  if (res == -1) {
162  if (!err.WouldBlock()) {
163  /* Something went wrong... */
164  if (!err.IsConnectionReset()) Debug(net, 0, "Recv failed: {}", err.AsString());
165  this->CloseConnection();
166  return nullptr;
167  }
168  /* Connection would block, so stop for now */
169  return nullptr;
170  }
171  if (res == 0) {
172  /* Client/server has left */
173  this->CloseConnection();
174  return nullptr;
175  }
176  }
177 
178  /* Parse the size in the received packet and if not valid, close the connection. */
179  if (!p->ParsePacketSize()) {
180  this->CloseConnection();
181  return nullptr;
182  }
183  }
184 
185  /* Read rest of packet */
186  while (p->RemainingBytesToTransfer() != 0) {
187  res = p->TransferIn<int>(recv, this->sock, 0);
188  if (res == -1) {
190  if (!err.WouldBlock()) {
191  /* Something went wrong... */
192  if (!err.IsConnectionReset()) Debug(net, 0, "Recv failed: {}", err.AsString());
193  this->CloseConnection();
194  return nullptr;
195  }
196  /* Connection would block */
197  return nullptr;
198  }
199  if (res == 0) {
200  /* Client/server has left */
201  this->CloseConnection();
202  return nullptr;
203  }
204  }
205 
206  /* Prepare for receiving a new packet */
207  this->packet_recv = nullptr;
208 
209  p->PrepareToRead();
210  return p;
211 }
212 
219 {
220  fd_set read_fd, write_fd;
221  struct timeval tv;
222 
223  FD_ZERO(&read_fd);
224  FD_ZERO(&write_fd);
225 
226  FD_SET(this->sock, &read_fd);
227  FD_SET(this->sock, &write_fd);
228 
229  tv.tv_sec = tv.tv_usec = 0; // don't block at all.
230  if (select(FD_SETSIZE, &read_fd, &write_fd, nullptr, &tv) < 0) return false;
231 
232  this->writable = !!FD_ISSET(this->sock, &write_fd);
233  return FD_ISSET(this->sock, &read_fd) != 0;
234 }
Packet::PrepareToSend
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
Definition: packet.cpp:83
NetworkSocketHandler
SocketHandler for all network sockets in OpenTTD.
Definition: core.h:42
SPS_CLOSED
@ SPS_CLOSED
The connection got closed.
Definition: tcp.h:25
NetworkTCPSocketHandler::IsConnected
bool IsConnected() const
Whether this socket is currently bound to a socket.
Definition: tcp.h:46
Packet::HasPacketSizeData
bool HasPacketSizeData() const
Check whether the packet, given the position of the "write" pointer, has read enough of the packet to...
Definition: packet.cpp:237
NetworkTCPSocketHandler::sock
SOCKET sock
The socket currently connected to.
Definition: tcp.h:39
Packet::TransferOut
ssize_t TransferOut(F transfer_function, D destination, Args &&... args)
Transfer data from the packet to the given function.
Definition: packet.h:141
Packet::ParsePacketSize
bool ParsePacketSize()
Reads the packet size from the raw packet and stores it in the packet->size.
Definition: packet.cpp:258
SPS_ALL_SENT
@ SPS_ALL_SENT
All packets in the queue are sent.
Definition: tcp.h:28
Packet::AddToQueue
static void AddToQueue(Packet **queue, Packet *packet)
Add the given Packet to the end of the queue of packets.
Definition: packet.cpp:59
NetworkTCPSocketHandler::ReceivePacket
virtual Packet * ReceivePacket()
Receives a packet for the given client.
Definition: tcp.cpp:144
Packet::PopFromQueue
static Packet * PopFromQueue(Packet **queue)
Pop the packet from the begin of the queue and set the begin of the queue to the second element in th...
Definition: packet.cpp:71
NetworkTCPSocketHandler::packet_recv
Packet * packet_recv
Partially received packet.
Definition: tcp.h:35
SendPacketsState
SendPacketsState
The states of sending the packets.
Definition: tcp.h:24
Packet::PrepareToRead
void PrepareToRead()
Prepares the packet so it can be read.
Definition: packet.cpp:277
NetworkError::WouldBlock
bool WouldBlock() const
Check whether this error describes that the operation would block.
Definition: os_abstraction.cpp:38
NetworkError::GetLast
static NetworkError GetLast()
Get the last network error.
Definition: os_abstraction.cpp:116
NetworkSocketHandler::MarkClosed
void MarkClosed()
Mark the connection as closed.
Definition: core.h:60
NetworkTCPSocketHandler::SendPacket
virtual void SendPacket(Packet *packet)
This function puts the packet in the send-queue and it is send as soon as possible.
Definition: tcp.cpp:81
NetworkTCPSocketHandler::CloseConnection
virtual NetworkRecvStatus CloseConnection(bool error=true)
This will put this socket handler in a close state.
Definition: tcp.cpp:65
Packet::TransferIn
ssize_t TransferIn(F transfer_function, S source, Args &&... args)
Transfer data from the given function into the packet.
Definition: packet.h:176
TCP_MTU
static const uint16 TCP_MTU
Number of bytes we can pack in a single TCP packet.
Definition: config.h:47
Packet
Internal entity of a packet.
Definition: packet.h:44
NetworkTCPSocketHandler::NetworkTCPSocketHandler
NetworkTCPSocketHandler(SOCKET s=INVALID_SOCKET)
Construct a socket handler for a TCP connection.
Definition: tcp.cpp:23
NetworkTCPSocketHandler::EmptyPacketQueue
void EmptyPacketQueue()
Free all pending and partially received packets.
Definition: tcp.cpp:39
NetworkTCPSocketHandler::writable
bool writable
Can we write to this socket?
Definition: tcp.h:40
SPS_NONE_SENT
@ SPS_NONE_SENT
The buffer is still full, so no (parts of) packets could be sent.
Definition: tcp.h:26
NetworkTCPSocketHandler::SendPackets
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Definition: tcp.cpp:99
NetworkTCPSocketHandler::CanSendReceive
bool CanSendReceive()
Check whether this socket can send or receive something.
Definition: tcp.cpp:218
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:22
tcp.h
NetworkError::IsConnectionReset
bool IsConnectionReset() const
Check whether this error describes a connection reset.
Definition: os_abstraction.cpp:53
NetworkError::AsString
const std::string & AsString() const
Get the string representation of the error message.
Definition: os_abstraction.cpp:79
NetworkError
Abstraction of a network error where all implementation details of the error codes are encapsulated i...
Definition: os_abstraction.h:21
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:132
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
SPS_PARTLY_SENT
@ SPS_PARTLY_SENT
The packets are partly sent; there are more packets to be sent in the queue.
Definition: tcp.h:27
NETWORK_RECV_STATUS_OKAY
@ NETWORK_RECV_STATUS_OKAY
Everything is okay.
Definition: core.h:23
Packet::RemainingBytesToTransfer
size_t RemainingBytesToTransfer() const
Get the amount of bytes that are still available for the Transfer functions.
Definition: packet.cpp:402
NetworkTCPSocketHandler::CloseSocket
void CloseSocket()
Close the actual socket of the connection.
Definition: tcp.cpp:53
NetworkTCPSocketHandler::packet_queue
Packet * packet_queue
Packets that are awaiting delivery.
Definition: tcp.h:34