File Network.h

File List > engine > include > Network.h

Go to the documentation of this file

#ifndef NETWORK_H
#define NETWORK_H

#ifdef __cplusplus
extern "C" {
#endif

#include "ThreadSafeQueue.h"
#include "SharedPtr.h"
#include "IntTypes.h"
#include <stdlib.h>

enum MatchmakingProtocolMessageType
{
    MM_ClientSeekServer,

    MM_ServerSeekClient,

    MM_ServerPeerJoined,

    MM_GameConnection,
};


struct PlayerInfo
{
    char username[64];
};

struct ClientMatchmakingInfo
{
    struct PlayerInfo playerInfo;
};

// ip numbers = (4 * groups of 3 + 3 dots) + ':' + 5 numbers for port + null terminator
#define IP_AND_PORT_BUF_SIZE (15 + 3 + 1 + 5 + 1) 

struct ServerMatchmakingInfo
{
    struct PlayerInfo playerInfo;
    int availableSlots;
};

struct PeerAddress
{
    char username[64];
    char address[IP_AND_PORT_BUF_SIZE];
};

struct MatchMakingMessage
{
    enum MatchmakingProtocolMessageType type;
    union
    {
        struct ClientMatchmakingInfo clientInfo;
        struct ServerMatchmakingInfo serverInfo;
        struct PeerAddress peer;
    }data;
};


struct NetworkQueueItem
{
    int client;

    SHARED_PTR(void) pData; 
    int pDataSize;
    bool bReliable;
    u64 sequenceNumber;
};

enum NetworkConnectionEventType
{
    NCE_ClientConnected,
    NCE_ClientDisconnected,
};

struct NetworkConnectionEvent
{
    enum NetworkConnectionEventType type;
    int client;
};

struct HostInfo
{
    const char* ip;
    unsigned short port;
};

enum GameRole
{
    GR_Singleplayer,
    GR_Client,
    GR_ClientServer
};

void NW_Init();

bool NW_DequeueData(struct NetworkQueueItem* pOut);

bool NW_DequeueConnectionEvent(struct NetworkConnectionEvent* pOut);

void NW_EnqueueData(struct NetworkQueueItem* pIn);

enum GameRole NW_GetRole();

#ifdef __cplusplus
}
#endif

#endif