-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsocket.hpp
49 lines (44 loc) · 1.12 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
#ifndef _SOCKET_HPP
#define _SOCKET_HPP
#include <sys/types.h>
#include <sys/socket.h>
#include <sstream>
#include "exceptions.hpp"
class Socket {
public:
Socket();
Socket(const Socket& s);
void create(int type);
size_t read(char *buf, size_t size);
void read_assure(char *buf, size_t size);
void write(const char *buf, size_t size);
void listen(in_addr_t bindip, in_port_t bindport);
void close();
bool is_ready();
Socket* accept();
in_addr_t get_peer();
void terminate() { terminate_ = true; }
private:
Socket(int s);
int s_;
bool created_;
int type_;
bool terminate_;
};
class SocketNotCreated : public GenericException {
public:
SocketNotCreated() : GenericException("socket is not created") {}
};
class SocketOsError : public GenericException {
public:
SocketOsError(std::string funcName, int eno) {
std::ostringstream s;
s << "Function execution error: " << funcName << "(); errno=" << eno;
GenericException(s.str());
}
};
class SocketNotEnoughData : public GenericException {
public:
SocketNotEnoughData() : GenericException("Read not enough data from socket") {}
};
#endif