forked from joyieldInc/predixy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocket.h
106 lines (99 loc) · 2.08 KB
/
Socket.h
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
105
106
/*
* predixy - A high performance and full features proxy for redis.
* Copyright (C) 2017 Joyield, Inc. <[email protected]>
* All rights reserved.
*/
#ifndef _PREDIXY_SOCKET_H_
#define _PREDIXY_SOCKET_H_
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/uio.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include "Exception.h"
class Socket
{
public:
DefException(SocketCallFail);
DefException(InvalidAddr);
DefException(AddrLenTooShort);
enum ClassType
{
RawType,
ListenType,
AcceptType,
ConnectType,
CustomType = 1024
};
enum StatusCode
{
Normal = 0,
None,
End,
IOError,
EventError,
ExceptError,
CustomStatus = 100
};
public:
Socket(int fd = -1);
Socket(int domain, int type, int protocol = 0);
Socket(const Socket&) = delete;
Socket(const Socket&&) = delete;
~Socket();
void attach(int fd);
void detach();
void close();
bool setNonBlock(bool val = true);
bool setTcpNoDelay(bool val = true);
bool setTcpKeepAlive(int interval);
int read(void* buf, int cnt);
int write(const void* buf, int cnt);
int writev(const struct iovec* vecs, int cnt);
int classType() const
{
return mClassType;
}
int fd() const
{
return mFd;
}
bool good() const
{
return mStatus == Normal;
}
int status() const
{
return mStatus;
}
const char* statusStr() const;
void setStatus(int st)
{
mStatus = st;
}
int getEvent() const
{
return mEvts;
}
void setEvent(int evts)
{
mEvts = evts;
}
public:
static int socket(int domain, int type, int protocol);
static void getFirstAddr(const char* addr, int type, int protocol, sockaddr* res, socklen_t* len);
protected:
int mClassType;
private:
int mFd;
int mEvts;
int mStatus;
};
#endif