socket.h
2.12 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
#ifndef __SOCKET_H__
#define __SOCKET_H__
#include <string>
#include <memory>
#include <exception>
namespace lemoce {
///
/// Brief Classe base que contem apenas o File Descriptor do socket.
///
class Socket
{
public:
Socket();
int getFd() const;
void setFd(int fd);
bool isClosed();
void close();
protected:
int fd; //!< Brief File Descriptor do socket
};
///
/// Brief Classe que abstrai as operações de recv e send do TCP/IP.
///
class ClientSocket : public Socket {
public:
ClientSocket();
ClientSocket(std::string remote, int port);
void setRemoteHost(std::string);
std::string getRemoteHost() const;
void setRemotePort(int port);
int getRemotePort() const;
void setLocalHost(std::string);
std::string getLocalHost() const;
void setLocalPort(int);
int getLocalPort() const;
void setBufferSize(int);
int getBufferSize() const;
void connect();
std::unique_ptr<std::string> read();
int read(char msg[], const size_t n);
void write(const std::string& message);
void write(const char * msg, const size_t n);
private:
int bufferSize;
void fillLocalHostPort();
std::string remoteHost; //!< Brief host do servidor
int remotePort; //!< Brief porta do servidor
std::string localHost; //!< Brief host do client
int localPort; //!< Brief porta do client
};
///
/// Brief Classe que abstrai as operações de accept
///
class ServerSocket : public Socket
{
public:
ServerSocket(int port);
void listen();
void accept(ClientSocket & client);
private:
static int BACKLOG; //!< Brief numero de backlog possíveis
int localPort; //!< Brief porta local do servidor
};
///
/// Brief Class embarca o código de erro
///
class SocketException : public std::exception
{
public:
SocketException(int err_number);
std::string getMessage() const;
virtual const char * what() const throw();
int getErrno() const;
private:
std::string message;
int error;
};
}
#endif /* __SOCKET_H__ */