commit | author | age
|
29b331
|
1 |
/******************************************************************************** |
GW |
2 |
* Copyright: (C) 2022 LingYun IoT System Studio |
|
3 |
* All rights reserved. |
|
4 |
* |
|
5 |
* Filename: socket.h |
|
6 |
* Description: This head file is for socket API functions |
|
7 |
* |
|
8 |
* Version: 1.0.0(18/04/22) |
|
9 |
* Author: Guo Wenxue <guowenxue@gmail.com> |
|
10 |
* ChangeLog: 1, Release initial version on "18/04/22 17:09:59" |
|
11 |
* |
|
12 |
********************************************************************************/ |
|
13 |
|
|
14 |
#ifndef _SOCKET_H_ |
|
15 |
#define _SOCKET_H_ |
|
16 |
|
|
17 |
#define HOSTNAME_LEN 64 |
|
18 |
|
|
19 |
typedef struct socket_ctx_s |
|
20 |
{ |
|
21 |
char host[HOSTNAME_LEN]; /* CLIENT: Connect server hostname; SERVER: Unused */ |
|
22 |
int port; /* CLIENT: Connect server port; SERVER: listen port */ |
|
23 |
int fd; /* socket descriptor */ |
|
24 |
} socket_ctx_t; |
|
25 |
|
|
26 |
/* description: initial socket context |
|
27 |
* input args: |
|
28 |
* $sock: socket context pointer |
|
29 |
* $host: connect server hostname for client mode, unused for server mode |
|
30 |
* $port: connect server port for client mode or listen port for server mode |
|
31 |
* return value: <0: failure 0:ok |
|
32 |
*/ |
|
33 |
extern int socket_init(socket_ctx_t *sock, char *host, int port); |
|
34 |
|
|
35 |
/* description: close socket |
|
36 |
* input args: |
|
37 |
* $sock: socket context pointer |
|
38 |
* return value: <0: failure 0:ok |
|
39 |
*/ |
|
40 |
extern int socket_term(socket_ctx_t *sock); |
|
41 |
|
|
42 |
/* description: socket server start listen |
|
43 |
* input args: |
|
44 |
* $sock: socket context pointer |
|
45 |
* return value: <0: failure 0:ok |
|
46 |
*/ |
|
47 |
extern int socket_listen(socket_ctx_t *sock); |
|
48 |
|
|
49 |
/* description: socket client connect to server |
|
50 |
* input args: |
|
51 |
* $sock: socket context pointer |
|
52 |
* return value: <0: failure 0:ok |
|
53 |
*/ |
|
54 |
extern int socket_connect(socket_ctx_t *sock); |
|
55 |
|
|
56 |
/* description: send data from the socket |
|
57 |
* input args: |
|
58 |
* $sock : socket context pointer |
|
59 |
* $data : socket send data |
|
60 |
* $bytes: socket send data bytes |
|
61 |
* return value: <0: failure 0:ok |
|
62 |
*/ |
|
63 |
extern int socket_send(socket_ctx_t *sock, char *data, int bytes); |
|
64 |
|
|
65 |
/* description: receive data from the socket |
|
66 |
* input args: |
|
67 |
* $sock : socket context pointer |
|
68 |
* $buf : socket receive data buffer |
|
69 |
* $size : socket receive data buffer size |
|
70 |
* $timeout: receive data time, <=0 will don't timeout |
|
71 |
* return value: <0: failure 0:ok |
|
72 |
*/ |
|
73 |
#define TIMEOUT_NONE 0 |
|
74 |
extern int socket_recv(socket_ctx_t *sock, char *buf, int size, int timeout); |
|
75 |
|
|
76 |
/*+-------------------------------------------------------------------+ |
|
77 |
*| socket utils function | |
|
78 |
*+-------------------------------------------------------------------+*/ |
|
79 |
|
|
80 |
|
|
81 |
/* socket connected or not: <0: failure 0:ok */ |
|
82 |
extern int sock_check_connect(int sockfd); |
|
83 |
|
|
84 |
/* description: set socket listen port as reusable, fix port already used bug */ |
|
85 |
extern int socket_set_reuseaddr(int sockfd); |
|
86 |
|
|
87 |
/* set socket as non-block mode, common socket default work as block mode */ |
|
88 |
extern int socket_set_nonblock(int sockfd); |
|
89 |
|
|
90 |
/* set socket receive and send buffer size in linux kernel space */ |
|
91 |
extern int socket_set_buffer(int sockfd, int rsize, int ssize); |
|
92 |
|
|
93 |
/* set heartbeat keepalive */ |
|
94 |
extern int socket_set_keepalive(int sockfd, int keepintvl, int keepcnt); |
|
95 |
|
|
96 |
/* Set open file description count to max */ |
|
97 |
extern void set_socket_rlimit(void); |
|
98 |
|
|
99 |
#endif /* ----- #ifndef _SOCKET_H_ ----- */ |
|
100 |
|