mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-08 04:21:50 +00:00
refactor Network.cpp
This commit is contained in:
parent
3c0ba4a8c4
commit
2f15a1a8d4
6 changed files with 1055 additions and 943 deletions
|
|
@ -1,75 +1,8 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
|
||||
#include "typedefs.hpp"
|
||||
#include "settings.hpp"
|
||||
#include "util/Buffer.hpp"
|
||||
#include "delegates.hpp"
|
||||
#include "commons.hpp"
|
||||
|
||||
namespace network {
|
||||
using OnResponse = std::function<void(std::vector<char>)>;
|
||||
using OnReject = std::function<void(int, std::vector<char>)>;
|
||||
using ConnectCallback = std::function<void(u64id_t, u64id_t)>;
|
||||
using ServerDatagramCallback = std::function<void(u64id_t sid, const std::string& addr, int port, const char* buffer, size_t length)>;
|
||||
using ClientDatagramCallback = std::function<void(u64id_t cid, const char* buffer, size_t length)>;
|
||||
|
||||
class Requests {
|
||||
public:
|
||||
virtual ~Requests() {}
|
||||
|
||||
virtual void get(
|
||||
const std::string& url,
|
||||
OnResponse onResponse,
|
||||
OnReject onReject=nullptr,
|
||||
std::vector<std::string> headers = {},
|
||||
long maxSize=0
|
||||
) = 0;
|
||||
|
||||
virtual void post(
|
||||
const std::string& url,
|
||||
const std::string& data,
|
||||
OnResponse onResponse,
|
||||
OnReject onReject=nullptr,
|
||||
std::vector<std::string> headers = {},
|
||||
long maxSize=0
|
||||
) = 0;
|
||||
|
||||
[[nodiscard]] virtual size_t getTotalUpload() const = 0;
|
||||
[[nodiscard]] virtual size_t getTotalDownload() const = 0;
|
||||
|
||||
virtual void update() = 0;
|
||||
};
|
||||
|
||||
enum class ConnectionState {
|
||||
INITIAL, CONNECTING, CONNECTED, CLOSED
|
||||
};
|
||||
|
||||
enum class TransportType {
|
||||
TCP, UDP
|
||||
};
|
||||
|
||||
class Connection {
|
||||
public:
|
||||
virtual ~Connection() = default;
|
||||
|
||||
virtual void close(bool discardAll=false) = 0;
|
||||
|
||||
virtual int send(const char* buffer, size_t length) = 0;
|
||||
|
||||
virtual size_t pullUpload() = 0;
|
||||
virtual size_t pullDownload() = 0;
|
||||
|
||||
[[nodiscard]] virtual int getPort() const = 0;
|
||||
[[nodiscard]] virtual std::string getAddress() const = 0;
|
||||
|
||||
[[nodiscard]] virtual ConnectionState getState() const = 0;
|
||||
|
||||
[[nodiscard]] virtual TransportType getTransportType() const noexcept = 0;
|
||||
};
|
||||
|
||||
class TcpConnection : public Connection {
|
||||
public:
|
||||
~TcpConnection() override = default;
|
||||
|
|
@ -96,15 +29,6 @@ namespace network {
|
|||
}
|
||||
};
|
||||
|
||||
class Server {
|
||||
public:
|
||||
virtual ~Server() = default;
|
||||
virtual void close() = 0;
|
||||
virtual bool isOpen() = 0;
|
||||
[[nodiscard]] virtual TransportType getTransportType() const noexcept = 0;
|
||||
[[nodiscard]] virtual int getPort() const = 0;
|
||||
};
|
||||
|
||||
class TcpServer : public Server {
|
||||
public:
|
||||
~TcpServer() override {}
|
||||
|
|
@ -160,8 +84,8 @@ namespace network {
|
|||
long maxSize=0
|
||||
);
|
||||
|
||||
[[nodiscard]] Connection* getConnection(u64id_t id);
|
||||
[[nodiscard]] Server* getServer(u64id_t id) const;
|
||||
[[nodiscard]] Connection* getConnection(u64id_t id, bool includePrivate);
|
||||
[[nodiscard]] Server* getServer(u64id_t id, bool includePrivate) const;
|
||||
|
||||
u64id_t connectTcp(const std::string& address, int port, consumer<u64id_t> callback);
|
||||
u64id_t connectUdp(const std::string& address, int port, const consumer<u64id_t>& callback, ClientDatagramCallback handler);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue