1 //       _________ __                 __
2 //      /   _____//  |_____________ _/  |______     ____  __ __  ______
3 //      \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
4 //      /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ |
5 //     /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
6 //             \/                  \/          \//_____/            \/
7 //  ______________________                           ______________________
8 //                        T H E   W A R   B E G I N S
9 //         Stratagus - A free fantasy real time strategy game engine
10 //
11 /**@name netdriver.h - Net driver header. */
12 //
13 //      (c) Copyright 2005 by Edward Haase and Jimmy Salmon
14 //
15 //      This program is free software; you can redistribute it and/or modify
16 //      it under the terms of the GNU General Public License as published by
17 //      the Free Software Foundation; only version 2 of the License.
18 //
19 //      This program is distributed in the hope that it will be useful,
20 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
21 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 //      GNU General Public License for more details.
23 //
24 //      You should have received a copy of the GNU General Public License
25 //      along with this program; if not, write to the Free Software
26 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 //      02111-1307, USA.
28 //
29 
30 #ifndef __NETDRIVER_H__
31 #define __NETDRIVER_H__
32 
33 //@{
34 
35 /*----------------------------------------------------------------------------
36 --  Includes
37 ----------------------------------------------------------------------------*/
38 
39 #include <time.h>
40 #include "net_lowlevel.h"
41 
42 /*----------------------------------------------------------------------------
43 --  Defines
44 ----------------------------------------------------------------------------*/
45 
46 #define DEFAULT_PORT			7775			// Server port
47 #define DEFAULT_MAX_CONN		500			// Max Connections
48 #define DEFAULT_SESSION_TIMEOUT		900			// 15 miniutes
49 #define DEFAULT_POLLING_DELAY		250			// MS (1000 = 1s)
50 
51 #define MAX_USERNAME_LENGTH 32
52 #define MAX_PASSWORD_LENGTH 32
53 
54 #define MAX_GAMENAME_LENGTH 32
55 #define MAX_VERSION_LENGTH 8
56 
57 /*----------------------------------------------------------------------------
58 --  Declarations
59 ----------------------------------------------------------------------------*/
60 
61 class GameData;
62 
63 /**
64 ** Global server variables.
65 */
66 class ServerStruct {
67 public:
ServerStruct()68 	ServerStruct() : Port(0), MaxConnections(0), IdleTimeout(0),
69 		PollingDelay(0) {}
70 
71 	int Port;
72  	int MaxConnections;
73 	int IdleTimeout;
74 	int PollingDelay;
75 };
76 
77 extern ServerStruct Server;
78 
79 /**
80 **  Session data
81 **
82 **  One per connection.
83 */
84 class Session {
85 public:
Session()86 	Session() : Next(NULL), Prev(NULL), Idle(0), Sock(0), Game(NULL)
87 	{
88 		Buffer[0] = '\0';
89 		AddrData.Host = 0;
90 		AddrData.IPStr[0] = '\0';
91 		AddrData.Port = 0;
92 		UserData.Name[0] = '\0';
93 		UserData.GameName[0] = '\0';
94 		UserData.Version[0] = '\0';
95 		UserData.LoggedIn = 0;
96 	}
97 
98 	Session *Next;
99 	Session *Prev;
100 
101 	char Buffer[1024];
102 	time_t Idle;
103 
104 	Socket Sock;
105 
106 	struct {
107 		unsigned long Host;
108 		char IPStr[16];
109 		int Port;
110 	} AddrData;               /// Remote address data.
111 
112 	struct {
113 		char Name[MAX_USERNAME_LENGTH + 1];
114 		char GameName[MAX_GAMENAME_LENGTH + 1];
115 		char Version[MAX_VERSION_LENGTH + 1];
116 		int LoggedIn;
117 	} UserData;               /// Specific user data.
118 
119 	GameData *Game;
120 };
121 
122 /**
123 **  Global session tracking.
124 */
125 class SessionPool {
126 public:
SessionPool()127 	SessionPool() : First(NULL), Last(NULL), Count(0), Sockets(NULL) {}
128 
129 	Session *First;
130 	Session *Last;
131 	int Count;
132 
133 	SocketSet *Sockets;
134 };
135 
136 	/// external reference to session tracking.
137 extern SessionPool *Pool;
138 
139 /*----------------------------------------------------------------------------
140 --  Functions
141 ----------------------------------------------------------------------------*/
142 
143 extern void Send(Session *session, const char *msg);
144 
145 extern int ServerInit(int port);
146 extern void ServerQuit(void);
147 extern int UpdateSessions(void);
148 
149 //@}
150 
151 #endif // __NETDRIVER_H__
152