-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbflib_netsp_tcp.cpp
260 lines (198 loc) · 6.08 KB
/
bflib_netsp_tcp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/******************************************************************************/
// Bullfrog Engine Emulation Library - for use to remake classic games like
// Syndicate Wars, Magic Carpet or Dungeon Keeper.
/******************************************************************************/
/** @file bflib_netsp_tcp.cpp
* TCP network ServiceProvider subclass declaration.
* @par Purpose:
* Defines ServiceProvider for TCP network with UDP LAN discovery.
* @par Comment:
* None.
* @author KeeperFX Team
* @date 10 April 2010 - ?
* @par Copying and copyrights:
* This program 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; either version 2 of the License, or
* (at your option) any later version.
*/
/******************************************************************************/
#include "bflib_netsp_tcp.hpp"
#include <cassert>
#include "bflib_netconfig.hpp"
#include "bflib_network.h"
#include "bflib_netsession.h"
#include "bflib_nethost_udp.hpp"
#include "bflib_netlisten_udp.hpp"
#include "bflib_client_tcp.hpp"
#include "bflib_server_tcp.hpp"
TCPServiceProvider::TCPServiceProvider() :
maxPlayers(1),
isServer(false),
joinable(false),
host(NULL),
listener(NULL),
base(NULL)
{
}
TCPServiceProvider::~TCPServiceProvider()
{
}
TbError TCPServiceProvider::Start(struct TbNetworkSessionNameEntry * sessionName, char * playerName, void * options)
{
SYNCDBG(7, "Starting");
assert(!started);
isServer = false;
ClearPlayers();
base = new TCP_NetClient("localhost", HOST_PORT_NUMBER);
if (base == NULL || base->hadError()) {
delete base;
base = NULL;
return Lb_FAIL;
}
//TODO NET see if we're responsible for adding ourself to player list or not
localPlayerId = 0; //for now...
started = true;
return Lb_OK;
}
TbError TCPServiceProvider::Start(char * sessionName, char * playerName, unsigned long maxPlayers, void * options)
{
SYNCDBG(7, "Starting");
assert(!started);
assert(maxPlayers >= 2);
assert(maxPlayers <= NETSP_PLAYERS_COUNT);
isServer = true;
ClearPlayers();
joinable = true;
this->maxPlayers = maxPlayers;
base = new TCP_NetServer(HOST_PORT_NUMBER);
if (base == NULL || base->hadError()) {
delete base;
base = NULL;
return Lb_FAIL;
}
// Fill in player info.
localPlayerId = 1;
AddPlayer(localPlayerId++, playerName, 0, 0);
//TODO NET make broadcast addresses configurable
std::vector<std::string> broadcastAddr;
broadcastAddr.push_back("255.255.255.255"); //global LAN broadcast addr
broadcastAddr.push_back("192.168.0.255"); //common LAN broadcast addr
broadcastAddr.push_back("5.255.255.255"); //Hamachi LAN
host = new UDP_NetHost(broadcastAddr);
/*NETMSG("Starting new session with name %s", sessionName);
TbNetworkSessionNameEntry * const session = AddSession(static_cast<unsigned long>(-1), sessionName);*/
started = true;
return Lb_OK;
}
TbError TCPServiceProvider::Stop(void)
{
SYNCDBG(7, "Starting");
/*int index = SessionIndex(local_id);
if (index >= 0) {
nsnames[index].in_use = false;
}*/
localPlayerId = 0;
delete host;
host = NULL;
delete base;
base = NULL;
started = false;
return Lb_OK;
}
TbError TCPServiceProvider::Enumerate(TbNetworkCallbackFunc sessionCallback, void * ptr)
{
SYNCDBG(7, "Starting");
// Check sessions list.
/*SDL_LockMutex(sessionsMutex);
for (int i = 0; i < SESSION_ENTRIES_COUNT; ++i) {
TbNetworkSessionNameEntry & session = nsnames[i];
if (session.in_use && session.id != local_id) {
sessionCallback(reinterpret_cast<TbNetworkCallbackData *>(&session), ptr);
}
}
SDL_UnlockMutex(sessionsMutex);*/
//for now, fake 1 session for testing
nsnames[0].in_use = true;
nsnames[0].joinable = true;
net_copy_name_string(nsnames[0].text, "TESTING", SESSION_NAME_MAX_LEN);
sessionCallback(reinterpret_cast<TbNetworkCallbackData *>(&nsnames[0]), ptr);
return Lb_OK;
}
TbError TCPServiceProvider::Enumerate(struct TbNetworkSessionNameEntry * sessionEntry,
TbNetworkCallbackFunc playerCallback, void * ptr) {
SYNCDBG(7, "Starting");
if (sessionEntry == NULL) {
for (int i = 0; i < players_count; ++i) {
TbNetworkPlayerNameEntry entry;
memset(&entry, 0, sizeof(entry));
if (i == 0) {
entry.ishost = isServer; //TODO NET check correctness of this... was written for old code
entry.islocal = true; //I hope
}
memcpy(entry.name, players[i].name, 19);
entry.id = players[i].id;
playerCallback(reinterpret_cast<TbNetworkCallbackData *>(&entry), ptr);
}
}
else {
//TODO NET look up remote session
}
return Lb_OK;
}
TbError TCPServiceProvider::Init(struct ReceiveCallbacks * recCb, void * a4) {
SYNCDBG(7, "Starting");
TbError retval = ServiceProvider::Initialise(recCb, a4);
if (retval != Lb_OK) {
return retval;
}
listener = new UDP_NetListener();
return Lb_OK;
}
TbError TCPServiceProvider::Release(void)
{
SYNCDBG(7, "Starting");
if (started) {
Stop();
}
delete listener;
listener = NULL;
return ServiceProvider::Release();
}
TbError TCPServiceProvider::ChangeSettings(unsigned long, void *) {
return Lb_FAIL;
}
TbError TCPServiceProvider::EnableNewPlayers(TbBool allow) {
joinable = allow;
return Lb_OK;
}
bool TCPServiceProvider::ReadMessage(ulong * playerId, void * msg, ulong * len) {
assert(started);
size_t len2 = *len;
bool retval = base->fetchDKMessage(*playerId, reinterpret_cast<char *>(msg), len2, false);
*len = len2;
return retval;
}
bool TCPServiceProvider::PeekMessage(ulong * playerId, void * msg, ulong * len) {
assert(started);
size_t len2 = *len;
bool retval = base->fetchDKMessage(*playerId, reinterpret_cast<char *>(msg), len2, true);
*len = len2;
return retval;
}
TbError TCPServiceProvider::SendMessage(ulong playerId, void * msg, uchar i) {
assert(started);
ulong totalMsgLen;
ServiceProvider::DecodeMessageStub(msg, &totalMsgLen, NULL, NULL);
totalMsgLen += 4; //include header
if (!base->sendDKMessage(playerId, reinterpret_cast<char *>(msg), totalMsgLen)) {
return Lb_FAIL;
}
return Lb_OK;
}
void TCPServiceProvider::update()
{
if (started) {
base->update();
}
}