wangzhengquan
2020-08-06 203df24a403a8c0cd8e93d0f33eaf10de2788969
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "dgram_mod_socket.h"
#include "dmod_socket.h"
 
typedef struct dgram_mod_socket_t {
  DModSocket *m_socket;
  
} dgram_mod_socket_t;
 
/**
 * 创建socket
 * @return socket地址
*/
void *dgram_mod_open_socket() {
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *)calloc(1, sizeof(dgram_mod_socket_t));
    // socket->mod = (socket_mod_t)mod;
    socket->m_socket = new DModSocket;
    return (void *)socket;
}
 
/**
 * 关闭socket
 * @return 0 成功, 其他值 失败的错误码
*/
int dgram_mod_close_socket(void * _socket) {
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    delete socket->m_socket;
    free(_socket);
    return 0;
}
 
/**
 * 绑定端口到socket, 如果不绑定则系统自动分配一个
 * @return 0 成功, 其他值 失败的错误码
*/
int dgram_mod_bind(void * _socket, int port) {
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    return socket->m_socket->bind(port);
}
 
/**
 * 强制绑定端口到socket, 适用于程序非正常关闭的情况下,重启程序绑定原来还没释放的key
 * @return 0 成功, 其他值 失败的错误码
*/
int dgram_mod_force_bind(void * _socket, int port) {
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    return socket->m_socket->force_bind(port);
}
/**
 * 发送信息
 * @port 发送给谁
 * @return 0 成功, 其他值 失败的错误码
 */
int dgram_mod_sendto(void *_socket, const void *buf, const int size, const int port){
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    return socket->m_socket->sendto(buf, size,port);
}
 
// 发送信息超时返回。 @sec 秒 , @nsec 纳秒
int dgram_mod_sendto_timeout(void *_socket, const void *buf, const int size, const int port, int sec, int nsec){
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    struct timespec timeout = {sec, nsec};
    return socket->m_socket->sendto_timeout(buf, size, port, &timeout);
}
 
// 发送信息立刻返回。
int dgram_mod_sendto_nowait(void *_socket, const void *buf, const int size, const int port){
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    return socket->m_socket->sendto_nowait(buf, size,port);
}
 
/**
 * 接收信息
 * @port 从谁哪里收到的信息
 * @return 0 成功, 其他值 失败的错误码
*/
int dgram_mod_recvfrom(void *_socket, void **buf, int *size, int *port) {
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    return socket->m_socket->recvfrom(buf, size, port);
}
// 接受信息超时返回。 @sec 秒 , @nsec 纳秒
int dgram_mod_recvfrom_timeout(void *_socket, void **buf, int *size, int *port, int sec, int nsec){
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    struct timespec timeout = {sec, nsec};
    return socket->m_socket->recvfrom_timeout(buf, size, port, &timeout);
}
 
int dgram_mod_recvfrom_nowait(void *_socket, void **buf, int *size, int *port){
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    return socket->m_socket->recvfrom_nowait(buf, size, port);
}
 
/**
 * 发送请求信息并等待接收应答
 * @port 发送给谁
 * @return 0 成功, 其他值 失败的错误码
*/
int dgram_mod_sendandrecv(void * _socket, const void *send_buf, const int send_size, const int port, void **recv_buf, int *recv_size){
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    return socket->m_socket->sendandrecv(send_buf, send_size, port, recv_buf, recv_size);
}
// 超时返回。 @sec 秒 , @nsec 纳秒
int dgram_mod_sendandrecv_timeout(void * _socket, const void *send_buf, const int send_size, const int port, void **recv_buf, int *recv_size, int sec, int nsec){
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    struct timespec timeout = {sec, nsec};
    return socket->m_socket->sendandrecv_timeout(send_buf, send_size, port, recv_buf, recv_size, &timeout);
}
int dgram_mod_sendandrecv_nowait(void * _socket, const void *send_buf, const int send_size, const int port, void **recv_buf, int *recv_size) {
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    return socket->m_socket->sendandrecv_nowait(send_buf, send_size, port, recv_buf, recv_size);
}
 
 
/**
 * 启动bus
 * 
 * @return 0 成功, 其他值 失败的错误码
*/
int  dgram_mod_start_bus(void * _socket) {
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    return socket->m_socket->start_bus();
}
 
/**
 * 订阅指定主题
 * @topic 主题
 * @size 主题长度
 * @port 总线端口
 */
int  dgram_mod_sub(void * _socket, void *topic, int size, int port){
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    return socket->m_socket->sub(topic,  size,  port);
}
// 超时返回。 @sec 秒 , @nsec 纳秒
int  dgram_mod_sub_timeout(void * _socket, void *topic, int size, int port, int sec, int nsec){
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    struct timespec timeout = {sec, nsec};
    return socket->m_socket->sub_timeout(topic,  size,  port, &timeout);
}
int  dgram_mod_sub_nowait(void * _socket, void *topic, int size, int port){
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    return socket->m_socket->sub_nowait(topic,  size,  port);
}
 
 
 
/**
 * 取消订阅指定主题
 * @topic 主题
 * @size 主题长度
 * @port 总线端口
 */
int  dgram_mod_desub(void * _socket, void *topic, int size, int port){
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    return socket->m_socket->desub(topic,  size,  port);
}
// 超时返回。 @sec 秒 , @nsec 纳秒
int  dgram_mod_desub_timeout(void * _socket, void *topic, int size, int port, int sec, int nsec){
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    struct timespec timeout = {sec, nsec};
    return socket->m_socket->desub_timeout(topic,  size,  port, &timeout);
}
int  dgram_mod_desub_nowait(void * _socket, void *topic, int size, int port){
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    return socket->m_socket->desub_nowait(topic,  size,  port);
}
 
 
 
/**
 * 发布主题
 * @topic 主题
 * @content 主题内容
 * @port 总线端口
 */
int  dgram_mod_pub(void * _socket, void *topic, int topic_size, void *content, int content_size, int port){
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    return socket->m_socket->pub(topic, topic_size, content, content_size, port);
}
//  超时返回。 @sec 秒 , @nsec 纳秒
int  dgram_mod_pub_timeout(void * _socket, void *topic, int topic_size, void *content, int content_size, int port, int sec, int nsec){
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    struct timespec timeout = {sec, nsec};
    return socket->m_socket->pub_timeout(topic, topic_size, content, content_size, port, &timeout);
}
int  dgram_mod_pub_nowait(void * _socket, void *topic, int topic_size, void *content, int content_size, int port){
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    return socket->m_socket->pub_nowait(topic, topic_size, content, content_size, port);
}
 
 
/**
 * 获取soket端口号
 */
int dgram_mod_get_port(void * _socket) {
    dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
    return socket->m_socket->get_port();
}
 
 
/**
 * 释放存储接收信息的buf
 */
void dgram_mod_free(void *buf) {
    free(buf);
}