1 /*
2  *  network_star.h
3 
4 	Copyright (C) 2003 and beyond by Woody Zenfell, III
5 	and the "Aleph One" developers.
6 
7 	This program is free software; you can redistribute it and/or modify
8 	it under the terms of the GNU General Public License as published by
9 	the Free Software Foundation; either version 3 of the License, or
10 	(at your option) any later version.
11 
12 	This program is distributed in the hope that it will be useful,
13 	but WITHOUT ANY WARRANTY; without even the implied warranty of
14 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 	GNU General Public License for more details.
16 
17 	This license is contained in the file "COPYING",
18 	which is included with this source code; it is available online at
19 	http://www.gnu.org/licenses/gpl.html
20 
21  *  Created by Woody Zenfell, III on Mon Apr 28 2003.
22  */
23 
24 #ifndef NETWORK_STAR_H
25 #define NETWORK_STAR_H
26 
27 #include "TickBasedCircularQueue.h"
28 #include "ActionQueues.h"
29 
30 #include "sdl_network.h"
31 
32 // We avoid including half the world just to get TICKS_PER_SECOND for standalone hub...
33 #ifndef A1_NETWORK_STANDALONE_HUB
34 # include "map.h" // TICKS_PER_SECOND
35 #else
36 enum {
37 	TICKS_PER_SECOND = 30
38 };
39 #endif
40 
41 #include <stdio.h>
42 
43 enum {
44         kEndOfMessagesMessageType = 0x454d,	// 'EM'
45         kTimingAdjustmentMessageType = 0x5441,	// 'TA'
46         kPlayerNetDeadMessageType = 0x4e44,	// 'ND'
47 	kSpokeToHubLossyByteStreamMessageType = 0x534c,	// 'SL'
48 	kHubToSpokeLossyByteStreamMessageType = 0x484c, // 'HL'
49 
50 	kSpokeToHubIdentification = 0x4944,   // 'ID'
51 	kSpokeToHubGameDataPacketV1Magic = 0x5331, // 'S1'
52 	kHubToSpokeGameDataPacketV1Magic = 0x4831, // 'H1'
53 	kHubToSpokeGameDataPacketWithSpokeFlagsV1Magic = 0x4631, // 'F1'
54 	kPingRequestPacket = 0x5051, // 'PQ'
55 	kPingResponsePacket = 0x5052, // 'PR'
56 
57         kPregameTicks = TICKS_PER_SECOND * 3,	// Synchronization/timing adjustment before real data
58         kActionFlagsSerializedLength = 4,	// bytes for each serialized action_flags_t (should be elsewhere)
59 
60 	kStarPacketHeaderSize = 4, // 2 bytes for packet magic, 2 for CRC
61 };
62 
63 typedef uint32 action_flags_t;	// (should be elsewhere)
64 typedef ConcreteTickBasedCircularQueue<action_flags_t> TickBasedActionQueue;
65 typedef WritableTickBasedCircularQueue<action_flags_t> WritableTickBasedActionQueue;
66 
67 
68 class InfoTree;
69 
70 extern void hub_initialize(int32 inStartingTick, size_t inNumPlayers, const NetAddrBlock* const* inPlayerAddresses, size_t inLocalPlayerIndex);
71 extern void hub_cleanup(bool inGraceful, int32 inSmallestPostGameTick);
72 extern void hub_received_network_packet(DDPPacketBufferPtr inPacket);
73 extern void DefaultHubPreferences();
74 extern InfoTree HubPreferencesTree();
75 extern void HubParsePreferencesTree(InfoTree prefs, std::string version);
76 
77 extern void spoke_initialize(const NetAddrBlock& inHubAddress, int32 inFirstTick, size_t inNumberOfPlayers, WritableTickBasedActionQueue* const inPlayerQueues[], bool inPlayerConnectedStatus[], size_t inLocalPlayerIndex, bool inHubIsLocal);
78 extern void spoke_cleanup(bool inGraceful);
79 extern void spoke_received_network_packet(DDPPacketBufferPtr inPacket);
80 extern int32 spoke_get_net_time();
81 // "Distribute to everyone" helps to match the existing (legacy) interfaces etc.
82 extern void spoke_distribute_lossy_streaming_bytes_to_everyone(int16 inDistributionType, byte* inBytes, uint16 inLength, bool inExcludeLocalPlayer, bool onlySendToTeam);
83 // distribute_lossy_streaming_bytes offers a more direct interface (not yet used) to star's lossy
84 //	distribution mechanism.  (e.g., can select certain recipients, send unregistered dist types, etc.)
85 extern void spoke_distribute_lossy_streaming_bytes(int16 inDistributionType, uint32 inDestinationsBitmask, byte* inBytes, uint16 inLength);
86 extern int32 spoke_latency(); // in ms, kNetLatencyInvalid if not yet valid
87 extern int32 hub_latency(int player_index); // in ms, kNetLatencyInvalid if not valid, kNetLatencyDisconnected if d/c
88 extern TickBasedActionQueue* spoke_get_unconfirmed_flags_queue();
89 extern int32 spoke_get_smallest_unconfirmed_tick();
90 extern void DefaultSpokePreferences();
91 extern InfoTree SpokePreferencesTree();
92 extern void SpokeParsePreferencesTree(InfoTree prefs, std::string version);
93 
94 #endif // NETWORK_STAR_H
95