-
Notifications
You must be signed in to change notification settings - Fork 131
/
tcpshm_client.h
236 lines (214 loc) · 9.1 KB
/
tcpshm_client.h
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
MIT License
Copyright (c) 2018 Meng Rao <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <string>
#include <array>
#include <strings.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include "tcpshm_conn.h"
namespace tcpshm {
template<class Derived, class Conf>
class TcpShmClient
{
public:
using Connection = TcpShmConnection<Conf>;
using LoginMsg = LoginMsgTpl<Conf>;
using LoginRspMsg = LoginRspMsgTpl<Conf>;
protected:
TcpShmClient(const std::string& client_name, const std::string& ptcp_dir)
: ptcp_dir_(ptcp_dir) {
strncpy(client_name_, client_name.c_str(), sizeof(client_name_) - 1);
mkdir(ptcp_dir_.c_str(), 0755);
client_name_[sizeof(client_name_) - 1] = 0;
conn_.init(ptcp_dir.c_str(), client_name_);
}
~TcpShmClient() {
Stop();
}
// connect and login to server, may block for a short time
// return true if success
bool Connect(bool use_shm,
const char* server_ipv4,
uint16_t server_port,
const typename Conf::LoginUserData& login_user_data) {
if(!conn_.IsClosed()) {
static_cast<Derived*>(this)->OnSystemError("already connected", 0);
return false;
}
conn_.TryCloseFd();
const char* error_msg;
if(!server_name_) {
std::string last_server_name_file = std::string(ptcp_dir_) + "/" + client_name_ + ".lastserver";
server_name_ = (char*)my_mmap<ServerName>(last_server_name_file.c_str(), false, &error_msg);
if(!server_name_) {
static_cast<Derived*>(this)->OnSystemError(error_msg, errno);
return false;
}
strncpy(conn_.GetRemoteName(), server_name_, sizeof(ServerName));
}
MsgHeader sendbuf[1 + (sizeof(LoginMsg) + 7) / 8];
sendbuf[0].size = sizeof(MsgHeader) + sizeof(LoginMsg);
sendbuf[0].msg_type = LoginMsg::msg_type;
sendbuf[0].ack_seq = 0;
LoginMsg* login = (LoginMsg*)(sendbuf + 1);
strncpy(login->client_name, client_name_, sizeof(login->client_name));
strncpy(login->last_server_name, server_name_, sizeof(login->last_server_name));
login->use_shm = use_shm;
login->client_seq_start = login->client_seq_end = 0;
login->user_data = login_user_data;
if(server_name_[0] &&
(!conn_.OpenFile(use_shm, &error_msg) ||
!conn_.GetSeq(&sendbuf[0].ack_seq, &login->client_seq_start, &login->client_seq_end, &error_msg))) {
static_cast<Derived*>(this)->OnSystemError(error_msg, errno);
return false;
}
int fd;
if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
static_cast<Derived*>(this)->OnSystemError("socket", errno);
return false;
}
struct timeval timeout;
timeout.tv_sec = 10;
timeout.tv_usec = 0;
if(setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout)) < 0) {
static_cast<Derived*>(this)->OnSystemError("setsockopt SO_RCVTIMEO", errno);
close(fd);
return false;
}
if(setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout)) < 0) {
static_cast<Derived*>(this)->OnSystemError("setsockopt SO_RCVTIMEO", errno);
close(fd);
return false;
}
int yes = 1;
if(Conf::TcpNoDelay && setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) < 0) {
static_cast<Derived*>(this)->OnSystemError("setsockopt TCP_NODELAY", errno);
close(fd);
return false;
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
inet_pton(AF_INET, server_ipv4, &(server_addr.sin_addr));
server_addr.sin_port = htons(server_port);
bzero(&(server_addr.sin_zero), 8);
if(connect(fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
static_cast<Derived*>(this)->OnSystemError("connect", errno);
close(fd);
return false;
}
sendbuf[0].template ConvertByteOrder<Conf::ToLittleEndian>();
login->ConvertByteOrder();
int ret = send(fd, sendbuf, sizeof(sendbuf), MSG_NOSIGNAL);
if(ret != sizeof(sendbuf)) {
static_cast<Derived*>(this)->OnSystemError("send", ret < 0 ? errno : 0);
close(fd);
return false;
}
MsgHeader recvbuf[1 + (sizeof(LoginRspMsg) + 7) / 8];
ret = recv(fd, recvbuf, sizeof(recvbuf), 0);
if(ret != sizeof(recvbuf)) {
static_cast<Derived*>(this)->OnSystemError("recv", ret < 0 ? errno : 0);
close(fd);
return false;
}
LoginRspMsg* login_rsp = (LoginRspMsg*)(recvbuf + 1);
recvbuf[0].template ConvertByteOrder<Conf::ToLittleEndian>();
login_rsp->ConvertByteOrder();
if(recvbuf[0].size != sizeof(MsgHeader) + sizeof(LoginRspMsg) || recvbuf[0].msg_type != LoginRspMsg::msg_type ||
login_rsp->server_name[0] == 0) {
static_cast<Derived*>(this)->OnSystemError("Invalid LoginRsp", 0);
close(fd);
return false;
}
if(login_rsp->status != 0) {
if(login_rsp->status == 1) { // seq number mismatch
sendbuf[0].template ConvertByteOrder<Conf::ToLittleEndian>();
login->ConvertByteOrder();
static_cast<Derived*>(this)->OnSeqNumberMismatch(sendbuf[0].ack_seq,
login->client_seq_start,
login->client_seq_end,
recvbuf[0].ack_seq,
login_rsp->server_seq_start,
login_rsp->server_seq_end);
}
else {
static_cast<Derived*>(this)->OnLoginReject(login_rsp);
}
close(fd);
return false;
}
login_rsp->server_name[sizeof(login_rsp->server_name) - 1] = 0;
// check if server name has changed
if(strncmp(server_name_, login_rsp->server_name, sizeof(ServerName)) != 0) {
conn_.Release();
strncpy(server_name_, login_rsp->server_name, sizeof(ServerName));
strncpy(conn_.GetRemoteName(), server_name_, sizeof(ServerName));
if(!conn_.OpenFile(use_shm, &error_msg)) {
static_cast<Derived*>(this)->OnSystemError(error_msg, errno);
close(fd);
return false;
}
conn_.Reset();
}
fcntl(fd, F_SETFL, O_NONBLOCK);
int64_t now = static_cast<Derived*>(this)->OnLoginSuccess(login_rsp);
conn_.Open(fd, recvbuf[0].ack_seq, now);
return true;
}
// we need to PollTcp even if using shm
void PollTcp(int64_t now) {
if(!conn_.IsClosed()) {
MsgHeader* head = conn_.TcpFront(now);
if(head) static_cast<Derived*>(this)->OnServerMsg(head);
}
if(conn_.TryCloseFd()) {
int sys_errno;
const char* reason = conn_.GetCloseReason(&sys_errno);
static_cast<Derived*>(this)->OnDisconnected(reason, sys_errno);
}
}
// only for using shm
void PollShm() {
MsgHeader* head = conn_.ShmFront();
if(head) static_cast<Derived*>(this)->OnServerMsg(head);
}
// stop the connection and close files
void Stop() {
if(server_name_) {
my_munmap<ServerName>(server_name_);
server_name_ = nullptr;
}
conn_.Release();
}
// get the connection reference which can be kept by user as long as TcpShmClient is not destructed
Connection& GetConnection() {
return conn_;
}
private:
char client_name_[Conf::NameSize];
using ServerName = std::array<char, Conf::NameSize>;
char* server_name_ = nullptr;
std::string ptcp_dir_;
Connection conn_;
};
} // namespace tcpshm