-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcpconnection.cpp
More file actions
48 lines (40 loc) · 1.54 KB
/
tcpconnection.cpp
File metadata and controls
48 lines (40 loc) · 1.54 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
#include "tcpconnection.hpp"
void TcpConnection::LaunchRead() {
auto res = WSARecv(fd_, &buf_, 1, &bytesTransferred_, &flag_, &overlapped_, nullptr);
auto err = GetLastError();
if (res == 0) {
std::cout << "impossible" << std::endl;
if (bytesTransferred_ != 0) DoOnRead(bytesTransferred_);
} else {
if (err == WSA_IO_PENDING) return;
std::cerr << "launch read failed with error: " << GetLastError() << " res = " << res << " fd = " << (uint64_t)fd_
<< std::endl;
}
}
void TcpConnection::OnRead(DWORD bytes) {
std::cout << "buffer read before = " << buffer << " bytesTransferred_ = " << bytesTransferred_ << " bytes = " << bytes
<< std::endl;
if (bytes != 0) {
DoOnRead(bytes);
} else {
OnClose();
return; // client close normally
}
auto res = WSARecv(fd_, (LPWSABUF)&buf_, 1, &bytesTransferred_, &flag_, &overlapped_, nullptr);
auto err = WSAGetLastError();
if (res == 0) {
if (bytesTransferred_ != 0) DoOnRead(bytesTransferred_);
} else if (res == SOCKET_ERROR) {
if (err == WSAEWOULDBLOCK || err == WSA_IO_PENDING) return;
if (err == WSAECONNABORTED || err == WSAECONNRESET) OnClose();
}
std::cout << "buffer read after = " << buffer << std::endl;
}
void TcpConnection::DoOnRead(DWORD bytes) {
CHECK(reader_, "invalid read callback");
CHECK(bytes <= READ_BUFFER_LEN, "overflow");
reader_(shared_from_this(), std::string_view(buffer, bytes));
memset(buffer, 0, sizeof(buffer));
bytesTransferred_ = 0;
flag_ = 0;
}