1 /*
2     C-Dogs SDL
3     A port of the legendary (and fun) action/arcade cdogs.
4 
5     Copyright (c) 2014-2016, Cong Xu
6     All rights reserved.
7 
8     Redistribution and use in source and binary forms, with or without
9     modification, are permitted provided that the following conditions are met:
10 
11     Redistributions of source code must retain the above copyright notice, this
12     list of conditions and the following disclaimer.
13     Redistributions in binary form must reproduce the above copyright notice,
14     this list of conditions and the following disclaimer in the documentation
15     and/or other materials provided with the distribution.
16 
17     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27     POSSIBILITY OF SUCH DAMAGE.
28 */
29 #pragma once
30 
31 #include <time.h>
32 
33 #include "net_util.h"
34 
35 // Stored information about game servers scanned
36 typedef struct
37 {
38 	NServerInfo ServerInfo;
39 	ENetAddress Addr;
40 	int LatencyMS;
41 } ScanInfo;
42 
43 typedef struct
44 {
45 	ENetHost *client;
46 	ENetPeer *peer;
47 	int ClientId;
48 	int FirstPlayerUID;
49 	bool Ready;
50 	// Socket used to scan for LAN servers
51 	ENetSocket scanner;
52 	// Only scan for a period; if > 0 then we are scanning
53 	int ScanTicks;
54 	// Addresses of scanned LAN servers
55 	CArray ScannedAddrs;		// of ScanInfo
56 	// Buffer of scanned addresses - new ones will be scanned here
57 	CArray scannedAddrBuf;	// of ScanInfo
58 } NetClient;
59 
60 extern NetClient gNetClient;
61 
62 void NetClientInit(NetClient *n);
63 void NetClientTerminate(NetClient *n);
64 
65 // Start searching for LAN servers; note that the result will be returned in
66 // NetClient's boolean
67 void NetClientFindLANServers(NetClient *n);
68 // Attempt to connect to a server
69 bool NetClientTryConnect(NetClient *n, const ENetAddress addr);
70 // Attempt to scan a host for a game server and connect
71 bool NetClientTryScanAndConnect(NetClient *n, const enet_uint32 host);
72 void NetClientDisconnect(NetClient *n);
73 void NetClientPoll(NetClient *n);
74 void NetClientFlush(NetClient *n);
75 // Send a command to the server
76 void NetClientSendMsg(NetClient *n, const GameEventType e, const void *data);
77 
78 bool NetClientIsConnected(const NetClient *n);
79