SuperTuxKart
connect_to_server.hpp
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 
19 #ifndef CONNECT_TO_SERVER_HPP
20 #define CONNECT_TO_SERVER_HPP
21 
22 #include "network/protocol.hpp"
23 #include "utils/cpp2011.hpp"
24 #include <atomic>
25 #include <memory>
26 #include <string>
27 
28 #include "irrString.h"
29 #include <enet/enet.h>
30 
31 namespace Online
32 {
33  class Request;
34 }
35 
36 class Server;
37 
38 class ConnectToServer : public Protocol
39 {
40 private:
41  std::shared_ptr<Server> m_server;
42 
43  irr::core::stringw m_error_msg;
44 
46  enum ConnectState : unsigned int
47  {
48  SET_PUBLIC_ADDRESS,
49  GOT_SERVER_ADDRESS,
50  DONE,
51  EXITING
52  };
53  std::atomic<ConnectState> m_state;
54 
55  void getClientServerInfo();
56  bool registerWithSTKServer();
57  bool tryConnect(int timeout, int retry, bool another_port = false,
58  bool ipv6 = false);
59  static ENetAddress m_server_address;
60  static int interceptCallback(ENetHost* host, ENetEvent* event);
61  static int m_retry_count;
62  static bool m_done_intecept;
63  bool detectPort();
64 public:
65  ConnectToServer(std::shared_ptr<Server> server);
66  virtual ~ConnectToServer();
67  virtual void setup() OVERRIDE;
68  virtual void asynchronousUpdate() OVERRIDE;
69  virtual void update(int ticks) OVERRIDE;
70 }; // class ConnectToServer
71 
72 #endif // CONNECT_TO_SERVER_HPP
Definition: connect_to_server.hpp:39
virtual void update(int ticks) OVERRIDE
Called by the protocol listener, synchronously with the main loop.
Definition: connect_to_server.cpp:350
ConnectToServer(std::shared_ptr< Server > server)
Specify server to connect to.
Definition: connect_to_server.cpp:82
bool registerWithSTKServer()
Register this client with the STK server.
Definition: connect_to_server.cpp:497
ConnectState
State for finite state machine.
Definition: connect_to_server.hpp:47
virtual ~ConnectToServer()
Destructor.
Definition: connect_to_server.cpp:93
virtual void setup() OVERRIDE
Initialise the protocol.
Definition: connect_to_server.cpp:108
static int interceptCallback(ENetHost *host, ENetEvent *event)
Intercept callback in enet to allow change server address and port if needed (Happens when there is f...
Definition: connect_to_server.cpp:402
virtual void asynchronousUpdate() OVERRIDE
Called by the protocol listener as often as possible.
Definition: connect_to_server.cpp:178
Abstract class used to define the global protocol functions.
Definition: protocol.hpp:68
Definition: server.hpp:44
Generic protocols declarations.