-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocklib.h
More file actions
85 lines (68 loc) · 1.61 KB
/
socklib.h
File metadata and controls
85 lines (68 loc) · 1.61 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
#pragma once
#include <memory>
#include <string>
#include <vector>
#include <iostream>
#include "pool.h"
typedef std::vector<char> ByteString;
class Address
{
public:
Address(): _data{0} {}
Address(const std::string& name, int port);
struct AddressData
{
char data[32];
} _data;
};
class Socket
{
public:
enum Family
{
INET,
INET6
};
enum Type
{
STREAM,
DGRAM
};
enum Error {
SOCKLIB_ETIMEDOUT,
};
Socket();
Socket(Family family, Type type);
~Socket();
Socket(const Socket& other) = delete;
Socket(Socket&& other);
int GetLastError();
int SetNonBlockingMode(bool shouldBeNonBlocking);
int SetTimeout(float seconds);
void Create(Family family, Type type);
int Bind(const Address& address);
int Listen(int backlog=16);
Socket Accept();
void AcceptInto(Socket& socket);
int Connect(const Address& address);
PoolView RecvIntoPool(unsigned int max_len);
int Recv(char* buffer, int size);
int Recv(ByteString& buffer);
int RecvFrom(char* buffer, int size, Address& src);
size_t Send(const char* data, size_t len);
size_t SendTo(const char* buffer, size_t len, const Address& dest);
size_t SendAll(const char* data, size_t len);
size_t SendAll(const ByteString& data);
static void native_destroy(Socket& socket);
bool _has_socket;
int _last_error;
struct SocketData
{
char data[32];
} _data;
};
void SockLibInit();
void SockLibShutdown();
ByteString to_bytestring(const char* msg, size_t len);
std::ostream& operator<<(std::ostream& s, const ByteString& b);
std::ostream& operator<<(std::ostream& s, const Address& a);