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 
28 };
29 
32 private:
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);
61 };
62 
66 class TCPConnecter {
67 private:
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:
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 */
NetworkAddress address
Address we&#39;re connecting to.
Definition: tcp.h:80
SOCKET sock
The socket currently connected to.
Definition: tcp.h:36
Internal entity of a packet.
Definition: packet.h:44
The connection got closed.
Definition: tcp.h:24
"Helper" class for creating TCP connections in a non-blocking manner
Definition: tcp.h:66
bool aborted
Whether we bailed out (i.e. connection making failed)
Definition: tcp.h:70
SOCKET sock
The socket we&#39;re connecting with.
Definition: tcp.h:72
Base socket handler for all TCP sockets.
Definition: tcp.h:31
Wrapper for (un)resolved network addresses; there&#39;s no reason to transform a numeric IP to a string a...
Definition: address.h:31
class ThreadObject * thread
Thread used to create the TCP connection.
Definition: tcp.h:68
virtual void OnConnect(SOCKET s)
Callback when the connection succeeded.
Definition: tcp.h:91
virtual Packet * ReceivePacket()
Receives a packet for the given client.
Definition: tcp.cpp:149
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Definition: tcp.cpp:99
virtual void OnFailure()
Callback for when the connection attempt failed.
Definition: tcp.h:96
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
bool writable
Can we write to this socket?
Definition: tcp.h:37
Packet * packet_recv
Partially received packet.
Definition: tcp.h:34
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:26
Wrapper for network addresses.
All packets in the queue are sent.
Definition: tcp.h:27
virtual ~TCPConnecter()
Silence the warnings.
Definition: tcp.h:85
The buffer is still full, so no (parts of) packets could be sent.
Definition: tcp.h:25
bool killed
Whether we got killed.
Definition: tcp.h:71
bool connected
Whether we succeeded in making the connection.
Definition: tcp.h:69
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:110
The packets are partly sent; there are more packets to be sent in the queue.
Definition: tcp.h:26
bool IsConnected() const
Whether this socket is currently bound to a socket.
Definition: tcp.h:43
Basic functions to create, fill and read packets.
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
bool HasSendQueue()
Whether there is something pending in the send queue.
Definition: tcp.h:57
bool CanSendReceive()
Check whether this socket can send or receive something.
Definition: tcp.cpp:229
SendPacketsState
The states of sending the packets.
Definition: tcp.h:23
NetworkTCPSocketHandler(SOCKET s=INVALID_SOCKET)
Construct a socket handler for a TCP connection.
Definition: tcp.cpp:27
Packet * packet_queue
Packets that are awaiting delivery.
Definition: tcp.h:33
A Thread Object which works on all our supported OSes.
Definition: thread.h:24
SocketHandler for all network sockets in OpenTTD.
Definition: core.h:45