StevEngine
StevEngine is a customizable C++ game engine.
Loading...
Searching...
No Matches
networking.hpp
1#pragma once
2#ifdef StevEngine_NETWORKING
3#include "utilities/Stream.hpp"
4#include <cstdint>
5
6#ifdef _WIN32
7#include <Ws2tcpip.h>
8#include <winsock2.h>
9#pragma comment(lib, "ws2_32.lib") //Winsock Library
10#else
11#include <arpa/inet.h>
12#include <netinet/in.h>
13#include <sys/socket.h>
14#include <unistd.h>
15#endif
16
20#ifndef TIMEOUT_PING
21#define TIMEOUT_PING 2
22#endif
23
24namespace StevEngine::Networking {
33 typedef uint32_t MessageID;
34
35#define AVAILABLE_MESSAGE_ID 5
36
40 typedef Utilities::Stream MessageData;
41
45 struct Message {
50 Message(MessageID id);
56 Message(MessageID id, MessageData data);
57
58 const MessageID id; //< id of the message
59 MessageData data; //< data contents of the message
60 };
61
65 void initWinSock();
66
70#ifdef _WIN32
71 extern WSADATA wsa;
72 typedef SOCKET Socket;
73#define MSG_NOSIGNAL 0
74#else
75 typedef int Socket;
76#endif
77
83 Message readReliableMessage(Socket connection);
87 bool sendReliableMessage(Socket connection, Message message);
93 Message readUnreliableMessage(Socket connection, sockaddr_in* address);
97 bool sendUnreliableMessage(Socket connection, const sockaddr_in* address, Message message);
101 void closeSocket(Socket connection);
102}
103#endif