-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp_server.cpp
140 lines (102 loc) · 2.66 KB
/
udp_server.cpp
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
/**
* @file udp_server.cpp
* @brief UDP服务类的实现
* @author hrh <[email protected]>
* @version 1.0.0
* @date 2011-12-13
*
* @verbatim
* ============================================================================
* Copyright (c) Shenzhen Landun technology Co.,Ltd. 2011
* All rights reserved.
*
* Use of this software is controlled by the terms and conditions found in the
* license agreenment under which this software has been supplied or provided.
* ============================================================================
*
* @endverbatim
*
*/
#include <stdio.h>
#include <string.h>
#include "udp_server.h"
#include "socket.h"
#include "ldczn_protocol.h"
UdpServer::UdpServer()
{
}
UdpServer::~UdpServer()
{
}
void UdpServer::BroadcastToClient()
{
int broadcast_sock; //广播回复socket
broadcast_sock = Socket::CreateUdp();
if (broadcast_sock < 0) {
printf("func: %s, Create Udp failed\n", __func__);
return;
}
struct packet_search_ack packet;
char auth_code[sizeof(packet.head)];
fill_std_header(&packet.head, ENCODING_TYPE_RAW, auth_code,
MESSAGE_TYPE_ACK, sizeof(packet));
packet.ack.id = 0;
packet.ack.type = REQ_TYPE_SEARCH;
packet.ack.sts = ACK_SUCCESS;
Socket::SendBroadcast(broadcast_sock, 39001, (char *)&packet, sizeof(packet));
Socket::Close(broadcast_sock);
}
void UdpServer::Init()
{
server_sock = Socket::CreateUdp();
if (server_sock < 0) {
printf("func: %s, Create udp failed\n", __func__);
return;
}
if (Socket::Bind(server_sock, 39000) < 0) {
printf("func: %s, Bind port 39000 failed\n", __func__);
return;
}
}
void UdpServer::Process(char *buf, int len)
{
struct header_std *head = (struct header_std *)buf;
if (len < sizeof(struct header_std)) {
printf("len of recv buf is too short\n");
return;
}
struct payload_req *req = (struct payload_req *)(buf + sizeof(*head));
if ((req->type & 0xFF000000) == REQ_TYPE_SEARCH) {
BroadcastToClient();
}
printf("func: %s, %d\n", __func__, req->type);
}
#define RECV_BUF_LENGTH 1024
void UdpServer::Run()
{
Init();
printf("start udp server \n");
int ret = -1;
int port = 0;
int bytes = -1;
char addr[32];
char buf[RECV_BUF_LENGTH];
while (!IsTerminated()) {
ret = Socket::CheckRead(server_sock, 5000);
if (ret < 0) {
printf("func: %s, Check socket error\n", __func__);
break;
}
if (ret > 0) {
if ((bytes = Socket::RecvFrom(server_sock, buf, addr, &port)) < 0) {
printf("udpserver recvfrom error\n");
break;
}
printf("func %s, recv from %s:%d\n", __func__, addr, port);
Process(buf, bytes);
}
}
Socket::Close(server_sock);
Terminate();
printf("func: %s exit\n", __func__);
}