~ubuntu-branches/debian/squeeze/openttd/squeeze

« back to all changes in this revision

Viewing changes to src/network/core/tcp.h

  • Committer: Bazaar Package Importer
  • Author(s): Jordi Mallach, Matthijs Kooijman, Jordi Mallach
  • Date: 2009-04-15 18:22:10 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090415182210-22ktb8kdbp2tf3bm
[ Matthijs Kooijman ]
* New upstream release.
* Remove Debian specific desktop file, upstream provides one now. 
* Add debian/watch file.

[ Jordi Mallach ]
* Bump Standards-Version to 3.8.1, with no changes required.
* Move to debhelper compat 7. Bump Build-Depends accordingly.
* Use dh_prep.
* Add "set -e" to config script.
* Remove a few extra doc files that get installed by upstream Makefile.
* Add more complete copyright information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: tcp.h 11818 2008-01-12 14:10:35Z rubidium $ */
 
1
/* $Id: tcp.h 15206 2009-01-22 10:09:56Z rubidium $ */
2
2
 
3
3
/**
4
4
 * @file tcp.h Basic functions to receive and send TCP packets.
10
10
#ifdef ENABLE_NETWORK
11
11
 
12
12
#include "os_abstraction.h"
 
13
#include "address.h"
13
14
#include "core.h"
14
15
#include "packet.h"
15
 
#include "../../tile_type.h"
16
 
 
17
 
/**
18
 
 * Enum with all types of UDP packets.
19
 
 * The order of the first 4 packets MUST not be changed, as
20
 
 * it protects old clients from joining newer servers
21
 
 * (because SERVER_ERROR is the respond to a wrong revision)
22
 
 */
23
 
enum {
24
 
        PACKET_SERVER_FULL,
25
 
        PACKET_SERVER_BANNED,
26
 
        PACKET_CLIENT_JOIN,
27
 
        PACKET_SERVER_ERROR,
28
 
        PACKET_CLIENT_COMPANY_INFO,
29
 
        PACKET_SERVER_COMPANY_INFO,
30
 
        PACKET_SERVER_CLIENT_INFO,
31
 
        PACKET_SERVER_NEED_PASSWORD,
32
 
        PACKET_CLIENT_PASSWORD,
33
 
        PACKET_SERVER_WELCOME,
34
 
        PACKET_CLIENT_GETMAP,
35
 
        PACKET_SERVER_WAIT,
36
 
        PACKET_SERVER_MAP,
37
 
        PACKET_CLIENT_MAP_OK,
38
 
        PACKET_SERVER_JOIN,
39
 
        PACKET_SERVER_FRAME,
40
 
        PACKET_SERVER_SYNC,
41
 
        PACKET_CLIENT_ACK,
42
 
        PACKET_CLIENT_COMMAND,
43
 
        PACKET_SERVER_COMMAND,
44
 
        PACKET_CLIENT_CHAT,
45
 
        PACKET_SERVER_CHAT,
46
 
        PACKET_CLIENT_SET_PASSWORD,
47
 
        PACKET_CLIENT_SET_NAME,
48
 
        PACKET_CLIENT_QUIT,
49
 
        PACKET_CLIENT_ERROR,
50
 
        PACKET_SERVER_QUIT,
51
 
        PACKET_SERVER_ERROR_QUIT,
52
 
        PACKET_SERVER_SHUTDOWN,
53
 
        PACKET_SERVER_NEWGAME,
54
 
        PACKET_SERVER_RCON,
55
 
        PACKET_CLIENT_RCON,
56
 
        PACKET_SERVER_CHECK_NEWGRFS,
57
 
        PACKET_CLIENT_NEWGRFS_CHECKED,
58
 
        PACKET_END                   ///< Must ALWAYS be on the end of this list!! (period)
59
 
};
60
 
 
61
 
/** Packet that wraps a command */
62
 
struct CommandPacket {
63
 
        CommandPacket *next; ///< the next command packet (if in queue)
64
 
        PlayerByte player; ///< player that is executing the command
65
 
        uint32 cmd;        ///< command being executed
66
 
        uint32 p1;         ///< parameter p1
67
 
        uint32 p2;         ///< parameter p2
68
 
        TileIndex tile;    ///< tile command being executed on
69
 
        char text[80];     ///< possible text sent for name changes etc
70
 
        uint32 frame;      ///< the frame in which this packet is executed
71
 
        byte callback;     ///< any callback function executed upon successful completion of the command
72
 
