/********************************************************************************
|
* 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_ ----- */
|