APUE course source code
guowenxue
2 days ago 9c22371ef5059a2e46226ee90a0667ffad65b574
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/********************************************************************************
 *      Copyright:  (C) 2022 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  socket.h
 *    Description:  This head file is for socket API functions
 *
 *        Version:  1.0.0(18/04/22)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "18/04/22 17:09:59"
 *
 ********************************************************************************/
 
#ifndef  _SOCKET_H_
#define  _SOCKET_H_
 
#include <poll.h>
#include <netdb.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/resource.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <linux/sockios.h>
 
#define HOSTNAME_LEN          64
 
typedef struct socket_s
{
    char        host[HOSTNAME_LEN]; /* CLIENT: Connect server hostname; SERVER: Unused */
    int         port;               /* CLIENT: Connect server port;     SERVER: listen port */
    int         fd;                 /* socket descriptor  */
    int         connected;          /* socket connect status: 1->connected 0->disconnected  */
} socket_t;
 
/*  description: initial socket context
 *   input args:
 *               $sock:  socket context pointer
 *               $host:  connect server hostname for client mode, unused for server mode
 *               $port:  connect server port for client mode or listen port for server mode
 * return value: <0: failure   0:ok
 */
extern int socket_init(socket_t *sock, char *host, int port);
 
/*  description: close socket
 *   input args:
 *               $sock:  socket context pointer
 * return value: <0: failure   0:ok
 */
extern int socket_term(socket_t *sock);
 
/*  description: socket server start listen
 *   input args:
 *               $sock:  socket context pointer
 *               $port:  socket listen port
 * return value: <0: failure   0:ok
 */
extern int socket_listen(socket_t *sock, int port);
 
/*  description: create epoll for socket server and add listenfd into it
 *   input args: $max_evts:  max events for epoll_create()
 *               $listenfd:  listen socket fd
 * return value: <0: failure  >=0: epollfd
 */
extern int epoll_init(int max_evts, int listenfd);
 
/*  description: close epoll
 *   input args: $epollfd:  epoll fd
 * return value: void
 */ 
extern void epoll_term(int epollfd);
 
/*  description: add new fd into epoll to monitor
 *   input args: $epollfd:  epoll fd
 *               $fd:       socket fd need added into epoll
 * return value: <0: failure  0: successfully
 */
extern int epoll_add(int epollfd, int fd);
 
/*  description: delete fd from epoll to monitor
 *   input args: $epollfd:  epoll fd
 *               $fd:       socket fd need added into epoll
 * return value: <0: failure  0: successfully
 */ 
extern int epoll_del(int epollfd, int fd);
 
/*  description: check socket connect status
 *   input args:
 *               $sock:  socket context pointer
 * return value: 1: connected   0:disconnected
 */
extern int socket_connected(socket_t *sock);
 
/*  description: socket client connect to server
 *   input args:
 *               $sock:  socket context pointer
 * return value: <0: failure   0:ok
 */
extern int socket_connect(socket_t *sock);
 
/*  description: send data from the socket
 *   input args:
 *               $sock :  socket context pointer
 *               $data :  socket send data
 *               $bytes:  socket send data bytes
 * return value: <0: failure   0:ok
 */
extern int socket_send(socket_t *sock, char *data, int bytes);
 
/*  description: receive data from the socket
 *   input args:
 *               $sock :  socket context pointer
 *               $buf  :  socket receive data buffer
 *               $size :  socket receive data buffer size
 *               $timeout: receive data time, <=0 will don't timeout
 * return value: <0: failure   0:ok
 */
#define TIMEOUT_NONE       0
extern int socket_recv(socket_t *sock, char *buf, int size, int timeout);
 
/*+-------------------------------------------------------------------+
 *|                socket utils function                              |
 *+-------------------------------------------------------------------+*/
 
/* description: set socket listen port as reusable, fix port already used bug  */
extern int socket_set_reuseaddr(int sockfd);
 
/* set socket as non-block mode, common socket default work as block mode */
extern int socket_set_nonblock(int sockfd);
 
/* set socket receive and send buffer size in linux kernel space */
extern int socket_set_buffer(int sockfd, int rsize, int ssize);
 
/* set heartbeat keepalive  */
extern int socket_set_keepalive(int sockfd, int keepintvl, int keepcnt);
 
/*  Set open file description count to max */
extern void set_socket_rlimit(void);
 
#endif   /* ----- #ifndef _SOCKET_H_  ----- */