-
Notifications
You must be signed in to change notification settings - Fork 0
/
UdpSocket.cpp
84 lines (76 loc) · 1.86 KB
/
UdpSocket.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
#include "UdpSocket.h"
#include "EventLoop.h"
#include "SocketsOps.h"
#include "Channel.h"
#include "Socket.h"
#include "Timestamp.h"
#include <netinet/in.h>
#include <sys/socket.h>
using namespace simple_muduo;
UdpSocket::UdpSocket(EventLoop* loop, std::string ip, uint16_t port)
: loop_(loop)
, ip_(ip)
, port_(port)
// , is_connect_peer_(false)
{}
UdpSocket::~UdpSocket()
{
int fd = channel_->fd();
if(fd > 0)
{
sockets::close(fd);
}
}
void UdpSocket::start()
{
fd_ = ::socket(AF_INET, SOCK_DGRAM, 0);
int opt = 1;
setsockopt(fd_, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, static_cast<socklen_t>(opt));
setsockopt(fd_, SOL_SOCKET, SO_REUSEPORT, (char*)&opt, static_cast<socklen_t>(opt));
struct sockaddr_in addr = {0};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(ip_.c_str());
addr.sin_port = ::htons(port_);
int ret = ::bind(fd_, (struct sockaddr*)&addr, sizeof addr);
if(ret != 0)
{
printf("bind dup erro\n");
}
{
struct sockaddr addr;
struct sockaddr_in* addr_v4;
socklen_t addr_len = sizeof(addr);
if(0 == ::getsockname(fd_, &addr, &addr_len))
{
if(addr.sa_family == AF_INET)
{
addr_v4 = (sockaddr_in*)& addr;
port_ = ntohs(addr_v4->sin_port);
}
}
}
channel_.reset(new Channel(loop_, fd_));
channel_->setReadCallback([this](Timestamp time){this->handleRead();});
channel_->enableReading();
}
int UdpSocket::send(const uint8_t *buf, size_t len, const struct sockaddr_in &peer_addr)
{
int ret =
sendto(fd_, (const char*)buf, len, 0,(struct sockaddr *)&peer_addr_, sizeof(peer_addr_));
if(ret < 0)
{
printf("send eror\n");
return -1;
}
return ret;
}
void UdpSocket::handleRead()
{
struct sockaddr_in peerAddr;
unsigned int nAddrLen=sizeof(peerAddr);
int recvLen = ::recvfrom(fd_, buf_,kBufferSize, 0, (struct sockaddr*)&peerAddr, &nAddrLen);
if(read_callbak_)
{
read_callbak_(buf_,recvLen,&peerAddr);
}
}