OpenTTD Source 20261112-master-gbcbec5e63f
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../../stdafx.h"
11#include "../../debug.h"
12
13#include "tcp.h"
14
15#include "../../safeguards.h"
16
22
29{
30 if (this->sock != INVALID_SOCKET) closesocket(this->sock);
31 this->sock = INVALID_SOCKET;
32}
33
41{
42 this->MarkClosed();
43 this->writable = false;
44
45 this->packet_queue.clear();
46 this->packet_recv = nullptr;
47
49}
50
57void NetworkTCPSocketHandler::SendPacket(std::unique_ptr<Packet> &&packet)
58{
59 assert(packet != nullptr);
60
61 packet->PrepareToSend();
62 this->packet_queue.push_back(std::move(packet));
63}
64
76{
77 /* We can not write to this socket!! */
78 if (!this->writable) return SendPacketsState::NoneSent;
79 if (!this->IsConnected()) return SendPacketsState::Closed;
80
81 while (!this->packet_queue.empty()) {
82 Packet &p = *this->packet_queue.front();
83 std::ptrdiff_t res = p.TransferOut(SocketSender{this->sock});
84 if (res == -1) {
86 if (!err.WouldBlock()) {
87 /* Something went wrong.. close client! */
88 if (!closing_down) {
89 Debug(Facility::Net, Severity::Critical, "Send failed: {}", err.AsString());
90 this->CloseConnection();
91 }
93 }
95 }
96 if (res == 0) {
97 /* Client/server has left us :( */
98 if (!closing_down) this->CloseConnection();
100 }
101
102 /* Is this packet sent? */
103 if (p.RemainingBytesToTransfer() == 0) {
104 /* Go to the next packet */
105 this->packet_queue.pop_front();
106 } else {
108 }
109 }
110
112}
113
119{
120 if (!this->IsConnected()) return nullptr;
121
122 if (this->packet_recv == nullptr) {
123 this->packet_recv = std::make_unique<Packet>(this, TCP_MTU);
124 }
125
126 Packet &p = *this->packet_recv.get();
127
128 /* Read packet size */
129 if (!p.HasPacketSizeData()) {
130 while (p.RemainingBytesToTransfer() != 0) {
131 std::ptrdiff_t res = p.TransferIn(SocketReceiver{this->sock});
132 if (res == -1) {
134 if (!err.WouldBlock()) {
135 /* Something went wrong... */
136 if (!err.IsConnectionReset()) Debug(Facility::Net, Severity::Critical, "Recv failed: {}", err.AsString());
137 this->CloseConnection();
138 return nullptr;
139 }
140 /* Connection would block, so stop for now */
141 return nullptr;
142 }
143 if (res == 0) {
144 /* Client/server has left */
145 this->CloseConnection();
146 return nullptr;
147 }
148 }
149
150 /* Parse the size in the received packet and if not valid, close the connection. */
151 if (!p.ParsePacketSize()) {
152 this->CloseConnection();
153 return nullptr;
154 }
155 }
156
157 /* Read rest of packet */
158 while (p.RemainingBytesToTransfer() != 0) {
159 std::ptrdiff_t res = p.TransferIn(SocketReceiver{this->sock});
160 if (res == -1) {
162 if (!err.WouldBlock()) {
163 /* Something went wrong... */
164 if (!err.IsConnectionReset()) Debug(Facility::Net, Severity::Critical, "Recv failed: {}", err.AsString());
165 this->CloseConnection();
166 return nullptr;
167 }
168 /* Connection would block */
169 return nullptr;
170 }
171 if (res == 0) {
172 /* Client/server has left */
173 this->CloseConnection();
174 return nullptr;
175 }
176 }
177
178 if (!p.PrepareToRead()) {
179 Debug(Facility::Net, Severity::Critical, "Invalid packet received (too small / decryption error)");
180 this->CloseConnection();
181 return nullptr;
182 }
183 return std::move(this->packet_recv);
184}
185
192{
193 assert(this->sock != INVALID_SOCKET);
194
195 fd_set read_fd, write_fd;
196 struct timeval tv;
197
198 FD_ZERO(&read_fd);
199 FD_ZERO(&write_fd);
200
201 FD_SET(this->sock, &read_fd);
202 FD_SET(this->sock, &write_fd);
203
204 tv.tv_sec = tv.tv_usec = 0; // don't block at all.
205 if (select(FD_SETSIZE, &read_fd, &write_fd, nullptr, &tv) < 0) return false;
206
207 this->writable = !!FD_ISSET(this->sock, &write_fd);
208 return FD_ISSET(this->sock, &read_fd) != 0;
209}
Abstraction of a network error where all implementation details of the error codes are encapsulated i...
std::string_view AsString() const
Get the string representation of the error message.
bool WouldBlock() const
Check whether this error describes that the operation would block.
bool IsConnectionReset() const
Check whether this error describes a connection reset.
static NetworkError GetLast()
Get the last network error.
void MarkClosed()
Mark the connection as closed.
Definition core.h:62
std::deque< std::unique_ptr< Packet > > packet_queue
Packets that are awaiting delivery. Cannot be std::queue as that does not have a clear() function.
Definition tcp.h:31
virtual NetworkRecvStatus CloseConnection(bool error=true)
This will put this socket handler in a close state.
Definition tcp.cpp:40
virtual std::unique_ptr< Packet > ReceivePacket()
Receives a packet for the given client.
Definition tcp.cpp:118
~NetworkTCPSocketHandler() override
Close the socket.
Definition tcp.cpp:18
bool IsConnected() const
Whether this socket is currently bound to a socket.
Definition tcp.h:42
SOCKET sock
The socket currently connected to.
Definition tcp.h:35
bool writable
Can we write to this socket?
Definition tcp.h:36
virtual void SendPacket(std::unique_ptr< Packet > &&packet)
This function puts the packet in the send-queue and it is send as soon as possible.
Definition tcp.cpp:57
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Definition tcp.cpp:75
void CloseSocket()
Close the actual socket of the connection.
Definition tcp.cpp:28
bool CanSendReceive()
Check whether this socket can send or receive something.
Definition tcp.cpp:191
std::unique_ptr< Packet > packet_recv
Partially received packet.
Definition tcp.h:32
static const size_t TCP_MTU
Number of bytes we can pack in a single TCP packet.
Definition config.h:43
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition core.h:19
@ Okay
Everything is okay.
Definition core.h:20
Functions related to debugging.
#define Debug(facility, severity, format_string,...)
Output a line of debugging information.
Definition debug.h:36
@ Net
Net message facility.
Definition debug_type.h:33
@ Critical
Critical, user should know about this.
Definition debug_type.h:15
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
std::ptrdiff_t TransferIn(F transfer_function)
Transfer data from the given function into the packet.
Definition packet.h:182
bool PrepareToRead()
Prepares the packet so it can be read.
Definition packet.cpp:276
bool HasPacketSizeData() const
Check whether the packet, given the position of the "write" pointer, has read enough of the packet to...
Definition packet.cpp:236
bool ParsePacketSize()
Reads the packet size from the raw packet and stores it in the packet->size.
Definition packet.cpp:257
size_t RemainingBytesToTransfer() const
Get the amount of bytes that are still available for the Transfer functions.
Definition packet.cpp:445
std::ptrdiff_t TransferOut(F transfer_function)
Transfer data from the packet to the given function.
Definition packet.h:153
Helper for Packet::TransferIn that reads data from a socket.
IPv4 addresses should be 4 bytes.
Basic functions to receive and send TCP packets.
SendPacketsState
The states of sending the packets.
Definition tcp.h:21
@ Closed
The connection got closed.
Definition tcp.h:22
@ PartlySent
The packets are partly sent; there are more packets to be sent in the queue.
Definition tcp.h:24
@ NoneSent
The buffer is still full, so no (parts of) packets could be sent.
Definition tcp.h:23
@ AllSent
All packets in the queue are sent.
Definition tcp.h:25