Commit 8fe032a0 authored by Filip Hauzvic's avatar Filip Hauzvic
Browse files

Socket interface

parent 9c903ceb
Loading
Loading
Loading
Loading
+49 −0
Original line number Diff line number Diff line
#pragma once NETWORK_TYPES_HPP
#include <cstdint>
#include <span>

typedef uint32_t PeerId;

struct NetworkMessage
{
    PeerId sender;
    std::span<uint8_t> data;
    int length;
    int channel;
};

enum SocketMode
{
    SERVER,
    CLIENT,
    NONE
};

enum SendResult
{
    SUCCESS,           // Only queued, not yet sent
    NOT_CONNECTED,
    MESSAGE_TOO_LARGE,
    BUFFER_FULL,
    PEER_NOT_FOUND
};

enum ReliabilityMode
{
    UNRELIABLE,
    RELIABLE
};

enum OrderingMode
{
    UNORDERED,
    ORDERED
};

enum ConnectionStatus
{
    DISCONNECTED,
    CONNECTING,
    CONNECTED,
    DISCONNECTING
};
 No newline at end of file
+36 −0
Original line number Diff line number Diff line
#pragma once SOCKET_HPP
#include <functional>
#include <string>
#include <net/network_types.hpp>

struct ISocket
{
    virtual ~ISocket() = 0;

    virtual bool start_server(int port, int max_connections) = 0;
    virtual bool start_client(std::string address, int port) = 0;
    virtual void server_disconnect_peer(PeerId peer) = 0;
    virtual void shutdown() = 0;

    virtual ConnectionStatus get_status() = 0;
    virtual SocketMode get_mode() = 0;
    virtual std::vector<PeerId> get_connected_peers() = 0;

    // Messaging
    virtual SendResult send(PeerId peer, std::span<const uint8_t> data, int length,
              ReliabilityMode reliability = UNRELIABLE,
              OrderingMode ordering = UNORDERED,
              int channel = 0) = 0;

    // Read a single message from some buffered queue (built in Update)
    virtual NetworkMessage* receive() = 0;

    std::function<void()> on_connected;
    std::function<void()> on_disconnected;
    std::function<void(PeerId)> on_peer_connected;
    std::function<void(PeerId)> on_peer_disconnected;

    // Update the socket, process incoming, call at least once per frame
    virtual void update() = 0;
    virtual int get_max_message_size() = 0;
};