-
Notifications
You must be signed in to change notification settings - Fork 5
/
tcpserver.h
81 lines (63 loc) · 1.95 KB
/
tcpserver.h
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
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2019-07-11 flybreak the first version
*/
#ifndef _TCPSERVER__
#define _TCPSERVER__
#include <rtthread.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/time.h>
/* configurable options */
#ifndef PKG_USING_TCPSERVER
#define TCPSERVER_NAME "tcpserv"
#define TCPSERVER_STACK_SIZE 4096
#define TCPSERVER_PRIO 12
#define TCPSERVER_SOCKET_MAX 1024
#define TCPSERVER_CLI_NUM 5
#endif
/* run state */
#define TCPSERVER_STATE_INIT 0
#define TCPSERVER_STATE_RUN 1
#define TCPSERVER_STATE_STOP 2
/* event flag */
#define TCPSERVER_EVENT_CONNECT (1 << 0)
#define TCPSERVER_EVENT_DISCONNECT (1 << 1)
#define TCPSERVER_EVENT_RECV (1 << 2)
typedef struct tcpclient *tcpclient_t;
struct tcpserver
{
const char *ip;
rt_uint16_t port;
int sock;
rt_uint8_t state;
fd_set read_set;
fd_set write_set;
int fd_max;
rt_mailbox_t mailbox;
rt_thread_t thread;
tcpclient_t *cli_list;
void (*tcpserver_event_notify)(tcpclient_t client, rt_uint8_t event);
};
struct tcpclient
{
struct tcpserver *server;
int sock;
rt_event_t event;
};
struct tcpserver *tcpserver_create(const char *ip, rt_uint16_t port);
rt_err_t tcpserver_destroy(struct tcpserver *server);
tcpclient_t tcpserver_accept(struct tcpserver *server, rt_int32_t timeout);
rt_err_t tcpserver_close(tcpclient_t client);
rt_size_t tcpserver_recv(tcpclient_t client, void *buf, rt_size_t size, rt_int32_t timeout);
rt_size_t tcpserver_send(tcpclient_t client, void *buf, rt_size_t size, rt_int32_t timeout); /* timeout unrealized */
void tcpserver_set_notify_callback(struct tcpserver *server,
void (*tcpserver_event_notify)(tcpclient_t client, rt_uint8_t event));
#endif