-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocket.hpp
65 lines (49 loc) · 1.44 KB
/
Socket.hpp
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
#pragma once
#include <string>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <poll.h>
#include <unistd.h>
#include <vector>
#include "Logger.hpp"
#include "PollableFileDescriptor.hpp"
#include "Client.hpp"
class Socket : public ReadFileDescriptor {
public:
Socket(
const std::string& interface,
int port,
const Logger& logger
);
~Socket();
const std::string& getInterface( void ) const;
int getPort( void ) const;
int getFileDescriptor( void ) const;
std::string toString( void ) const;
void startListening( void );
// Override the doRead function to open a connection with a Client
// instead of actually reading from the file descriptor
ssize_t doRead( void );
// Override the readingDone function to mark the descriptor for
// reading again
void readingDone( void );
private:
int port;
std::string interface;
sockaddr_in socket_struct;
int fd = -1; // Default to avoid close() calls on nonexistent fds
Logger l;
public:
class Exception : public std::exception {
private:
const std::string _message;
public:
Exception(const Socket& socket, const std::string& reason) :
_message("Exception for socket " + socket.toString() + " : " + reason) { }
Exception(const std::string& reason) :
_message("Socket exception : " + reason) { }
virtual const char* what() const throw() {
return _message.c_str();
}
};
};