ft_irc 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
Client.cpp
[詳解]
1
8#include "Client.hpp"
9#include "Channel.hpp" // Include Channel.hpp
10#include <algorithm>
11#include <ctime> // For time()
12#include <iostream>
13#include <unistd.h>
14
15Client::Client(int fd, const std::string &hostname)
16 : _fd(fd), _hostname(hostname), _isAuthenticated(false), _isRegistered(false), _receiveBuffer(""), _sendBuffer(""),
17 _isMarkedForDisconnect(false), _realname(""), _lastActivityTime(std::time(NULL)),
18 _lastPingSentTime(0) {}
19
21
22// Getters
23int Client::getFd() const { return _fd; }
24
25const std::string &Client::getNickname() const { return _nickname; }
26
27const std::string &Client::getUsername() const { return _username; }
28
29const std::string &Client::getHostname() const { return _hostname; }
30
31bool Client::isAuthenticated() const { return _isAuthenticated; }
32
33bool Client::isRegistered() const { return _isRegistered; }
34
35bool Client::isMarkedForDisconnect() const { return _isMarkedForDisconnect; }
36
37const std::string &Client::getQuitMessage() const { return _quitMessage; }
38
39const std::string &Client::getRealname() const { return _realname; }
40
41const std::string &Client::getSendBuffer() const { return _sendBuffer; }
42
43const std::vector<Channel *> &Client::getChannels() const { return _channels; }
44
46
48
49// Returns the standard IRC prefix for a client (:nickname!username@hostname)
51 if (_nickname.empty() || _username.empty()) {
52 return "";
53 }
54 return ":" + _nickname + "!" + _username + "@" + _hostname;
55}
56
57// Setters
58void Client::setNickname(const std::string &nickname) { _nickname = nickname; }
59
60void Client::setUsername(const std::string &username) { _username = username; }
61
62void Client::setRealname(const std::string &realname) { _realname = realname; }
63
64void Client::setAuthenticated(bool value) { _isAuthenticated = value; }
65
66void Client::setRegistered(bool value) { _isRegistered = value; }
67
69 _isMarkedForDisconnect = true;
70 _quitMessage = message;
71}
72
74 // Check if already in channel to avoid duplicates
75 for (size_t i = 0; i < _channels.size(); ++i) {
76 if (_channels[i] == channel) {
77 return;
78 }
79 }
80 _channels.push_back(channel);
81}
82
84 _channels.erase(std::remove(_channels.begin(), _channels.end(), channel), _channels.end());
85}
86
88
90
91void Client::clearPingSentTime() { _lastPingSentTime = 0; } // PONG応答時にPING状態をリセット
92
93// Appends raw data from recv() to the client's buffer
94void Client::appendBuffer(const std::string &data) { _receiveBuffer += data; }
95
96// Appends data to the client's send buffer
98 if (_sendBuffer.length() + data.length() > MAX_SENDBUFF_SIZE) {
99 // To avoid partial messages, we could try to send a proper ERROR message here,
100 // but the send buffer is already full, so it's unlikely to succeed.
101 // We will mark the client for disconnection. The server will close the socket.
102 markForDisconnect("Output buffer full, disconnecting.");
103 } else {
104 _sendBuffer += data;
105 }
106}
107
108// Removes sent data from the client's send buffer
109void Client::removeSentData(size_t bytes_sent) {
110 if (bytes_sent >= _sendBuffer.length()) {
111 _sendBuffer.clear();
112 } else {
113 _sendBuffer = _sendBuffer.substr(bytes_sent);
114 }
115}
116
117// Extracts a single line ending in \r\n or \n from the buffer
119 size_t pos = _receiveBuffer.find("\r\n");
120 if (pos == std::string::npos) {
121 // Fallback for clients like 'nc' that only send '\n'
122 pos = _receiveBuffer.find("\n");
123 }
124
125 if (pos != std::string::npos) {
126 std::string line = _receiveBuffer.substr(0, pos);
127 // Erase the line and the newline characters from the buffer
128 _receiveBuffer.erase(0, pos + (_receiveBuffer[pos] == '\r' ? 2 : 1));
129 return line;
130 }
131 return "";
132}
133
134// Sends a message to the client, appending the required \r\n
135void Client::sendMessage(const std::string &message) const {
136 // Cast away constness to modify _sendBuffer
137 Client *self = const_cast<Client *>(this);
138 self->appendToSendBuffer(message + "\r\n");
139}
Manages channel members and states.
Manages client connection and state.
T begin(T... args)
チャンネルのメンバーと状態を管理するクラス。
Definition Channel.hpp:25
Represents an IRC client connected to the server.
Definition Client.hpp:25
std::time_t _lastPingSentTime
Timestamp of the last PING sent to the client.
Definition Client.hpp:44
void setNickname(const std::string &nickname)
Sets the client's nickname.
Definition Client.cpp:58
const std::string & getSendBuffer() const
Gets the client's send buffer content.
Definition Client.cpp:41
void setUsername(const std::string &username)
Sets the client's username.
Definition Client.cpp:60
bool isRegistered() const
Checks if the client is registered.
Definition Client.cpp:33
const std::vector< Channel * > & getChannels() const
Gets the list of channels the client is in.
Definition Client.cpp:43
void removeChannel(Channel *channel)
Removes a channel from the client's list of joined channels.
Definition Client.cpp:83
static const size_t MAX_SENDBUFF_SIZE
Maximum size of the send buffer (1MB).
Definition Client.hpp:27
void clearPingSentTime()
Clears the last PING sent time, indicating no PING is currently pending a PONG. This should be called...
Definition Client.cpp:91
const std::string & getRealname() const
Gets the client's real name.
Definition Client.cpp:39
std::string getPrefix() const
Generates the client's IRC prefix (e.g., :nick!user@host).
Definition Client.cpp:50
void setRealname(const std::string &realname)
Sets the client's real name.
Definition Client.cpp:62
void updateActivityTime()
Updates the client's last activity time to the current time.
Definition Client.cpp:87
std::time_t getLastActivityTime() const
Gets the timestamp of the client's last activity.
Definition Client.cpp:45
bool isAuthenticated() const
Checks if the client is authenticated.
Definition Client.cpp:31
void appendBuffer(const std::string &data)
Appends data to the client's receive buffer.
Definition Client.cpp:94
virtual void sendMessage(const std::string &message) const
Sends a message to the client by appending it to the send buffer.
Definition Client.cpp:135
const std::string & getNickname() const
Gets the client's nickname.
Definition Client.cpp:25
virtual ~Client()
Destroys the Client object.
Definition Client.cpp:20
void setLastPingSentTime(std::time_t time)
Sets the timestamp of the last PING sent to the client.
Definition Client.cpp:89
const std::string & getHostname() const
Gets the client's hostname.
Definition Client.cpp:29
std::time_t getLastPingSentTime() const
Gets the timestamp of the last PING sent to the client.
Definition Client.cpp:47
const std::string & getUsername() const
Gets the client's username.
Definition Client.cpp:27
void addChannel(Channel *channel)
Adds a channel to the client's list of joined channels.
Definition Client.cpp:73
void markForDisconnect(const std::string &message)
Marks the client for disconnection with a specified message.
Definition Client.cpp:68
const std::string & getQuitMessage() const
Gets the client's quit message.
Definition Client.cpp:37
Client(int fd, const std::string &hostname)
Constructs a new Client object.
Definition Client.cpp:15
void appendToSendBuffer(const std::string &data)
Appends data to the client's send buffer.
Definition Client.cpp:97
bool isMarkedForDisconnect() const
Checks if the client is marked for disconnection.
Definition Client.cpp:35
std::string readLineFromBuffer()
Reads a single line from the receive buffer.
Definition Client.cpp:118
std::time_t _lastActivityTime
Timestamp of the last activity from the client.
Definition Client.hpp:43
int getFd() const
Gets the client's file descriptor.
Definition Client.cpp:23
void removeSentData(size_t bytes_sent)
Removes a specified number of bytes from the beginning of the send buffer.
Definition Client.cpp:109
void setRegistered(bool value)
Sets the client's registration status.
Definition Client.cpp:66
void setAuthenticated(bool value)
Sets the client's authentication status.
Definition Client.cpp:64
T clear(T... args)
T empty(T... args)
T end(T... args)
T erase(T... args)
T find(T... args)
T push_back(T... args)
T size(T... args)
T substr(T... args)
T time(T... args)