dl
dl_net.h
浏览该文件的文档.
1
12#pragma once
13
14#include "io/dl_log.h"
15#include "base/dl_string.h"
16#include "system/dl_thread.h"
17
18#ifndef NDEBUG
19#define DL_NETWORK_DEBUG_INFO
20#endif
21
22namespace dl
23{
24 constexpr uint32_t NETWORK_MESSAGE_TAG = 0x1efe9722;
26struct MsgHead
27{
28 uint32_t _magic;
29 uint32_t _size;
30 uint32_t _check;
32};
33
35{
36 std::string _ip;
37 uint16_t _port;
39};
40
41template<typename T>
43{
44 std::mutex _mutex;// 队列锁
45 std::list<T> _list;// 先入先出队列
46 std::list<T> _listTemp;// 主线程缓冲
47
48 T* Beg()
49 {
50 _mutex.lock();
51 if (_list.empty())
52 {
53 _mutex.unlock();
54 return nullptr;
55 }
56 return &_list.front();
57 }
58 void End()
59 {
60 _list.pop_front();
61 _mutex.unlock();
62 }
63
64 // todo 左值引用
65 void Add(const T& buf)
66 {
67 if (_mutex.try_lock())
68 {
69 _list.splice(_list.begin(), _listTemp);
70 _list.push_back(buf);
71 _mutex.unlock();
72 return;
73 }
74 _listTemp.push_back(buf);
75 }
76};
79
80class ClientImp;
82class Client
83{
84public:
85 bool Connent(std::string_view ip, uint16_t port);
86 void Send(BufferView buf);
87
90private:
91 ClientImp* _imp;
92 Thread* _t;
93 std::atomic_bool _bEnd;
94 BufferList _bufList;
95
96 static void _thread_run(void*);// 主循环
97};
98
99
100class ServerImp;
103{
104public:
105 bool Accept(uint16_t port);
106 void Run();
107
108 std::string GetIp();
109 uint16_t GetPort();
110
113private:
114 ServerImp* _imp;
115 Thread* _t;
116
117 static void _thread_run(void*);
118};
119
120class ClientUdpImp;
123{
124public:
125 void SetAddress(std::string_view ip, uint16_t port, bool broadcast = false);
126 void Send(BufferView buf);
127 void Send(std::string_view str)
128 {
130 }
131 Buffer* GetPopMsg() { return _bufListRecv.Beg(); }
132 void PopMsg() { _bufListRecv.End(); }
133
136private:
137 ClientUdpImp* _imp;
138 std::atomic_bool _bEnd;
139 Thread* _tSend;
140 Thread* _tRecv;
141 BufferList _bufListSend;
142 BufferList _bufListRecv;
143
144 static void _thread_send(void*);
145 static void _thread_recv(void*);
146};
147
148class ServerUdpImp;
151{
152 friend class ServerUdpImp;
153public:
154 void Start(std::string_view ip, uint16_t port);
155
156 UdpMessage* GetPopMsg() { return _listMsg.Beg(); }
157 void PopMsg() { _listMsg.End(); }
158
159 std::string GetIp();
160 uint16_t GetPort();
161
164private:
165 ServerUdpImp* _imp;
166 Thread* _t;
167 UdpMessageList _listMsg;
168
169 static void _thread_run(void*);
170};
171
173{
174public:
176 {
177 //Debug();
178 }
179
182 static std::string GetHostName();
183
187 static std::vector<std::string> GetIpAddressLocal(bool ipv4 = true);
191 static std::string GetIpAddressLocalFirst(bool ipv4 = true)
192 {
193 std::vector<std::string> vec = GetIpAddressLocal(ipv4);
194 return vec.empty() ? "127.0.0.1" : vec[0];
195 }
196
197 void Debug()
198 {
199 log_msg("----------------网络信息-------------\n"
200 "主机名:{}\n"
201 "本地ip:{}\n",
202 GetHostName(),
203 String::Splicing(GetIpAddressLocal()));
204 }
205};
206
207extern Network* g_net;
208}
void Send(BufferView buf)
bool Connent(std::string_view ip, uint16_t port)
void Send(BufferView buf)
Buffer * GetPopMsg()
void Send(std::string_view str)
void SetAddress(std::string_view ip, uint16_t port, bool broadcast=false)
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()
返回主机名
void Run()
std::string GetIp()
uint16_t GetPort()
bool Accept(uint16_t port)
friend class ServerUdpImp
uint16_t GetPort()
std::string GetIp()
UdpMessage * GetPopMsg()
void Start(std::string_view ip, uint16_t port)
日志系统
#define log_msg(...)
字符串相关
线程封装
constexpr uint32_t NETWORK_MESSAGE_TAG
std::span< std::byte > BufferView
Network * g_net
ListMultiThread< UdpMessage > UdpMessageList
std::vector< std::byte > Buffer
ListMultiThread< Buffer > BufferList
BufferView CreateBufferView(std::string_view str)
创建buffer视图
多线程队列
std::list< Buffer > _listTemp
std::list< Buffer > _list
void Add(const T &buf)
网络消息头
uint32_t _size
uint32_t _magic
uint32_t _check
std::string _ip