Program Listing for File socket.h

Return to documentation for file (blam\components\networking\socket.h)

#pragma once

#include <winsock.h>

#include "messages.h"

#ifndef BLAM
#define BLAM
#endif

namespace Blam
{
    struct Endpoint
    {
        UINT32 address;
        UINT16 port;
    };

    struct Socket
    {
        SOCKET handle;
    };

    struct LinearAllocator
    {
        UINT8* memory;
        UINT8* next;
        UINT64 bytesRemaining;
    };

    BLAM void     linearAllocCreate(Blam::LinearAllocator* alloc, UINT64 size);
    BLAM UINT8*   linearAlloc(Blam::LinearAllocator* alloc, UINT64 size);

    namespace Network
    {
        struct PacketBuffer
        {
            UINT32 index;
            UINT32 size;
            UINT8* packets;
            UINT32* packetSizes;
            Endpoint* endpoints;
            LARGE_INTEGER* times;
        };

        struct Socket
        {
            Blam::Socket sock;
            PacketBuffer sendBuf;
            PacketBuffer recvBuf;
            bool receive;
        };

        BLAM bool Init();
        BLAM bool Start(Socket* socket, LinearAllocator* allocator);
        BLAM void Close(Socket* socket);
        BLAM bool Bind(Socket* socket, Endpoint* endpoint);
        BLAM bool Send(Socket* socket, UINT8* packet, UINT32 packetSize, Endpoint* endpoint);
        BLAM bool Receive(Socket* socket, UINT8* buffer, UINT32 bufferSize, UINT32* packetSizeOut, Endpoint* endpoint);

        BLAM bool IsConnected();

        BLAM void HandleReceive(Socket* sock, UINT8* socketBuffer, UINT32 bufferSize, UINT32* outPacketSize, Endpoint* outFrom);
    }
}