-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientCreation.cpp
52 lines (42 loc) · 1.1 KB
/
ClientCreation.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
#include "Server.hpp"
void Server::RemoveClient(int fd)
{
close(fd);
// delete clients[fd]
clients.erase(fd);
for (std::vector<struct pollfd>::iterator it = this->fds.begin(); it != fds.end(); it++)
{
if (it->fd == fd)
{
fds.erase(it);
break;
}
}
}
void Server::CreateClient(int fd)
{
Client *a = new Client(fd, 0);
clients.insert(std::pair<int, Client>(fd, *a));
struct pollfd fd_poll;
fd_poll.fd = fd;
fd_poll.events = POLLIN;
fd_poll.revents = 0;
fds.push_back(fd_poll);
SendMessage(fd, "Please enter your PASSWORD using PASS: \r\n");
}
void Server::AcceptClient()
{
struct sockaddr_in addr;
addr.sin_port = htons(this->port);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
socklen_t len = sizeof(addr);
int new_fd = accept(fd_SerSocket, (struct sockaddr *)&addr, &len);
if (new_fd == -1)
{
perror("accept");
throw std::string("accept client failed");
}
CreateClient(new_fd);
std::cout << "client " << new_fd << " accepted" << std::endl;
}