        bool my_cmd;       ///< did the command originate from "me"
73
 
};
74
 
 
75
 
/** Status of a client */
76
 
enum ClientStatus {
77
 
        STATUS_INACTIVE,   ///< The client is not connected nor active
78
 
        STATUS_AUTHORIZING,///< The client is authorizing
79
 
        STATUS_AUTH,       ///< The client is authorized
80
 
        STATUS_MAP_WAIT,   ///< The client is waiting as someone else is downloading the map
81
 
        STATUS_MAP,        ///< The client is downloading the map
82
 
        STATUS_DONE_MAP,   ///< The client has downloaded the map
83
 
        STATUS_PRE_ACTIVE, ///< The client is catching up the delayed frames
84
 
        STATUS_ACTIVE,     ///< The client is an active player in the game
85
 
};
86
16
 
87
17
/** Base socket handler for all TCP sockets */
88
18
class NetworkTCPSocketHandler : public NetworkSocketHandler {
89
 
/* TODO: rewrite into a proper class */
90
19
private:
91
20
        Packet *packet_queue;     ///< Packets that are awaiting delivery
92
21
        Packet *packet_recv;      ///< Partially received packet
93
22
public:
94
 
        uint16 index;             ///< Client index
95
 
        uint32 last_frame;        ///< Last frame we have executed
96
 
        uint32 last_frame_server; ///< Last frame the server has executed
97
 
        byte lag_test;            ///< Byte used for lag-testing the client
98
 
 
99
 
        ClientStatus status;      ///< Status of this client
100
23
        bool writable;            ///< Can we write to this socket?
101
24
 
102
 
        CommandPacket *command_queue; ///< The command-queue awaiting delivery
103
 
 
104
 
        NetworkRecvStatus CloseConnection();
105
 
        void Initialize();
106
 
        void Destroy();
107
 
 
 
25
        virtual NetworkRecvStatus CloseConnection();
108
26
        void Send_Packet(Packet *packet);
109
27
        bool Send_Packets();
110
28
        bool IsPacketQueueEmpty();
111
29
 
112
30
        Packet *Recv_Packet(NetworkRecvStatus *status);
 
31
 
 
32
        NetworkTCPSocketHandler(SOCKET s = INVALID_SOCKET);
 
33
        ~NetworkTCPSocketHandler();
 
34
};
 
35
 
 
36
/**
 
37
 * "Helper" class for creating TCP connections in a non-blocking manner
 
38
 */
 
39
class TCPConnecter {
 
40
private:
 
41
        class ThreadObject *thread; ///< Thread used to create the TCP connection
 
42
        bool connected;             ///< Whether we succeeded in making the connection
 
43
        bool aborted;               ///< Whether we bailed out (i.e. connection making failed)
 
44
        bool killed;                ///< Whether we got killed
 
45
        SOCKET sock;                ///< The socket we're connecting with
 
46
 
 
47
        /** The actual connection function */
 
48
        void Connect();
 
49
 
 
50
        /**
 
51
         * Entry point for the new threads.
 
52
         * @param param the TCPConnecter instance to call Connect on.
 
53
         */
 
54
        static void ThreadEntry(void *param);
 
55
 
 
56
protected:
 
57
        /** Address we're connecting to */
 
58
        NetworkAddress address;
 
59
 
 
60
public:
 
61
        /**
 
62
         * Create a new connecter for the given address
 
63
         * @param address the (un)resolved address to connect to
 
64
         */
 
65
        TCPConnecter(const NetworkAddress &address);
 
66
        /** Silence the warnings */
 
67
        virtual ~TCPConnecter() {}
 
68
 
 
69
        /**
 
70
         * Callback when the connection succeeded.
 
71
         * @param s the socket that we opened
 
72
         */
 
73
        virtual void OnConnect(SOCKET s) {}
 
74
 
 
75
        /**
 
76
         * Callback for when the connection attempt failed.
 
77
         */
 
78
        virtual void OnFailure() {}
 
79
 
 
80
        /**
 
81
         * Check whether we need to call the callback, i.e. whether we
 
82
         * have connected or aborted and call the appropriate callback
 
83
         * for that. It's done this way to ease on the locking that
 
84
         * would otherwise be needed everywhere.
 
85
         */
 
86
        static void CheckCallbacks();
 
87
 
 
88
        /** Kill all connection attempts. */
 
89
        static void KillAll();
113
90
};
114
91
 
115
92
#endif /* ENABLE_NETWORK */