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