NetworkManager.h
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 |
/* Author: Chase Hutchens Date : 7/1/15 */ // All content © 2014-2015 DigiPen (USA) Corporation, all rights reserved. #pragma once // Local Modules #include "exports.h" // Other Engine Modules #include "../Engine/Messages.h" #include "../DebugTools/DebugTimer.h" // Third Party Modules #include <thread> /* std::thread */ #include <queue> /* std::queue */ #include <atomic> /* std::atomic */ #include <memory> /* std::unique_ptr */ #include <mutex> /* std::mutex */ #define PACKET_SIZE 4096 // Used for declaring a particular message #define NETMSG_DECLARATION '!' #define NMD NETMSG_DECLARATION // Used for separating specific message pieces #define NETMSG_SEPARATOR ':' #define NMS NETMSG_SEPARATOR // Used for separating your data pieces within specific message pieces #define NETMSG_DATA_SEPARATOR '|' #define NMDS NETMSG_DATA_SEPARATOR class SocketObject; class NetworkChatService; class ClientMessageLogger; typedef unsigned short u_short; enum class NET_EXPORT MESSAGE_TYPE { Info, // Message Containing Information for the Server to Log Game, // Message Containing Game Information (WIP) Relay, // Message that will be relayed to all other clients Data, // Message that is received when receiving downloaded data }; class NetworkManager : public EventBase { private: bool establishInProgress, activeConnection, allowNewMessages; bool disconnectMsgsCompleted; float timeSincePing, sentKbps, recvKbps; SocketObject* clientSocket; double networkTime; std::string sessionID, pingMsg; std::thread connector, receiver, sender, disconnector; std::mutex toSendLock; std::vector<std::string> receivedInfoMsgs, receivedGameMsgs, receivedRelayMsgs, receivedDataMsgs; std::atomic<std::queue<std::string> > toSendData; DebugTimer m_DBT_NetworkManager; std::unique_ptr<ClientMessageLogger> logger; private: // thread functions void EstablishConnection(float waitTime, float timeoutTime); void ReceivePacket(); void SendPacket(); void DisconnectConnection(); // -- void ExecuteReceivedServerMessages();//(std::string receivedMsg); public: NET_EXPORT static NetworkManager& GetInstance(); NET_EXPORT static void Init(); NET_EXPORT static void Update(); NET_EXPORT static void Free(); NET_EXPORT void Connect(int protocol, const char* ip, u_short port); NET_EXPORT void Disconnect(); NET_EXPORT void OfflineSafeDisconnect(); NET_EXPORT void AddMsgToSend(const std::string& msg); NET_EXPORT bool IsActiveConnection(); NET_EXPORT bool IsEstablishInProgress(); NET_EXPORT DebugTimer* GetNetworkDebugPingTimer(); NET_EXPORT std::string GetServerCmdFromMessageType(MESSAGE_TYPE msgType); NET_EXPORT std::string GetSessionID(); NET_EXPORT double GetNetworkTime(); NET_EXPORT float GetSentKbps(); NET_EXPORT float GetRecvKbps(); }; |