-
Notifications
You must be signed in to change notification settings - Fork 8
/
proxy.h
101 lines (93 loc) · 3.83 KB
/
proxy.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#pragma once
#ifndef PROXY_H
#define PROXY_H
#include "json/CJsonObject.h"
#include "rbslib/Function.h"
#include "rbslib/Network.h"
#include "rbslib/TaskPool.h"
#include <atomic>
#include <exception>
#include <list>
#include <map>
#include <memory>
#include <shared_mutex>
class ProxyException : public std::exception {
private:
std::string message;
public:
ProxyException(const std::string& message) noexcept;
const char* what() const noexcept override;
};
struct User {
std::string username;
std::string uuid;
std::string ip;
std::atomic_uint64_t upload_bytes = 0;
std::time_t connect_time;
RbsLib::Network::TCP::TCPConnection client, server;
User(const RbsLib::Network::TCP::TCPConnection& client, const RbsLib::Network::TCP::TCPConnection& server);
};
/*描述用户信息,不包括连接信息等,用于返回*/
struct UserInfo {
/**
* @brief 用户信息结构体
*
* 用于描述用户的基本信息,包括用户名、UUID、IP地址、上传字节数和连接时间。
*/
std::string username; /**< 用户名 */
std::string uuid; /**< UUID */
std::string ip; /**< IP地址 */
std::uint64_t upload_bytes; /**< 上传及下载字节数 */
std::time_t connect_time; /**< 连接时间 */
};
struct Motd {
neb::CJsonObject motd_json;
Motd& operator=(const Motd&) = delete;
void SetVersion(const std::string& version_name, int protocol);
void SetPlayerMaxNumber(int n);
void SetOnlinePlayerNumber(int n);
void SetSampleUsers(std::list<UserInfo> const& users);
auto ToString() -> std::string;
static auto LoadMotdFromFile(const std::string& path) -> std::string;
};
class Proxy {
public:
class CallbackException : public std::exception {
//定义用于在回调中抛出的异常,该异常用于指示回调函数返回状态
protected:
std::string message;
public:
CallbackException(const std::string& message) noexcept;
const char* what() const noexcept override;
};
//以下为回调函数,均不保证线程安全,需要注意线程安全问题
RbsLib::Function::Function<void(const RbsLib::Network::TCP::TCPConnection& client)> on_connected;//发生在连接成功并将连接加入连接池后,抛出任何异常将导致连接关闭
RbsLib::Function::Function<void(const RbsLib::Network::TCP::TCPConnection& client)> on_disconnect;//发生在即将与客户端断开连接前,此时连接已不可用,不允许在连接上收发数据,仅用于标识连接。在连接上收发将导致异常。抛出异常无效
RbsLib::Function::Function<void(const RbsLib::Network::TCP::TCPConnection& client, const std::string& username, const std::string& uuid)> on_login;//发生在收到客户端的登录数据包后,用户未加入在线用户列表,login抛出的异常将会显示在客户端
RbsLib::Function::Function<void(const RbsLib::Network::TCP::TCPConnection& client, const UserInfo& userinfo)> on_logout;//发生在用户即将断开连接之前,用户已从在线用户列表中移除并且连接未从连接池断开,logout要求与disconnect要求一致,且保证logout先于disconnect。logout抛出异常将导致未定义行为
RbsLib::Function::Function<void(const std::exception& ex)> exception_handle;//用于输出错误日志,error_message_callback抛出的异常将导致未定义行为
Proxy(bool is_ipv6_local, const std::string& local_address, std::uint16_t local_port, bool is_ipv6_remote, const std::string& remote_server_addr, std::uint16_t);
Proxy(const Proxy&) = delete;
Proxy& operator=(const Proxy&) = delete;
void Start();
void KickByUsername(const std::string& username);
void KickByUUID(const std::string& uuid);
auto GetUsersInfo() -> std::list<UserInfo>;
void SetMotd(const std::string& motd);
void SetMaxPlayer(int n);
auto PingTest()const->std::uint64_t;
~Proxy() noexcept;
protected:
int max_player = -1;
std::shared_mutex motd_mutex;
Motd motd;
std::list<RbsLib::Network::TCP::TCPConnection> connections;
std::map<std::string, std::shared_ptr<User>> users;
RbsLib::Network::TCP::TCPServer local_server;
std::string remote_server_addr;
std::uint16_t remote_server_port;
bool is_ipv6_remote;
std::shared_mutex global_mutex;
RbsLib::Thread::TaskPool thread_pool = 11;
};
#endif // !PROXY_H