-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomm_socket.cpp
More file actions
136 lines (122 loc) · 2.98 KB
/
comm_socket.cpp
File metadata and controls
136 lines (122 loc) · 2.98 KB
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
#include"comm_socket.h"
#ifndef LINUX
bool CommonSocket::wsa_inited_ = false;
#endif
CommonSocket::CommonSocket(){
memset(&local_addr_, 0, sizeof(local_addr_));
memset(&peer_addr_, 0, sizeof(peer_addr_));
#ifndef LINUX //in windows system
if (wsa_inited_){
return;
}
WSADATA wsaData;
int iErrorCode;
if (WSAStartup(MAKEWORD(2, 1), &wsaData)) //µ÷ÓÃWindows Sockets DLL
{
printf("Winsock init failed\n");
WSACleanup();
return;
}
wsa_inited_ = true;
#endif
}
CommonSocket::~CommonSocket(){
Close();
}
bool CommonSocket::Bind(const char* listen_ip, uint16_t listen_port){
local_addr_.sin_family = AF_INET;
local_addr_.sin_port = htons(listen_port);
local_addr_.sin_addr.s_addr = inet_addr(listen_ip);
uint32_t option = 1;
if (setsockopt(socket_fd_, SOL_SOCKET, SO_REUSEADDR, (char*)&option, sizeof(option)) < 0){
perror("set socket reuse addr\n");
return false;
}
if (bind(socket_fd_, (sockaddr*)&local_addr_, sizeof(local_addr_)) != 0){
perror("bind socket\n");
return false;
}
return true;
}
bool CommonSocket::SetSendBufSize(int size){
int result = setsockopt(socket_fd_, SOL_SOCKET, SO_SNDBUF, (char*)&size, sizeof(int));
if (result < 0){
perror("setsockopt send buf size\n");
return false;
}
return true;
}
bool CommonSocket::SetRecvBufSize(int size){
int result = setsockopt(socket_fd_, SOL_SOCKET, SO_RCVBUF, (char*)&size, sizeof(int));
if (result < 0){
perror("setsockopt recv buf size\n");
return false;
}
return true;
}
bool CommonSocket::SetBlocking(bool block){
#ifdef LINUX
int opts;
opts = fcntl(socket_fd_, F_GETFL);
if (opts < 0){
perror("fcntl get");
return false;
}
if (block){ //set block mode
opts = opts & (~O_NONBLOCK);
}
else{ // set nonblock mode
opts = opts | O_NONBLOCK;
}
if (fcntl(socket_fd_, F_SETFL, opts) < 0){
perror("fcntl set");
return false;
}
#else
unsigned int uint;
int ret;
if (block){
uint = 0;
ret = ioctlsocket(socket_fd_, FIONBIO, (unsigned long *)&uint);//block
}
else{
uint = 1;
ret = ioctlsocket(socket_fd_, FIONBIO, (unsigned long *)&uint);//non block
}
if (ret == SOCKET_ERROR) {
perror("set non block failed\n");
}
#endif
return true;
}
bool CommonSocket::Connect(const char* peer_ip, uint16_t peer_port){
peer_addr_.sin_family = AF_INET;
peer_addr_.sin_addr.s_addr = inet_addr(peer_ip);
peer_addr_.sin_port = htons(peer_port);
int result = connect(socket_fd_, (sockaddr*)&peer_addr_, sizeof(peer_addr_));
if (result < 0){
perror("Common connect\n");
}
return result == 0;
}
void CommonSocket::Close(){
#ifdef LINUX
close(socket_fd_);
#else
closesocket(socket_fd_);
#endif
}
int CommonSocket::Send(char*buf, int len){
int result = send(socket_fd_, buf, len, 0);
if (result < 0){
perror("Common send to connected peer\n");
}
return result;
}
int CommonSocket::Recv(char* buf, int buf_len){
int recv_bytes = recv(socket_fd_, buf, buf_len, 0);
if (recv_bytes < 0){
perror("Common recv from connected peer \n");
}
return recv_bytes;
}