SuperTuxKart
network.hpp
Go to the documentation of this file.
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2013-2015 SuperTuxKart-Team
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
22 #ifndef HEADER_NETWORK_HPP
23 #define HEADER_NETWORK_HPP
24 
25 #include "utils/synchronised.hpp"
26 #include "utils/types.hpp"
27 
28 // enet.h includes win32.h, which without lean_and_mean includes
29 // winspool.h, which defines MAX_PRIORITY as a macro, which then
30 // results in request_manager.hpp not being compilable.
31 #define WIN32_LEAN_AND_MEAN
32 #include <enet/enet.h>
33 
34 #include <stdio.h>
35 #include <vector>
36 
37 class BareNetworkString;
38 class NetworkString;
39 class SocketAddress;
40 
45 class Network
46 {
47 private:
49  ENetHost* m_host;
50 
51  uint16_t m_port;
52 
53  bool m_ipv6_socket;
56 
57 public:
58  static bool m_connection_debug;
59  Network(int peer_count, int channel_limit,
60  uint32_t max_incoming_bandwidth,
61  uint32_t max_outgoing_bandwidth,
62  ENetAddress* address,
63  bool change_port_if_bound = false);
64  virtual ~Network();
65 
66  static void openLog();
67  static void logPacket(const BareNetworkString &ns, bool incoming);
68  static void closeLog();
69  ENetPeer *connectTo(const ENetAddress &address);
70  void sendRawPacket(const BareNetworkString &buffer,
71  const SocketAddress& dst);
72  int receiveRawPacket(char *buffer, int buf_len,
73  SocketAddress* sender, int max_tries = -1);
74  void broadcastPacket(NetworkString *data,
75  bool reliable = true);
76  // ------------------------------------------------------------------------
77  uint16_t getPort() const { return m_port; }
78  // ------------------------------------------------------------------------
80  ENetHost* getENetHost() { return m_host; }
81  // ------------------------------------------------------------------------
82  bool isIPv6Socket() { return m_ipv6_socket; }
83 }; // class Network
84 
85 #endif // HEADER_ENET_SOCKET_HPP
Describes a chain of 8-bit unsigned integers.
Definition: network_string.hpp:53
A new implementation of NetworkString, which has a fixed format: Byte 0: The type of the message,...
Definition: network_string.hpp:422
Definition: network.hpp:46
void broadcastPacket(NetworkString *data, bool reliable=true)
Broadcasts a packet to all peers.
Definition: network.cpp:214
Network(int peer_count, int channel_limit, uint32_t max_incoming_bandwidth, uint32_t max_outgoing_bandwidth, ENetAddress *address, bool change_port_if_bound=false)
Constructor that just initialises this object (esp.
Definition: network.cpp:61
virtual ~Network()
Destructor.
Definition: network.cpp:112
static Synchronised< FILE * > m_log_file
Where to log packets.
Definition: network.hpp:55
static void logPacket(const BareNetworkString &ns, bool incoming)
Log packets into a file.
Definition: network.cpp:243
int receiveRawPacket(char *buffer, int buf_len, SocketAddress *sender, int max_tries=-1)
Receives a packet directly from the network interface and filter its address.
Definition: network.cpp:163
ENetHost * m_host
ENet host interfacing sockets.
Definition: network.hpp:49
void sendRawPacket(const BareNetworkString &buffer, const SocketAddress &dst)
Sends a packet whithout ENet adding its headers.
Definition: network.cpp:132
ENetHost * getENetHost()
Returns a pointer to the ENet host object.
Definition: network.hpp:80
Describes a IPv4 or IPv6 address in sockaddr_in(6) format, suitable in using with sendto.
Definition: socket_address.hpp:47
Declares the general types that are used by the network.