NSUKit 1.4.0
板卡级统一交互接口
载入中...
搜索中...
未找到
tcp_interface.cpp
浏览该文件的文档.
1//
2// Created by 56585 on 2023/8/16.
3//
4
6
7
8using namespace nsukit;
9
10
12 mixin_ = new Mixin_VirtualRegCmd(this);
13}
14
15
17 delete mixin_;
18}
19
20
23 tcpPort = param->cmd_tcp_port;
24 ipAddr = param->cmd_ip;
25
26 opLock.lock();
27 try {
28 sockGen.Init(true, tcpPort, ipAddr);
30 } catch (...) {
31 opLock.unlock();
32 return nsukitStatus_t::NSUKIT_STATUS_ACCEPT_FAIL;
33 }
34 opLock.unlock();
35 return nsukitStatus_t::NSUKIT_STATUS_SUCCESS;
36}
37
38
41 return nsukitStatus_t::NSUKIT_STATUS_SUCCESS;
42}
43
44
46 sockGen.recvTimeout = (int) (s * 1000);
47 tcpTimeout = s;
48 return nsukitStatus_t::NSUKIT_STATUS_SUCCESS;
49}
50
51
53 return mixin_->_common_write(addr, value);
54}
55
56
58 return mixin_->_common_read(addr, buf);
59}
60
61
63 return send_bytes(bytes.data(), bytes.size());
64}
65
66
68 auto res = nsukitStatus_t::NSUKIT_STATUS_SUCCESS;
69 opLock.lock();
70 auto tcp_status = TcpSendBytes(&sockGen, bytes, length);
71 if (tcp_status == 0) {
72 res |= nsukitStatus_t::NSUKIT_STATUS_ITF_FAIL;
73 DEBUG_PRINT_CLASS("Send Bytes FAIL, status 0");
74 }
75
76// sockGen.CloseSock(sockGen.tcpClient);
77 opLock.unlock();
78 return res;
79}
80
81
83 auto res = nsukitStatus_t::NSUKIT_STATUS_SUCCESS;
84 opLock.lock();
85 auto tcp_status = TcpRecvBytes(&sockGen, buf, size);
86 if (tcp_status == 0) {
87 res |= nsukitStatus_t::NSUKIT_STATUS_ITF_FAIL;
88 DEBUG_PRINT_CLASS("Recv Bytes FAIL, status 0");
89 }
90// sockGen.CloseSock(sockGen.tcpClient);
91 opLock.unlock();
92 return res;
93}
94
95
97 if (upload_thread.joinable()) {
99 }
100 if (upload_thread.joinable()) {
102 }
103 std::map<unsigned int, Memory *>::iterator it;
104 for (it = memory_dict.begin(); it != memory_dict.end(); ++it) {
105 if (it->second->self_alloc) free(it->second->memory);
106 delete it->second;
107 }
108}
109
110
112 ipAddr = param->stream_ip;
113 tcpPort = param->stream_tcp_port;
115 opLock.lock();
116 try {
117 sockGen.Init(true, tcpPort, ipAddr);
118 } catch (...) {
119 opLock.unlock();
120 return nsukitStatus_t::NSUKIT_STATUS_ACCEPT_FAIL;
121 }
122 opLock.unlock();
123 return nsukitStatus_t::NSUKIT_STATUS_SUCCESS;
124}
125
126
128 auto res = nsukitStatus_t::NSUKIT_STATUS_SUCCESS;
129 opLock.lock();
130 try {
132 } catch (...) {
133 res |= nsukitStatus_t::NSUKIT_STATUS_ITF_FAIL;
134 DEBUG_PRINT_CLASS("ITF_FALL close tcp link catch");
135 }
136 opLock.unlock();
137 return res;
138}
139
140
142 tcpTimeout = s;
143 return nsukitStatus_t::NSUKIT_STATUS_SUCCESS;
144}
145
146
148 auto mem = new Memory{};
149 if (buf == nullptr) {
150 mem->memory = (nsuMemory_p) malloc(length);
151 mem->self_alloc = true;
152 } else {
153 mem->memory = (nsuMemory_p) buf;
154 mem->self_alloc = false;
155 }
156
157 memset(mem->memory, length, 1);
158 mem->idx = memory_index;
159 mem->mem_size = length;
160 mem->finish_event.setEvent();
161
163 memory_index++;
164 return mem;
165}
166
167
169 auto mem = (Memory *) fd;
170 if (!mem->finish_event.isSet()) {
171 return nsukitStatus_t::NSUKIT_STATUS_STREAM_RUNNING;
172 }
173 mem->finish_event.resetEvent();
174 if (mem->self_alloc) free(mem->memory);
175 memory_dict.erase(mem->idx);
176 delete mem;
177 return nsukitStatus_t::NSUKIT_STATUS_SUCCESS;
178}
179
180
182 auto mem = (Memory *) fd;
183 return (nsuVoidBuf_p) (mem->memory);
184}
185
186
189 auto res = nsukitStatus_t::NSUKIT_STATUS_SUCCESS;
190 auto mem = (Memory *) fd;
191 { // set memory param
192 std::unique_lock<std::mutex> lock(mem->mtx);
193 if (mem->mem_size < length + offset) {
194 return nsukitStatus_t::NSUKIT_STATUS_INVALID_VALUE;
195 }
196 mem->need_size = length;
197 mem->using_size = 0;
198 mem->error_msg = "";
199 }
200
201 {
202 std::unique_lock<std::mutex> lock(opLock);
203 if ((!sockGen.tcpClientConnected) && sockGen.AcceptClient()==-1) return nsukitStatus_t::NSUKIT_STATUS_ACCEPT_FAIL;
204 }
205
206 auto thread = std::thread(_recv, &sockGen, &stop_event, mem, block_size, length, offset);
207 if (upload_thread.joinable()) upload_thread.join();
208 upload_thread = std::move(thread);
209 return res;
210}
211
212
213void TCPStreamUItf::_recv(SocketGenerator *sock, ThreadSafeEvent *stop_event, Memory *mem, uint32_t block,
214 nsuStreamLen_t length, nsuStreamLen_t offset) {
215 auto buffer = (nsuCharBuf_p) (mem->memory) + offset;
218 int stLen = 0;
219 int totalLen = 0;
220 int cur_count;
221
222 while (length) {
223 if (stop_event->isSet()) {
224 mem->finish_event.setEvent();
225 break;
226 }
227 cur_count = length - totalLen > block ? block : length - totalLen;
228 stLen = sock->RecvData(sock->tcpClient, buffer + totalLen, cur_count);
229 if (stLen == 0) {
230 // server connect interrupt
231 std::unique_lock<std::mutex> lock(mem->mtx);
232 mem->error_msg += "connect interrupt error";
233 break;
234 } else if (stLen > 0) {
235 std::unique_lock<std::mutex> lock(mem->mtx);
236 totalLen += stLen;
237 mem->using_size = totalLen;
238 if (totalLen >= length) {
239 break;
240 }
241 }
242 }
243 mem->finish_event.setEvent();
245}
246
247
249 auto mem = (Memory *) fd;
250 if (memory_dict.count(mem->idx) == 0) {
251 return nsukitStatus_t::NSUKIT_STATUS_INVALID_VALUE;
252 }
253 mem->finish_event.waitForEvent(static_cast<int>(timeout));
254
255 std::unique_lock<std::mutex> lock(mem->mtx);
256 if (!mem->error_msg.empty()) {
257 return nsukitStatus_t::NSUKIT_STATUS_STREAM_FAIL;
258 } else if (mem->using_size < mem->need_size) {
259 return nsukitStatus_t::NSUKIT_STATUS_STREAM_RUNNING;
260 } else {
261 return nsukitStatus_t::NSUKIT_STATUS_SUCCESS;
262 }
263
264}
265
266
269 if (upload_thread.joinable()) upload_thread.join();
271 return nsukitStatus_t::NSUKIT_STATUS_SUCCESS;
272}
273
274
277 bool (*stop_event)(), float timeout, int flag) {
278 auto res = open_recv(chnl, fd, length, offset);
279 if (stop_event == nullptr) stop_event = []() {return false;};
280 res = nsukitStatus_t::NSUKIT_STATUS_STREAM_RUNNING;
281 while (!stop_event() && res!=nsukitStatus_t::NSUKIT_STATUS_SUCCESS) {
282 res = wait_stream(fd, 1.);
283 }
284 return res;
285}
286
287
290 return I_BaseStreamUItf::open_send(chnl, fd, length, offset);
291}
292
293
296 bool (*stop_event)(), float timeout, int flag) {
297 return I_BaseStreamUItf::stream_send(chnl, fd, length, offset, stop_event, timeout, flag);
298}
void resetEvent()
Definition: type.cpp:33
bool isSet()
Definition: type.cpp:7
void setEvent()
Definition: type.cpp:13
U_BaseCmdMixin * mixin_
Definition: base_itf.h:83
virtual nsukitStatus_t stream_send(nsuChnlNum_t chnl, nsuMemory_p fd, nsuStreamLen_t length, nsuStreamLen_t offset=0, bool(*stop_event)()=nullptr, float timeout=5., int flag=1)
Definition: base_itf.h:255
virtual nsukitStatus_t open_send(nsuChnlNum_t chnl, nsuMemory_p fd, nsuStreamLen_t length, nsuStreamLen_t offset=0)
Definition: base_itf.h:233
void Init(bool update, unsigned short int port=0, std::string ip="")
Definition: c_socket.cpp:20
int ConnectServer(float connect_time=SELECT_TIMEOUT)
Definition: c_socket.cpp:99
int RecvData(int s, char *buf, int len)
Definition: c_socket.cpp:170
nsukitStatus_t accept(nsuInitParam_t *param) override
SocketGenerator sockGen
Definition: tcp_interface.h:15
nsukitStatus_t set_timeout(float s) override
nsukitStatus_t recv_bytes(nsuSize_t size, nsuCharBuf_p buf) override
std::string ipAddr
Definition: tcp_interface.h:18
~TCPCmdUItf() override
nsukitStatus_t send_bytes(nsuBytes_t &bytes) override
nsukitStatus_t write(nsuRegAddr_t addr, nsuRegValue_t value) override
nsukitStatus_t read(nsuRegAddr_t addr, nsuRegValue_t *buf) override
nsukitStatus_t close() override
nsukitStatus_t set_timeout(float s) override
static void _recv(SocketGenerator *sock, ThreadSafeEvent *stop_event, Memory *mem, uint32_t block, nsuStreamLen_t length, nsuStreamLen_t offset=0)
unsigned int memory_index
nsukitStatus_t wait_stream(nsuMemory_p fd, float timeout=1.) override
nsukitStatus_t open_send(nsuChnlNum_t chnl, nsuMemory_p fd, nsuStreamLen_t length, nsuStreamLen_t offset=0) override
nsukitStatus_t close() override
std::map< unsigned int, Memory * > memory_dict
nsukitStatus_t free_buffer(nsuMemory_p fd) override
std::thread upload_thread
nsukitStatus_t break_stream(nsuMemory_p fd) override
nsuVoidBuf_p get_buffer(nsuMemory_p fd, nsuStreamLen_t length) override
nsukitStatus_t accept(nsuInitParam_t *param) override
nsukitStatus_t stream_send(nsuChnlNum_t chnl, nsuMemory_p fd, nsuStreamLen_t length, nsuStreamLen_t offset=0, bool(*stop_event)()=nullptr, float timeout=5., int flag=1) override
SocketGenerator sockGen
nsuMemory_p alloc_buffer(nsuStreamLen_t length, nsuVoidBuf_p buf=nullptr) override
nsukitStatus_t open_recv(nsuChnlNum_t chnl, nsuMemory_p fd, nsuStreamLen_t length, nsuStreamLen_t offset=0) override
nsukitStatus_t stream_recv(nsuChnlNum_t chnl, nsuMemory_p fd, nsuStreamLen_t length, nsuStreamLen_t offset=0, bool(*stop_event)()=nullptr, float timeout=5., int flag=1) override
ThreadSafeEvent stop_event
virtual nsukitStatus_t _common_write(nsuRegAddr_t reg, nsuRegValue_t value)
Definition: base_itf.h:43
virtual nsukitStatus_t _common_read(nsuRegAddr_t reg, nsuRegValue_t *buf)
Definition: base_itf.h:47
#define DEBUG_PRINT_CLASS(message)
Definition: config.h:42
int TcpRecvBytes(SocketGenerator *s, char *buf, size_t len, bool *stopped=nullptr)
Definition: c_socket.cpp:203
int TcpSendBytes(SocketGenerator *s, char *buf, size_t len, bool *stopped=nullptr)
Definition: c_socket.cpp:228
std::string cmd_ip
Definition: type.h:98
uint32_t stream_tcp_block
Definition: type.h:104
uint32_t cmd_tcp_port
Definition: type.h:99
float cmd_tcp_timeout
Definition: type.h:100
std::string stream_ip
Definition: type.h:102
uint32_t stream_tcp_port
Definition: type.h:103
DLLEXTERN typedef nsuSize_t nsuStreamLen_t
Definition: type.h:88
nsukitStatus_t
Definition: type.h:53
DLLEXTERN typedef size_t nsuSize_t
Definition: type.h:87
DLLEXTERN typedef void * nsuVoidBuf_p
Definition: type.h:83
DLLEXTERN typedef void * nsuMemory_p
Definition: type.h:86
DLLEXTERN typedef char * nsuCharBuf_p
Definition: type.h:82
DLLEXTERN typedef std::vector< char > nsuBytes_t
Definition: type.h:81
DLLEXTERN typedef uint32_t nsuRegValue_t
Definition: type.h:90
DLLEXTERN typedef uint8_t nsuChnlNum_t
Definition: type.h:85
DLLEXTERN typedef uint32_t nsuRegAddr_t
Definition: type.h:89