-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseconnection.cpp
More file actions
57 lines (44 loc) · 1.99 KB
/
baseconnection.cpp
File metadata and controls
57 lines (44 loc) · 1.99 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
#include "baseconnection.h"
void BaseConnection::subscribe(CONNECTIONS type, BaseLogger* observer){
//pushing observer to their type
observers[type].push_back(observer);
}
BaseConnection::BaseConnection(int port){
//binds and listens
BindAndListen(port);
}
void BaseConnection::BindAndListen(int port){
std::cout << to_yellow("Atempting to listen to PORT: ") << to_white(std::to_string(port)) << std::endl;
//ConnectionServer Address
//sockadde is the data type that stores network information
sockaddr_in ConnectionServerAddress;
ConnectionServerAddress.sin_family = AF_INET;
ConnectionServerAddress.sin_port = htons(port);// port to Network byte order
ConnectionServerAddress.sin_addr.s_addr = INADDR_ANY;// accept any ip address
//binding
std::cout << to_yellow("ATTEMPTING TO BIND TO PORT: ") << port << std::endl;
if(bind(
ConnectionServerSocket,
(struct sockaddr*) &ConnectionServerAddress,
sizeof(ConnectionServerAddress)
) < 0){
std::cout << to_red("COULD NOT LISTEN ON PORT: ") << port<< std::endl;
close(ConnectionServerSocket);
throw std::runtime_error("error while binding for incoming clients");
}
std::cout << to_green("BINDING to port ") + to_white(std::to_string(port)) +to_green(" COMPLETE") << std::endl;
std::cout << to_yellow("ATTEMPTING TO LISTEN ON PORT: ") << port << std::endl;
//listening for incoming conns
if(listen(ConnectionServerSocket, 5) < 0){
std::cout << to_red("COULD NOT LISTEN ON PORT: ")<< port << std::endl;
close(ConnectionServerSocket);
throw std::runtime_error("error while listening for incoming clients");
};
std::cout << to_green("NOW LISTENING on port ") + to_white(std::to_string(port)) +to_green(" COMPLETE") << std::endl;
}
void BaseConnection::notify(CONNECTIONS connection, Log log){
for(BaseLogger* x: observers[connection]){
x->onEvent(log);
}
}
BaseConnection::~BaseConnection(){}