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();
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();
106 void Run();
107
110private:
111 ServerImp* _imp;
112 Thread* _t;
113
114 static void _thread_run(void*);
115};
116
117class ClientUdpImp;
120{
121public:
122 void SetAddress(std::string_view ip, uint16_t port, bool broadcast = false);
123 void Send(BufferView buf);
124 Buffer* GetPopMsg() { return _bufListRecv.Beg(); }
125 void PopMsg() { _bufListRecv.End(); }
126
129private:
130 ClientUdpImp* _imp;
131 std::atomic_bool _bEnd;
132 Thread* _tSend;
133 Thread* _tRecv;
134 BufferList _bufListSend;
135 BufferList _bufListRecv;
136
137 static void _thread_send(void*);
138 static void _thread_recv(void*);
139};
140
141class ServerUdpImp;
144{
145 friend class ServerUdpImp;
146public:
147 void Start(std::string_view ip, uint16_t port);
148
149 UdpMessage* GetPopMsg() { return _listMsg.Beg(); }
150 void PopMsg() { _listMsg.End(); }
151
154private:
155 ServerUdpImp* _imp;
156 Thread* _t;
157 UdpMessageList _listMsg;
158
159 static void _thread_run(void*);
160};
161
163{
164public:
166 {
167 //Debug();
168 }
169
172 static std::string GetHostName();
173
177 static std::vector<std::string> GetIpAddressLocal(bool ipv4 = true);
181 static std::string GetIpAddressLocalFirst(bool ipv4 = true)
182 {
183 std::vector<std::string> vec = GetIpAddressLocal(ipv4);
184 return vec.empty() ? "127.0.0.1" : vec[0];
185 }
186
187 void Debug()
188 {
189 log_msg("----------------网络信息-------------\n"
190 "主机名:{}\n"
191 "本地ip:{}\n",
192 GetHostName(),
193 String::Splicing(GetIpAddressLocal()));
194 }
195};
196
197extern Network* g_net;
198}
bool Connent()
void Send(BufferView buf)
void Send(BufferView buf)
Buffer * GetPopMsg()
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()
bool Accept()
friend class ServerUdpImp
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
多线程队列
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