PDF
AUBO SDK  0.26.0
socket.h
浏览该文件的文档.
1/** @file socket.h
2 * @brief socket通信
3 */
4#ifndef AUBO_SDK_SOCKET_INTERFACE_H
5#define AUBO_SDK_SOCKET_INTERFACE_H
6
7#include <vector>
8#include <memory>
9
10#include <aubo/type_def.h>
11#include <aubo/global_config.h>
12
13namespace arcs {
14namespace common_interface {
15
16
17/**
18 * \chinese
19 * @defgroup Socket Socket (socket网络通信)
20 * 串口通信
21 * \endchinese
22 *
23 * \english
24 * @defgroup Socket Socket Network Communication
25 * Socket communication
26 * \endenglish
27 */
28class ARCS_ABI_EXPORT Socket
29{
30public:
32 virtual ~Socket();
33
34 /**
35 * @ingroup Socket
36 * \english
37 * Open TCP/IP ethernet communication socket
38 *
39 * Instruction
40 *
41 * @param address
42 * @param port
43 * @param socket_name
44 * @return
45 *
46 * @par Python function prototype
47 * socketOpen(self: pyaubo_sdk.Socket, arg0: str, arg1: int, arg2: str) -> int
48 *
49 * @par Lua function prototype
50 * socketOpen(address: string, port: number, socket_name: string) -> nil
51 *
52 * @par JSON-RPC request example
53 * {"jsonrpc":"2.0","method":"Socket.socketOpen","params":["172.16.26.248",8000,"socket_0"],"id":1}
54 *
55 * @par JSON-RPC response example
56 * {"id":1,"jsonrpc":"2.0","result":0}
57 * \endenglish
58 * \chinese
59 * 打开TCP/IP以太网通信socket
60 *
61 * 指令
62 *
63 * @param address 地址
64 * @param port 端口
65 * @param socket_name 套接字名称
66 * @return 返回值
67 *
68 * @par Python函数原型
69 * socketOpen(self: pyaubo_sdk.Socket, arg0: str, arg1: int, arg2: str) -> int
70 *
71 * @par Lua函数原型
72 * socketOpen(address: string, port: number, socket_name: string) -> nil
73 *
74 * @par JSON-RPC请求示例
75 * {"jsonrpc":"2.0","method":"Socket.socketOpen","params":["172.16.26.248",8000,"socket_0"],"id":1}
76 *
77 * @par JSON-RPC响应示例
78 * {"id":1,"jsonrpc":"2.0","result":0}
79 * \endchinese
80 */
81 int socketOpen(const std::string &address, int port,
82 const std::string &socket_name = "socket_0");
83
84 /**
85 * @ingroup Socket
86 * \english
87 * Closes TCP/IP socket communication
88 * Closes down the socket connection to the server.
89 *
90 * Instruction
91 *
92 * @param socket_name
93 * @return
94 *
95 * @par Python function prototype
96 * socketClose(self: pyaubo_sdk.Socket, arg0: str) -> int
97 *
98 * @par Lua function prototype
99 * socketClose(socket_name: string) -> nil
100 *
101 * @par JSON-RPC request example
102 * {"jsonrpc":"2.0","method":"Socket.socketClose","params":["socket_0"],"id":1}
103 *
104 * @par JSON-RPC response example
105 * {"id":1,"jsonrpc":"2.0","result":0}
106 * \endenglish
107 * \chinese
108 * 关闭TCP/IP socket 通信
109 * 关闭与服务器的 socket 连接。
110 *
111 * 指令
112 *
113 * @param socket_name 套接字名称
114 * @return 返回值
115 *
116 * @par Python函数原型
117 * socketClose(self: pyaubo_sdk.Socket, arg0: str) -> int
118 *
119 * @par Lua函数原型
120 * socketClose(socket_name: string) -> nil
121 *
122 * @par JSON-RPC请求示例
123 * {"jsonrpc":"2.0","method":"Socket.socketClose","params":["socket_0"],"id":1}
124 *
125 * @par JSON-RPC响应示例
126 * {"id":1,"jsonrpc":"2.0","result":0}
127 * \endchinese
128 */
129 int socketClose(const std::string &socket_name = "socket_0");
130
131 /**
132 * @ingroup Socket
133 * \english
134 * Reads a number of ascii formatted floats from the socket. A maximum
135 * of 30 values can be read in one command.
136 * A list of numbers read (list of floats, length=number+1)
137 *
138 * Result will be stored in a register named reg_key. Use getFloatVec
139 * to retrieve data
140 *
141 * @param number
142 * @param variable
143 * @param socket_name
144 * @return
145 *
146 * @par Python function prototype
147 * socketReadAsciiFloat(self: pyaubo_sdk.Socket, arg0: int, arg1: str, arg2:
148 * str) -> int
149 *
150 * @par Lua function prototype
151 * socketReadAsciiFloat(number: number, variable: string, socket_name:
152 * string) -> number
153 * \endenglish
154 * \chinese
155 * 从socket读取指定数量的ASCII格式浮点数。一次最多可读取30个值。
156 * 读取到的数字列表(浮点数列表,长度=number+1)
157 *
158 * 结果将存储在名为reg_key的寄存器中。使用getFloatVec获取数据
159 *
160 * @param number 数量
161 * @param variable 变量名
162 * @param socket_name 套接字名称
163 * @return 返回值
164 *
165 * @par Python函数原型
166 * socketReadAsciiFloat(self: pyaubo_sdk.Socket, arg0: int, arg1: str, arg2:
167 * str) -> int
168 *
169 * @par Lua函数原型
170 * socketReadAsciiFloat(number: number, variable: string, socket_name:
171 * string) -> number
172 * \endchinese
173 */
174 int socketReadAsciiFloat(int number, const std::string &variable,
175 const std::string &socket_name = "socket_0");
176
177 /**
178 * @ingroup Socket
179 * \english
180 * Reads a number of 32 bit integers from the socket. Bytes are in
181 * network byte order. A maximum of 30 values can be read in one
182 * command.
183 * A list of numbers read (list of ints, length=number+1)
184 *
185 * Instruction
186 *
187 * std::vector<int>
188 *
189 * @param number
190 * @param variable
191 * @param socket_name
192 * @return
193 *
194 * @par Python function prototype
195 * socketReadBinaryInteger(self: pyaubo_sdk.Socket, arg0: int, arg1: str,
196 * arg2: str) -> int
197 *
198 * @par Lua function prototype
199 * socketReadBinaryInteger(number: number, variable: string, socket_name:
200 * string) -> number
201 * \endenglish
202 * \chinese
203 * 从socket读取指定数量的32位整数。字节为网络字节序。一次最多可读取30个值。
204 * 读取到的数字列表(整数列表,长度=number+1)
205 *
206 * 指令
207 *
208 * std::vector<int>
209 *
210 * @param number 数量
211 * @param variable 变量名
212 * @param socket_name 套接字名称
213 * @return 返回值
214 *
215 * @par Python函数原型
216 * socketReadBinaryInteger(self: pyaubo_sdk.Socket, arg0: int, arg1: str,
217 * arg2: str) -> int
218 *
219 * @par Lua函数原型
220 * socketReadBinaryInteger(number: number, variable: string, socket_name:
221 * string) -> number
222 * \endchinese
223 */
224 int socketReadBinaryInteger(int number, const std::string &variable,
225 const std::string &socket_name = "socket_0");
226
227 /**
228 * @ingroup Socket
229 * \english
230 * Reads a number of bytes from the socket. Bytes are in network byte
231 * order. A maximum of 30 values can be read in one command.
232 * A list of numbers read (list of ints, length=number+1)
233 *
234 * Instruction
235 *
236 * std::vector<char>
237 *
238 * @param number
239 * @param variable
240 * @param socket_name
241 * @return
242 *
243 * @par Python function prototype
244 * socketReadByteList(self: pyaubo_sdk.Socket, arg0: int, arg1: str, arg2:
245 * str) -> int
246 *
247 * @par Lua function prototype
248 * socketReadByteList(number: number, variable: string, socket_name: string)
249 * -> number
250 * \endenglish
251 * \chinese
252 * 从socket读取指定数量的字节。字节为网络字节序。一次最多可读取30个值。
253 * 读取到的数字列表(整数列表,长度=number+1)
254 *
255 * 指令
256 *
257 * std::vector<char>
258 *
259 * @param number 数量
260 * @param variable 变量名
261 * @param socket_name 套接字名称
262 * @return 返回值
263 *
264 * @par Python函数原型
265 * socketReadByteList(self: pyaubo_sdk.Socket, arg0: int, arg1: str, arg2:
266 * str) -> int
267 *
268 * @par Lua函数原型
269 * socketReadByteList(number: number, variable: string, socket_name: string)
270 * -> number
271 * \endchinese
272 */
273 int socketReadByteList(int number, const std::string &variable,
274 const std::string &socket_name = "socket_0");
275
276 /**
277 * @ingroup Socket
278 * \english
279 * Reads all data from the socket and returns the data as a string.
280 * Bytes are in network byte order.
281 *
282 * The optional parameters "prefix" and "suffix", can be used to express
283 * what is extracted from the socket. The "prefix" specifies the start
284 * of the substring (message) extracted from the socket. The data up to
285 * the end of the "prefix" will be ignored and removed from the socket.
286 * The "suffix" specifies the end of the substring (message) extracted
287 * from the socket. Any remaining data on the socket, after the "suffix",
288 * will be preserved. E.g. if the socket server sends a string
289 * "noise>hello<", the controller can receive the "hello" by calling this
290 * script function with the prefix=">" and suffix="<". By using the
291 * "prefix" and "suffix" it is also possible send multiple string to the
292 * controller at once, because the suffix defines where the message ends.
293 * E.g. sending ">hello<>world<"
294 *
295 * Instruction
296 *
297 * std::string
298 *
299 * @param variable
300 * @param socket_name
301 * @param prefix
302 * @param suffix
303 * @param interpret_escape
304 * @return
305 *
306 * @par Python function prototype
307 * socketReadString(self: pyaubo_sdk.Socket, arg0: str, arg1: str, arg2:
308 * str, arg3: str, arg4: bool) -> int
309 *
310 * @par Lua function prototype
311 * socketReadString(variable: string, socket_name: string, prefix: string,
312 * suffix: string, interpret_escape: boolean) -> number
313 *
314 * @par JSON-RPC request example
315 * {"jsonrpc":"2.0","method":"Socket.socketReadString","params":["camera","socket_0","","",false],"id":1}
316 *
317 * @par JSON-RPC response example
318 * {"id":1,"jsonrpc":"2.0","result":0}
319 * \endenglish
320 * \chinese
321 * 从socket读取所有数据并将其作为字符串返回。
322 * 字节为网络字节序。
323 *
324 * 可选参数"prefix"和"suffix"可用于指定从socket中提取的内容。
325 * "prefix"指定提取子字符串(消息)的起始位置。直到"prefix"结尾的数据将被忽略并从socket中移除。
326 * "suffix"指定提取子字符串(消息)的结束位置。"suffix"之后的任何剩余数据将保留在socket中。
327 * 例如,如果socket服务器发送字符串"noise>hello<",控制器可以通过调用此脚本函数并设置prefix=">"和suffix="<"来接收"hello"。
328 * 通过使用"prefix"和"suffix",还可以一次向控制器发送多条字符串,因为"suffix"定义了消息的结束位置。例如发送">hello<>world<"
329 *
330 * 指令
331 *
332 * std::string
333 *
334 * @param variable 变量名
335 * @param socket_name 套接字名称
336 * @param prefix 前缀
337 * @param suffix 后缀
338 * @param interpret_escape 是否解释转义字符
339 * @return 返回值
340 *
341 * @par Python函数原型
342 * socketReadString(self: pyaubo_sdk.Socket, arg0: str, arg1: str, arg2:
343 * str, arg3: str, arg4: bool) -> int
344 *
345 * @par Lua函数原型
346 * socketReadString(variable: string, socket_name: string, prefix: string,
347 * suffix: string, interpret_escape: boolean) -> number
348 *
349 * @par JSON-RPC请求示例
350 * {"jsonrpc":"2.0","method":"Socket.socketReadString","params":["camera","socket_0","","",false],"id":1}
351 *
352 * @par JSON-RPC响应示例
353 * {"id":1,"jsonrpc":"2.0","result":0}
354 * \endchinese
355 */
356 int socketReadString(const std::string &variable,
357 const std::string &socket_name = "socket_0",
358 const std::string &prefix = "",
359 const std::string &suffix = "",
360 bool interpret_escape = false);
361
362 /**
363 * @ingroup Socket
364 * \english
365 * Reads all data from the socket and returns the data as a vector of chars.
366 *
367 * Instruction
368 * std::vector<char>
369 *
370 * @param variable
371 * @param socket_name
372 * @return
373 *
374 * @par Python function prototype
375 * socketReadAllString(self: pyaubo_sdk.Socket, arg0: str, arg1: str) -> int
376 *
377 * @par Lua function prototype
378 * socketReadAllString(variable: string, socket_name: string) -> number
379 *
380 * @par JSON-RPC request example
381 * {"jsonrpc":"2.0","method":"Socket.socketReadAllString","params":["camera","socket_0"],"id":1}
382 *
383 * @par JSON-RPC response example
384 * {"id":1,"jsonrpc":"2.0","result":0}
385 * \endenglish
386 * \chinese
387 * 从socket读取所有数据并将其作为char向量返回。
388 *
389 * 指令
390 * std::vector<char>
391 *
392 * @param variable 变量名
393 * @param socket_name 套接字名称
394 * @return 返回值
395 *
396 * @par Python函数原型
397 * socketReadAllString(self: pyaubo_sdk.Socket, arg0: str, arg1: str) -> int
398 *
399 * @par Lua函数原型
400 * socketReadAllString(variable: string, socket_name: string) -> number
401 *
402 * @par JSON-RPC请求示例
403 * {"jsonrpc":"2.0","method":"Socket.socketReadAllString","params":["camera","socket_0"],"id":1}
404 *
405 * @par JSON-RPC响应示例
406 * {"id":1,"jsonrpc":"2.0","result":0}
407 * \endchinese
408 */
409 int socketReadAllString(const std::string &variable,
410 const std::string &socket_name = "socket_0");
411
412 /**
413 * @ingroup Socket
414 * \english
415 * Sends a byte to the server
416 * Sends the byte <value> through the socket. Expects no response. Can
417 * be used to send special ASCII characters; 10 is newline, 2 is start of
418 * text, 3 is end of text.
419 *
420 * Instruction
421 *
422 * @param value
423 * @param socket_name
424 * @return
425 *
426 * @par Python function prototype
427 * socketSendByte(self: pyaubo_sdk.Socket, arg0: str, arg1: str) -> int
428 *
429 * @par Lua function prototype
430 * socketSendByte(value: string, socket_name: string) -> nil
431 *
432 * \endenglish
433 * \chinese
434 * 发送一个字节到服务器
435 * 通过socket发送字节<value>,不期望响应。可用于发送特殊ASCII字符;10为换行符,2为文本开始,3为文本结束。
436 *
437 * 指令
438 *
439 * @param value 字节值
440 * @param socket_name 套接字名称
441 * @return 返回值
442 *
443 * @par Python函数原型
444 * socketSendByte(self: pyaubo_sdk.Socket, arg0: str, arg1: str) -> int
445 *
446 * @par Lua函数原型
447 * socketSendByte(value: string, socket_name: string) -> nil
448 *
449 * \endchinese
450 */
451 int socketSendByte(char value, const std::string &socket_name = "socket_0");
452
453 /**
454 * @ingroup Socket
455 * \english
456 * Sends an int (int32_t) to the server
457 * Sends the int <value> through the socket. Send in network byte order.
458 * Expects no response
459 *
460 * Instruction
461 *
462 * @param value
463 * @param socket_name
464 * @return
465 *
466 * @par Python function prototype
467 * socketSendInt(self: pyaubo_sdk.Socket, arg0: int, arg1: str) -> int
468 *
469 * @par Lua function prototype
470 * socketSendInt(value: number, socket_name: string) -> nil
471 *
472 * \endenglish
473 * \chinese
474 * 发送一个int(int32_t)到服务器
475 * 通过socket发送int <value>,以网络字节序发送。不期望响应。
476 *
477 * 指令
478 *
479 * @param value 整数值
480 * @param socket_name 套接字名称
481 * @return 返回值
482 *
483 * @par Python函数原型
484 * socketSendInt(self: pyaubo_sdk.Socket, arg0: int, arg1: str) -> int
485 *
486 * @par Lua函数原型
487 * socketSendInt(value: number, socket_name: string) -> nil
488 *
489 * \endchinese
490 */
491 int socketSendInt(int value, const std::string &socket_name = "socket_0");
492
493 /**
494 * @ingroup Socket
495 * \english
496 * Sends a string with a newline character to the server
497 * Sends the string <str> through the socket in ASCII coding. Expects no
498 * response.
499 *
500 * Instruction
501 *
502 * @param str
503 * @param socket_name
504 * @return
505 *
506 * @par Python function prototype
507 * socketSendLine(self: pyaubo_sdk.Socket, arg0: str, arg1: str) -> int
508 *
509 * @par Lua function prototype
510 * socketSendLine(str: string, socket_name: string) -> nil
511 *
512 * @par JSON-RPC request example
513 * {"jsonrpc":"2.0","method":"Socket.socketSendLine","params":["abcd","socket_0"],"id":1}
514 *
515 * @par JSON-RPC response example
516 * {"id":1,"jsonrpc":"2.0","result":0}
517 * \endenglish
518 * \chinese
519 * 发送带有换行符的字符串到服务器.
520 * 通过socket以ASCII编码发送字符串<str>,不期望响应。
521 *
522 * 指令
523 *
524 * @param str 字符串
525 * @param socket_name 套接字名称
526 * @return 返回值
527 *
528 * @par Python函数原型
529 * socketSendLine(self: pyaubo_sdk.Socket, arg0: str, arg1: str) -> int
530 *
531 * @par Lua函数原型
532 * socketSendLine(str: string, socket_name: string) -> nil
533 *
534 * @par JSON-RPC请求示例
535 * {"jsonrpc":"2.0","method":"Socket.socketSendLine","params":["abcd","socket_0"],"id":1}
536 *
537 * @par JSON-RPC响应示例
538 * {"id":1,"jsonrpc":"2.0","result":0}
539 * \endchinese
540 */
541 int socketSendLine(const std::string &str,
542 const std::string &socket_name = "socket_0");
543
544 /**
545 * @ingroup Socket
546 * \english
547 * Sends a string to the server
548 * Sends the string <str> through the socket in ASCII coding. Expects no
549 * response.
550 *
551 * Instruction
552 *
553 * @param str
554 * @param socket_name
555 * @return
556 *
557 * @par Python function prototype
558 * socketSendString(self: pyaubo_sdk.Socket, arg0: str, arg1: str) -> int
559 *
560 * @par Lua function prototype
561 * socketSendString(str: string, socket_name: string) -> nil
562 *
563 * @par JSON-RPC request example
564 * {"jsonrpc":"2.0","method":"Socket.socketSendString","params":["abcd","socket_0"],"id":1}
565 *
566 * @par JSON-RPC response example
567 * {"id":1,"jsonrpc":"2.0","result":0}
568 * \endenglish
569 * \chinese
570 * 发送字符串到服务器
571 * 通过socket以ASCII编码发送字符串<str>,不期望响应。
572 *
573 * 指令
574 *
575 * @param str 字符串
576 * @param socket_name 套接字名称
577 * @return 返回值
578 *
579 * @par Python函数原型
580 * socketSendString(self: pyaubo_sdk.Socket, arg0: str, arg1: str) -> int
581 *
582 * @par Lua函数原型
583 * socketSendString(str: string, socket_name: string) -> nil
584 *
585 * @par JSON-RPC请求示例
586 * {"jsonrpc":"2.0","method":"Socket.socketSendString","params":["abcd","socket_0"],"id":1}
587 *
588 * @par JSON-RPC响应示例
589 * {"id":1,"jsonrpc":"2.0","result":0}
590 * \endchinese
591 */
592 int socketSendString(const std::string &str,
593 const std::string &socket_name = "socket_0");
594
595 /**
596 * @ingroup Socket
597 * \english
598 * Sends all data in the given vector of chars to the server.
599 *
600 * @param is_check Whether to check the sending status
601 * @param str The data to send as a vector of chars
602 * @param socket_name The name of the socket
603 * @return Status code
604 *
605 * @par Python function prototype
606 * socketSendAllString(self: pyaubo_sdk.Socket, arg0: bool, arg1: List[str], arg2: str) -> int
607 *
608 * @par Lua function prototype
609 * socketSendAllString(is_check: boolean, str: table, socket_name: string) -> nil
610 * \endenglish
611 * \chinese
612 * 发送给定char向量中的所有数据到服务器。
613 *
614 * @param is_check 是否检查发送状态
615 * @param str 要发送的数据,char向量
616 * @param socket_name 套接字名称
617 * @return 状态码
618 *
619 * @par Python函数原型
620 * socketSendAllString(self: pyaubo_sdk.Socket, arg0: bool, arg1: List[str], arg2: str) -> int
621 *
622 * @par Lua函数原型
623 * socketSendAllString(is_check: boolean, str: table, socket_name: string) -> nil
624 * \endchinese
625 */
626 int socketSendAllString(bool is_check, const std::vector<char> &str,
627 const std::string &socket_name = "socket_0");
628
629 /**
630 * @ingroup Socket
631 * \english
632 * Check if the socket is connected
633 * @brief socketHasConnected
634 * @param socket_name
635 *
636 * @return
637 *
638 * @par Python function prototype
639 * socketHasConnected(self: pyaubo_sdk.Socket, arg0: str) -> bool
640 *
641 * @par Lua function prototype
642 * socketHasConnected(socket_name: string) -> boolean
643 * \endenglish
644 * \chinese
645 * 检测 socket 连接是否成功
646 * @brief socketHasConnected
647 * @param socket_name
648 *
649 * @return
650 *
651 * @par Python函数原型
652 * socketHasConnected(self: pyaubo_sdk.Socket, arg0: str) -> bool
653 *
654 * @par Lua函数原型
655 * socketHasConnected(socket_name: string) -> boolean
656 * \endchinese
657 */
658 bool socketHasConnected(const std::string &socket_name = "socket_0");
659
660protected:
661 void *d_;
662};
663using SocketPtr = std::shared_ptr<Socket>;
664
665} // namespace common_interface
666} // namespace arcs
667#endif
bool socketHasConnected(const std::string &socket_name="socket_0")
检测 socket 连接是否成功
int socketClose(const std::string &socket_name="socket_0")
关闭TCP/IP socket 通信 关闭与服务器的 socket 连接。
int socketSendAllString(bool is_check, const std::vector< char > &str, const std::string &socket_name="socket_0")
发送给定char向量中的所有数据到服务器。
int socketReadString(const std::string &variable, const std::string &socket_name="socket_0", const std::string &prefix="", const std::string &suffix="", bool interpret_escape=false)
从socket读取所有数据并将其作为字符串返回。 字节为网络字节序。
int socketReadBinaryInteger(int number, const std::string &variable, const std::string &socket_name="socket_0")
从socket读取指定数量的32位整数。字节为网络字节序。一次最多可读取30个值。 读取到的数字列表(整数列表,长度=number+1)
int socketOpen(const std::string &address, int port, const std::string &socket_name="socket_0")
打开TCP/IP以太网通信socket
int socketReadAsciiFloat(int number, const std::string &variable, const std::string &socket_name="socket_0")
从socket读取指定数量的ASCII格式浮点数。一次最多可读取30个值。 读取到的数字列表(浮点数列表,长度=number+1)
int socketReadByteList(int number, const std::string &variable, const std::string &socket_name="socket_0")
从socket读取指定数量的字节。字节为网络字节序。一次最多可读取30个值。 读取到的数字列表(整数列表,长度=number+1)
int socketSendInt(int value, const std::string &socket_name="socket_0")
发送一个int(int32_t)到服务器 通过socket发送int ,以网络字节序发送。不期望响应。
int socketSendByte(char value, const std::string &socket_name="socket_0")
发送一个字节到服务器 通过socket发送字节<value>,不期望响应。可用于发送特殊ASCII字符;10为换行符,2为文本开始,3为文本结束。
int socketSendLine(const std::string &str, const std::string &socket_name="socket_0")
发送带有换行符的字符串到服务器.
int socketReadAllString(const std::string &variable, const std::string &socket_name="socket_0")
从socket读取所有数据并将其作为char向量返回。
int socketSendString(const std::string &str, const std::string &socket_name="socket_0")
发送字符串到服务器 通过socket以ASCII编码发送字符串<str>,不期望响应。
std::shared_ptr< Socket > SocketPtr
数据类型的定义