-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClient.cpp
More file actions
120 lines (92 loc) · 2.37 KB
/
Client.cpp
File metadata and controls
120 lines (92 loc) · 2.37 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
#include "Client.hpp"
Client::Client(int fd) : _fd(fd), _is_passed(false), _is_ping(true), _nickname("*") {
struct sockaddr_in client_addr;
socklen_t addr_len = sizeof(client_addr);
getsockname(_fd, (struct sockaddr *)&client_addr, &addr_len);
_ip = inet_ntoa(client_addr.sin_addr);
}
Client::~Client() {}
void Client::setNickname(std::string const &nickname) {
_nickname = nickname;
}
void Client::setUsername(std::string const &username) {
_username = username;
}
void Client::setRealname(std::string const &realname) {
_realname = realname;
}
void Client::setRecvBuf(std::string message) {
_recv_buffer.append(message);
}
void Client::setSendBuf(std::string message) {
_send_buffer.append(message);
}
void Client::addChannel(std::string channel) {
_joined_channels.insert(channel);
}
void Client::delChannel(std::string channel) {
std::set<std::string>::iterator it = _joined_channels.find(channel);
if (it != _joined_channels.end()) {
_joined_channels.erase(it);
}
}
void Client::setPassed(bool flag) {
_is_passed = flag;
}
void Client::setPing(bool flag) {
_is_ping = flag;
}
std::string Client::getNickname() const {
return _nickname;
}
std::string Client::getUsername() const {
return _username;
}
std::string Client::getIp() const {
return _ip;
}
std::string Client::getRealname() const {
return _realname;
}
int Client::getFd() const {
return _fd;
}
std::string Client::getRecvBuf() const {
return _recv_buffer.substr(0, _recv_buffer.find("\r\n") + 2);
}
std::string Client::getSendBuf() const {
return _send_buffer;
}
std::set<std::string> Client::getJoinedChannels() const {
return _joined_channels;
}
bool Client::getPassed() const {
return _is_passed;
}
bool Client::getPing() const {
return _is_ping;
}
void Client::clearRecvBuf() {
_recv_buffer = _recv_buffer.substr(_recv_buffer.find("\r\n") + 2, _recv_buffer.size());
}
void Client::clearSendBuf() {
_send_buffer = _send_buffer.substr(_send_buffer.find("\r\n") + 2, _send_buffer.size());
}
int Client::recvSocket() {
char buf[BUFFER_SIZE] = {0};
if (recv(_fd, buf, sizeof(buf), 0) <= 0) {
return EOF;
}
setRecvBuf(buf);
if (_recv_buffer.find("\r\n") != std::string::npos) {
return END;
}
return CONTINUE;
}
int Client::sendSocket() {
int size = send(_fd, _send_buffer.c_str(), _send_buffer.size(), 0);
_send_buffer.erase(0, size);
if (_send_buffer.size() == 0)
return true;
return false;
}