OpenTTD

tcp.h
Go to the documentation of this file.
1 /* $Id: tcp.h 24900 2013-01-08 22:46:42Z planetmaker $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
14 #ifndef NETWORK_CORE_TCP_H
15 #define NETWORK_CORE_TCP_H
16
17 #include "address.h"
18 #include "packet.h"
19
20 #ifdef ENABLE_NETWORK
21
23 enum SendPacketsState {
24 SPS_CLOSED,
25 SPS_NONE_SENT,
26 SPS_PARTLY_SENT,
27 SPS_ALL_SENT,
28 };
29
31 class NetworkTCPSocketHandler : public NetworkSocketHandler {
32 private:
33 Packet *packet_queue;
34 Packet *packet_recv;
35 public:
36 SOCKET sock;
37 bool writable;
38
43 bool IsConnected() const { return this->sock != INVALID_SOCKET; }
44
45 virtual NetworkRecvStatus CloseConnection(bool error = true);
46 virtual void SendPacket(Packet *packet);
47 SendPacketsState SendPackets(bool closing_down = false);
48
49 virtual Packet *ReceivePacket();
50
51 bool CanSendReceive();
52
57 bool HasSendQueue() { return this->packet_queue != NULL; }
58
59 NetworkTCPSocketHandler(SOCKET s = INVALID_SOCKET);
60 ~NetworkTCPSocketHandler();
61 };
62
66 class TCPConnecter {
67 private:
68 class ThreadObject *thread;
69 bool connected;
70 bool aborted;
71 bool killed;
72 SOCKET sock;
73
74 void Connect();
75
76 static void ThreadEntry(void *param);
77
78 protected:
80 NetworkAddress address;
81
82 public:
83 TCPConnecter(const NetworkAddress &address);
85 virtual ~TCPConnecter() {}
86
91 virtual void OnConnect(SOCKET s) {}
92
96 virtual void OnFailure() {}
97
98 static void CheckCallbacks();
99 static void KillAll();
100 };
101
102 #endif /* ENABLE_NETWORK */
103
104 #endif /* NETWORK_CORE_TCP_H */
TCPConnecter::address
NetworkAddress address
Address we&#39;re connecting to.
Definition: tcp.h:80
NetworkTCPSocketHandler::sock
SOCKET sock
The socket currently connected to.
Definition: tcp.h:36
Packet
Internal entity of a packet.
Definition: packet.h:44
SPS_CLOSED
The connection got closed.
Definition: tcp.h:24
TCPConnecter
"Helper" class for creating TCP connections in a non-blocking manner
Definition: tcp.h:66
TCPConnecter::aborted
bool aborted
Whether we bailed out (i.e. connection making failed)
Definition: tcp.h:70
TCPConnecter::sock
SOCKET sock
The socket we&#39;re connecting with.
Definition: tcp.h:72
NetworkTCPSocketHandler
Base socket handler for all TCP sockets.
Definition: tcp.h:31
NetworkAddress
Wrapper for (un)resolved network addresses; there&#39;s no reason to transform a numeric IP to a string a...
Definition: address.h:31
TCPConnecter::thread
class ThreadObject * thread
Thread used to create the TCP connection.
Definition: tcp.h:68
TCPConnecter::OnConnect
virtual void OnConnect(SOCKET s)
Callback when the connection succeeded.
Definition: tcp.h:91
NetworkTCPSocketHandler::ReceivePacket
virtual Packet * ReceivePacket()
Receives a packet for the given client.
Definition: tcp.cpp:149
NetworkTCPSocketHandler::SendPackets
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Definition: tcp.cpp:99
TCPConnecter::OnFailure
virtual void OnFailure()
Callback for when the connection attempt failed.
Definition: tcp.h:96
NetworkTCPSocketHandler::CloseConnection
virtual NetworkRecvStatus CloseConnection(bool error=true)
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
Definition: tcp.cpp:42
NetworkTCPSocketHandler::writable
bool writable
Can we write to this socket?
Definition: tcp.h:37
NetworkTCPSocketHandler::packet_recv
Packet * packet_recv
Partially received packet.
Definition: tcp.h:34
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:26
address.h
Wrapper for network addresses.
SPS_ALL_SENT
All packets in the queue are sent.
Definition: tcp.h:27
TCPConnecter::~TCPConnecter
virtual ~TCPConnecter()
Silence the warnings.
Definition: tcp.h:85
SPS_NONE_SENT
The buffer is still full, so no (parts of) packets could be sent.
Definition: tcp.h:25
TCPConnecter::killed
bool killed
Whether we got killed.
Definition: tcp.h:71
TCPConnecter::connected
bool connected
Whether we succeeded in making the connection.
Definition: tcp.h:69
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:110
SPS_PARTLY_SENT
The packets are partly sent; there are more packets to be sent in the queue.
Definition: tcp.h:26
NetworkTCPSocketHandler::IsConnected
bool IsConnected() const
Whether this socket is currently bound to a socket.
Definition: tcp.h:43
packet.h
Basic functions to create, fill and read packets.
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:65
NetworkTCPSocketHandler::HasSendQueue
bool HasSendQueue()
Whether there is something pending in the send queue.
Definition: tcp.h:57
NetworkTCPSocketHandler::CanSendReceive
bool CanSendReceive()
Check whether this socket can send or receive something.
Definition: tcp.cpp:229
SendPacketsState
SendPacketsState
The states of sending the packets.
Definition: tcp.h:23
NetworkTCPSocketHandler::NetworkTCPSocketHandler
NetworkTCPSocketHandler(SOCKET s=INVALID_SOCKET)
Construct a socket handler for a TCP connection.
Definition: tcp.cpp:27
NetworkTCPSocketHandler::packet_queue
Packet * packet_queue
Packets that are awaiting delivery.
Definition: tcp.h:33
ThreadObject
A Thread Object which works on all our supported OSes.
Definition: thread.h:24
NetworkSocketHandler
SocketHandler for all network sockets in OpenTTD.
Definition: core.h:45