the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 147 lines 3.9 kB view raw
1#pragma once 2 3#ifdef _WINDOWS64 4 5#include <WinSock2.h> 6#include <WS2tcpip.h> 7#include <vector> 8#include "..\..\Common\Network\NetworkPlayerInterface.h" 9 10#pragma comment(lib, "Ws2_32.lib") 11 12#define WIN64_NET_DEFAULT_PORT 25565 13#define WIN64_NET_MAX_CLIENTS 7 14#define WIN64_NET_RECV_BUFFER_SIZE 65536 15#define WIN64_LAN_DISCOVERY_PORT 25566 16#define WIN64_LAN_BROADCAST_MAGIC 0x4D434C4E 17 18class Socket; 19 20#pragma pack(push, 1) 21struct Win64LANBroadcast 22{ 23 DWORD magic; 24 WORD netVersion; 25 WORD gamePort; 26 wchar_t hostName[32]; 27 BYTE playerCount; 28 BYTE maxPlayers; 29 DWORD gameHostSettings; 30 DWORD texturePackParentId; 31 BYTE subTexturePackId; 32 BYTE isJoinable; 33}; 34#pragma pack(pop) 35 36struct Win64LANSession 37{ 38 char hostIP[64]; 39 int hostPort; 40 wchar_t hostName[32]; 41 unsigned short netVersion; 42 unsigned char playerCount; 43 unsigned char maxPlayers; 44 unsigned int gameHostSettings; 45 unsigned int texturePackParentId; 46 unsigned char subTexturePackId; 47 bool isJoinable; 48 DWORD lastSeenTick; 49}; 50 51struct Win64RemoteConnection 52{ 53 SOCKET tcpSocket; 54 BYTE smallId; 55 HANDLE recvThread; 56 volatile bool active; 57}; 58 59class WinsockNetLayer 60{ 61public: 62 static bool Initialize(); 63 static void Shutdown(); 64 65 static bool HostGame(int port); 66 static bool JoinGame(const char *ip, int port); 67 68 static bool SendToSmallId(BYTE targetSmallId, const void *data, int dataSize); 69 static bool SendOnSocket(SOCKET sock, const void *data, int dataSize); 70 71 static bool IsHosting() { return s_isHost; } 72 static bool IsConnected() { return s_connected; } 73 static bool IsActive() { return s_active; } 74 75 static BYTE GetLocalSmallId() { return s_localSmallId; } 76 static BYTE GetHostSmallId() { return s_hostSmallId; } 77 78 static SOCKET GetSocketForSmallId(BYTE smallId); 79 80 static void HandleDataReceived(BYTE fromSmallId, BYTE toSmallId, unsigned char *data, unsigned int dataSize); 81 82 static bool PopDisconnectedSmallId(BYTE *outSmallId); 83 static void PushFreeSmallId(BYTE smallId); 84 85 static bool StartAdvertising(int gamePort, const wchar_t *hostName, unsigned int gameSettings, unsigned int texPackId, unsigned char subTexId, unsigned short netVer); 86 static void StopAdvertising(); 87 static void UpdateAdvertisePlayerCount(BYTE count); 88 static void UpdateAdvertiseJoinable(bool joinable); 89 90 static bool StartDiscovery(); 91 static void StopDiscovery(); 92 static std::vector<Win64LANSession> GetDiscoveredSessions(); 93 94 static int GetHostPort() { return s_hostGamePort; } 95 96private: 97 static DWORD WINAPI AcceptThreadProc(LPVOID param); 98 static DWORD WINAPI RecvThreadProc(LPVOID param); 99 static DWORD WINAPI ClientRecvThreadProc(LPVOID param); 100 static DWORD WINAPI AdvertiseThreadProc(LPVOID param); 101 static DWORD WINAPI DiscoveryThreadProc(LPVOID param); 102 103 static SOCKET s_listenSocket; 104 static SOCKET s_hostConnectionSocket; 105 static HANDLE s_acceptThread; 106 static HANDLE s_clientRecvThread; 107 108 static bool s_isHost; 109 static bool s_connected; 110 static bool s_active; 111 static bool s_initialized; 112 113 static BYTE s_localSmallId; 114 static BYTE s_hostSmallId; 115 static BYTE s_nextSmallId; 116 117 static CRITICAL_SECTION s_sendLock; 118 static CRITICAL_SECTION s_connectionsLock; 119 120 static std::vector<Win64RemoteConnection> s_connections; 121 122 static SOCKET s_advertiseSock; 123 static HANDLE s_advertiseThread; 124 static volatile bool s_advertising; 125 static Win64LANBroadcast s_advertiseData; 126 static CRITICAL_SECTION s_advertiseLock; 127 static int s_hostGamePort; 128 129 static SOCKET s_discoverySock; 130 static HANDLE s_discoveryThread; 131 static volatile bool s_discovering; 132 static CRITICAL_SECTION s_discoveryLock; 133 static std::vector<Win64LANSession> s_discoveredSessions; 134 135 static CRITICAL_SECTION s_disconnectLock; 136 static std::vector<BYTE> s_disconnectedSmallIds; 137 138 static CRITICAL_SECTION s_freeSmallIdLock; 139 static std::vector<BYTE> s_freeSmallIds; 140}; 141 142extern bool g_Win64MultiplayerHost; 143extern bool g_Win64MultiplayerJoin; 144extern int g_Win64MultiplayerPort; 145extern char g_Win64MultiplayerIP[256]; 146 147#endif