19#include <magic_enum/magic_enum.hpp>
68 auto msg_id_opt = magic_enum::enum_cast<E>(
id);
69 if (!msg_id_opt.has_value())
74 return msg_id_opt.value();
87 auto msg_id_opt = magic_enum::enum_cast<E>(
id);
88 if (!msg_id_opt.has_value())
93 return msg_id_opt.value();
106 bool Connent(std::string_view ip, uint16_t port);
108 template<IsEnum E, IsPod T>
121 magic_enum::enum_name(msg_id),
125 Send((uint32_t)msg_id, buf);
137 auto msg_id_opt = magic_enum::enum_cast<E>(msg->
_head.
_id);
138 if (!msg_id_opt.has_value())
143 E msg_id = msg_id_opt.value();
151 magic_enum::enum_name(msg_id),
167 std::atomic_bool _bEnd;
168 std::atomic_bool _bValid;
173 static void _thread_send(
void*);
174 static void _thread_recv(
void*);
186 if (_bufListRecv.
PopFront(_msgTemp))
196 log_warn(
"未调用 Connent,或无网络权限,后续调用无效!");
216 template<IsEnum E, IsPod T>
217 void Send(
int sokect_id,
E msg_id,
const T& a)
228 magic_enum::enum_name(msg_id),
232 _send_imp(sokect_id, (uint32_t)msg_id, buf);
235 template<IsEnum E, IsNotPod T>
236 void Send(
int sokect_id,
E msg_id,
const T& a)
238 Buffer buf = a.ToBuffer();
247 magic_enum::enum_name(msg_id),
251 _send_imp(sokect_id, (uint32_t)msg_id, buf);
266 auto msg_id_opt = magic_enum::enum_cast<E>(msg->
_head.
_id);
267 if (!msg_id_opt.has_value())
272 E msg_id = msg_id_opt.value();
280 magic_enum::enum_name(msg_id),
296 static void _thread_run(
void*);
299 void _send_imp(
int sokect_id, uint32_t msg_id,
BufferView buf);
309 void SetAddress(std::string_view ip, uint16_t port,
bool broadcast =
false);
311 void Send(std::string_view str)
318 if (_bufListRecv.PopFront(_bufTemp))
328 std::atomic_bool _bValid;
329 std::atomic_bool _bEnd;
336 static void _thread_send(
void*);
337 static void _thread_recv(
void*);
343 log_warn(
"未调用 SetAddress,或无网络权限,后续调用无效!");
354 bool Start(std::string_view ip, uint16_t port);
358 if (_listMsg.PopFront(_msgTemp))
375 static void _thread_run(
void*);
400 return vec.empty() ?
"127.0.0.1" : vec[0];
405 log_msg(
"----------------网络信息-------------\n"
413extern Network*
g_net;
void Send(E msg_id, const T &a)
std::string GetRemoteAddressString()
返回例如 192.168.1.1:7300
bool Connent(std::string_view ip, uint16_t port)
void Send(BufferView buf)
void Send(std::string_view str)
void SetAddress(std::string_view ip, uint16_t port, bool broadcast=false)
bool PopFront(T &a)
取第一个,顺序通过 SetQueueMode 指定,默认FIFO
static std::string GetIpAddressLocalFirst(bool ipv4=true)
返回第一个本地ip,失败返回127.0.0.1
static std::vector< std::string > GetIpAddressLocal(bool ipv4=true)
返回本地ip
static std::string GetHostName()
返回主机名
std::string GetRemoteAddressString(int socket_id)
std::function< void(int)> CbDisconnect
断线回调
void Send(int sokect_id, E msg_id, const T &a)
bool Accept(uint16_t port)
TcpMessageServer * GetPopMsg()
void SetCbDisconnect(CbDisconnect func)
设置断线回调
bool Start(std::string_view ip, uint16_t port)
friend class ServerUdpImp
constexpr bool NETWORK_LOG_CLIENT
constexpr bool NETWORK_LOG_SERVER_UDP
constexpr bool NETWORK_LOG_LOW
ListMultiThread< UdpMessage > UdpMessageList
ListMultiThread< TcpMessage > TcpMessageList
std::string to_string_auto(BufferView buf, bool add_tag=false)
buf转为字符串,若不为utf8则转为16进制
ListMultiThread< Buffer > BufferList
constexpr uint8_t NETWORK_MESSAGE_TAG
constexpr bool NETWORK_LOG_CLIENT_UDP
constexpr bool NETWORK_LOG_SERVER
BufferView CreateBufferView(const Buffer &buf)
创建buffer视图(右值不能默认转换)