chen
2024-09-20 292ed46c6066d47289f1330b1c2bcc6d74761f95
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
/*
 * Copyright (c) 2015-2020 QXSI. All rights reserved.
 */
 
#ifndef QXWZ_USER_IMPL_H__
#define QXWZ_USER_IMPL_H__
 
#ifdef __cplusplus
extern "C" {
#endif
 
#include "qxwz_types.h"
/*
 * Log
 */
qxwz_int32_t qxwz_printf(const qxwz_char_t *fmt, ...);
/*
 * Mutex lock
 */
typedef struct {
    qxwz_void_t *mutex_entity;
} qxwz_mutex_t;
 
qxwz_int32_t qxwz_mutex_init(qxwz_mutex_t *mutex);
qxwz_int32_t qxwz_mutex_trylock(qxwz_mutex_t *mutex);
qxwz_int32_t qxwz_mutex_lock(qxwz_mutex_t *mutex);
qxwz_int32_t qxwz_mutex_unlock(qxwz_mutex_t *mutex);
qxwz_int32_t qxwz_mutex_destroy(qxwz_mutex_t *mutex);
/*
 * Memory
 */
qxwz_void_t *qxwz_malloc(qxwz_uint32_t size);
qxwz_void_t *qxwz_calloc(qxwz_uint32_t nmemb, qxwz_uint32_t size);
qxwz_void_t *qxwz_realloc(qxwz_void_t *ptr, qxwz_uint32_t size);
qxwz_void_t qxwz_free(qxwz_void_t *ptr);
 
#define QXWZ_ERR_SOCK_DISCONNECTED      -1  /* socket is closed by peer */
#define QXWZ_ERR_SOCK_CONNECT           -2  /* something wrong happens when try to connect to the server */
#define QXWZ_ERR_SOCK_SEND              -3  /* something wrong happens when try to send data to server */
#define QXWZ_ERR_SOCK_RECV              -4  /* something wrong happens when try to receive data from server */
#define QXWZ_ERR_SOCK_UNKNOWN           -5  /* unknown error happens to socket */
 
typedef struct {
    const qxwz_char_t *hostname;
    qxwz_uint16_t port;
} qxwz_sock_host_t;
 
 
/*
 * Create a socket handler.
 *
 * @return:
 *    >= 0: succeeds, identifier of the socket handler;
 *      -1: failed;
 */
qxwz_int32_t qxwz_sock_create();
 
/*
 * Establish a connection to the server specified by the argument `serv` in a blocking way.
 * @param[in]  sock: socket handler;
 * @param[in]  serv: the server info, see `qxwz_sock_host_t`;
 *
 * @return:
 *      0: connecting succeeded;
 *     -1: connecting failed;
 */
qxwz_int32_t qxwz_sock_connect(qxwz_int32_t sock, qxwz_sock_host_t *serv);
 
/*
 * Send data in a non-blocking way.
 * @param[in]  sock: socket handler;
 * @param[in]  send_buf: pointer of the buffer to be sent;
 * @param[in]  len: length of the buffer;
 *
 * @return:
 *    >=0: the number of bytes sent;
 *     -1: fails;
 */
qxwz_int32_t qxwz_sock_send(qxwz_int32_t sock, const qxwz_uint8_t *send_buf, qxwz_uint32_t len);
 
/*
 * Receive data in a non-blocking way.
 * @param[in]  sock: socket handler;
 * @param[in]  recv_buf: pointer of the buffer to recv the data;
 * @param[in]  len: length of the buffer;
 *
 * @return:
 *     >0: the number of bytes received;
 *      0: connection is closed by peer
 *     -1: no data received this time
 *     -2: error occur;
 */
qxwz_int32_t qxwz_sock_recv(qxwz_int32_t sock, qxwz_uint8_t *recv_buf, qxwz_uint32_t len);
 
 
/*
 * Close a socket handler.
 * @param[in]  sock: socket handler.
 *
 * @return:
 *      0: succeeds;
 *     -1: fails;
 */
qxwz_int32_t qxwz_sock_close(qxwz_int32_t sock);
 
#ifdef __cplusplus
}
#endif
 
#endif