1 /*-------------------------------------------------------------------------------
2 
3 	BARONY
4 	File: net.hpp
5 	Desc: prototypes and definitions for net.cpp
6 
7 	Copyright 2013-2016 (c) Turning Wheel LLC, all rights reserved.
8 	See LICENSE for details.
9 
10 -------------------------------------------------------------------------------*/
11 
12 #pragma once
13 
14 #include <queue>
15 
16 #define DEFAULT_PORT 57165
17 #define LOBBY_CHATBOX_LENGTH 62
18 #define PACKET_LIMIT 200
19 
20 extern char lobbyChatbox[LOBBY_CHATBOX_LENGTH];
21 extern list_t lobbyChatboxMessages;
22 
23 // function prototypes for net.c:
24 int power(int a, int b);
25 int sendPacket(UDPsocket sock, int channel, UDPpacket* packet, int hostnum, bool tryReliable = false);
26 int sendPacketSafe(UDPsocket sock, int channel, UDPpacket* packet, int hostnum);
27 void messagePlayer(int player, char const * const message, ...);
28 void messagePlayerColor(int player, Uint32 color, char const * const message, ...);
29 void sendEntityUDP(Entity* entity, int c, bool guarantee);
30 void sendEntityTCP(Entity* entity, int c);
31 void sendMapSeedTCP(int c);
32 void sendMapTCP(int c);
33 void serverUpdateEntitySprite(Entity* entity);
34 void serverUpdateEntitySkill(Entity* entity, int skill);
35 void serverUpdateEntityFSkill(Entity* entity, int fskill);
36 void serverUpdateEntityStatFlag(Entity* entity, int flag);
37 void serverSpawnMiscParticles(Entity* entity, int particleType, int particleSprite, Uint32 optionalUid = 0);
38 void serverSpawnMiscParticlesAtLocation(Sint16 x, Sint16 y, Sint16 z, int particleType, int particleSprite);
39 void serverUpdateEntityFlag(Entity* entity, int flag);
40 void serverUpdateBodypartIDs(Entity* entity);
41 void serverUpdateEntityBodypart(Entity* entity, int bodypart);
42 void serverUpdateEffects(int player);
43 void serverUpdateHunger(int player);
44 void serverUpdatePlayerStats();
45 void serverUpdatePlayerGameplayStats(int player, int gameplayStat, int changeval);
46 void serverUpdatePlayerConduct(int player, int conduct, int value);
47 void serverUpdatePlayerLVL();
48 void serverRemoveClientFollower(int player, Uint32 uidToRemove);
49 void serverSendItemToPickupAndEquip(int player, Item* item);
50 void serverUpdateAllyStat(int player, Uint32 uidToUpdate, int LVL, int HP, int MAXHP, int type);
51 void serverUpdatePlayerSummonStrength(int player);
52 void serverUpdateAllyHP(int player, Uint32 uidToUpdate, int HP, int MAXHP, bool guarantee = false);
53 void sendMinimapPing(Uint8 player, Uint8 x, Uint8 y);
54 void sendAllyCommandClient(int player, Uint32 uid, int command, Uint8 x, Uint8 y, Uint32 targetUid = 0);
55 enum NetworkingLobbyJoinRequestResult : int
56 {
57 	NET_LOBBY_JOIN_P2P_FAILURE,
58 	NET_LOBBY_JOIN_P2P_SUCCESS,
59 	NET_LOBBY_JOIN_DIRECTIP_FAILURE,
60 	NET_LOBBY_JOIN_DIRECTIP_SUCCESS
61 };
62 NetworkingLobbyJoinRequestResult lobbyPlayerJoinRequest(int& outResult);
63 Entity* receiveEntity(Entity* entity);
64 void clientActions(Entity* entity);
65 void clientHandleMessages(Uint32 framerateBreakInterval);
66 void serverHandleMessages(Uint32 framerateBreakInterval);
67 bool handleSafePacket();
68 
69 void closeNetworkInterfaces();
70 
71 // server/game flags
72 extern Uint32 svFlags;
73 extern Uint32 settings_svFlags;
74 const Uint32 NUM_SERVER_FLAGS =  9;
75 const Uint32 SV_FLAG_CHEATS  = 1;
76 const Uint32 SV_FLAG_FRIENDLYFIRE = 2;
77 const Uint32 SV_FLAG_MINOTAURS = 4;
78 const Uint32 SV_FLAG_HUNGER  = 8;
79 const Uint32 SV_FLAG_TRAPS = 16;
80 const Uint32 SV_FLAG_HARDCORE = 32;
81 const Uint32 SV_FLAG_CLASSIC = 64;
82 const Uint32 SV_FLAG_KEEPINVENTORY = 128;
83 const Uint32 SV_FLAG_LIFESAVING = 256;
84 
85 class SteamPacketWrapper
86 {
87 	Uint8* _data;
88 	int _len;
89 	//TODO: Encapsulate CSteam ID?
90 public:
91 	SteamPacketWrapper(Uint8* data, int len);
92 	~SteamPacketWrapper(); //NOTE: DOES free _data. Don't keep it somewhere else or segfaults will ensue. If you're lucky.
93 
94 	Uint8*& data();
95 	int& len();
96 };
97 
98 class NetHandler
99 {
100 	SDL_Thread* steam_packet_thread;
101 	bool continue_multithreading_steam_packets;
102 	SDL_mutex* game_packets_lock;
103 public:
104 	NetHandler();
105 	~NetHandler();
106 	std::queue<SteamPacketWrapper* > game_packets;
107 
108 	void initializeMultithreadedPacketHandling();
109 	void stopMultithreadedPacketHandling();
110 	void toggleMultithreading(bool disableMultithreading);
111 
112 	bool getContinueMultithreadingSteamPackets();
113 
114 	void addGamePacket(SteamPacketWrapper* packet);
115 
116 	/*
117 	 * This function will take the next packet in the queue, pop it off, and then return it.
118 	 * Returns nullptr if no packets.
119 	 * NOTE: You *MUST* free the data returned by this, or else you will leak memory! Such is the way of things.
120 	 */
121 	SteamPacketWrapper* getGamePacket();
122 
123 	SDL_mutex* continue_multithreading_steam_packets_lock;
124 };
125 extern NetHandler* net_handler;
126 
127 extern bool disableMultithreadedSteamNetworking;
128 extern bool disableFPSLimitOnNetworkMessages;
129 
130 int steamPacketThread(void* data);
131 int EOSPacketThread(void* data);
132 
133 void deleteMultiplayerSaveGames(); //Server function, deletes its own save and broadcasts delete packet to clients.
134