-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientsocket.cpp
144 lines (131 loc) · 3.28 KB
/
clientsocket.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
140
141
142
143
144
#include <monetary.h>
#include "clientsocket.h"
#include "supertcpmanager.h"
#include <cassert>
ClientSocket::ClientSocket(int sockfd, SuperTcpManager &tcpManager, std::function<void(int)> dataReceived, char *buffer, size_t bufferSize, ServerSocket *server)
: AbstractSocket(sockfd, tcpManager),
server(server),
userFunc(dataReceived),
buffer(buffer),
bufferSize(bufferSize),
enableRead(true)
{
SuperTcpManager::printMyDebug("Client constuctor full", sockfd);
tcpManager.addSocket(sockfd, [&](epoll_event ev){
if ((ev.events & EPOLLERR)
|| (ev.events & EPOLLHUP))
{
this->tcpManager.close(this->sockfd);
this->userFunc(0);
}
else if (ev.events & EPOLLIN)
{
this->receiveData();
}
else if (ev.events & EPOLLOUT)
{
this->sendData();
}
});
}
ClientSocket::ClientSocket(int sockfd, SuperTcpManager &tcpManager, ServerSocket* server)
: ClientSocket(sockfd, tcpManager, std::function<void(int)>(), nullptr, 0, server)
{
}
void ClientSocket::receiveData()
{
if (buffer == nullptr || !enableRead)
{
return;
}
bool closeConnection = false;
size_t total = 0;
while (true)
{
std::cout << "read " << bufferSize << " " << total << " " << bufferSize - total << std::endl;
ssize_t count = read(sockfd, buffer + total, bufferSize - total );
if (count == -1)
{
if (errno != EAGAIN)
{
closeConnection = true;
}
break;
}
else if (count == 0)
{
closeConnection = true;
break;
}
total += count;
// buffer[total] = '\0';
assert(total <= bufferSize);
if (total == bufferSize)
{
break;
}
}
enableRead = false;
if (closeConnection)
{
SuperTcpManager::printMyDebug("Close descriptor", sockfd);
userFunc(0);
close();
}
else
{
userFunc(total);
}
}
ClientSocket::~ClientSocket()
{
SuperTcpManager::printMyDebug("Client destructor", sockfd);
}
void ClientSocket::close()
{
if (server == nullptr)
{
tcpManager.removeSocket(this);
}
else
{
server->removeClient(this);
}
}
void ClientSocket::send(const std::string &message)
{
countSended = ::send(sockfd, message.c_str(), message.size(), 0);
if (countSended != message.size())
{
if (countSended < 0)
{
countSended = 0;
}
this->message = message;
tcpManager.setFdOptions(sockfd, Epoll::DEFAULT_EVENTS | EPOLLOUT);
}
}
void ClientSocket::setFunction(std::function<void (int)> callback)
{
userFunc = callback;
}
void ClientSocket::setBuffer(char *buffer)
{
this->buffer = buffer;
}
void ClientSocket::setBufferSize(size_t bufferSize)
{
this->bufferSize = bufferSize;
}
void ClientSocket::sendData()
{
ssize_t writeBytes = ::send(sockfd, message.data() + sizeof(char) * countSended, message.size() - countSended, 0);
if (writeBytes > 0)
{
countSended += writeBytes;
}
if (writeBytes <= 0 || countSended == message.size())
{
tcpManager.setFdOptions(sockfd, Epoll::DEFAULT_EVENTS);
}
}