-
Notifications
You must be signed in to change notification settings - Fork 2
/
sock_wrap.h
89 lines (71 loc) · 2.52 KB
/
sock_wrap.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
#ifndef _SOCK_WRAP_H_
#define _SOCK_WRAP_H_
#include "platform.h"
#if defined(_WIN32_PLATFROM_)
#include <winsock2.h>
typedef SOCKET HSocket;
#endif
#if defined(_LINUX_PLATFORM_)
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
typedef int HSocket;
#define SOCKET_ERROR (-1)
#define INVALID_SOCKET 0
#endif
typedef struct
{
int block;
int sendbuffersize;
int recvbuffersize;
int lingertimeout;
int recvtimeout;
int sendtimeout;
} socketoption_t;
typedef struct
{
int nbytes;
int nresult;
} transresult_t;
int InitializeSocketEnvironment();
void FreeSocketEnvironment();
void GetAddressFrom(sockaddr_in *addr, const char *ip, int port);
void GetIpAddress(char *ip, sockaddr_in *addr);
bool IsValidSocketHandle(HSocket handle);
int GetLastSocketError();
HSocket SocketOpen(int tcpudp);
void SocketClose(HSocket &handle);
int SocketBlock(HSocket hs, bool bblock);
int SocketTimeOut(HSocket hs, int recvtimeout, int sendtimeout, int lingertimeout);
int SocketBind(HSocket hs, sockaddr_in *addr);
HSocket SocketAccept(HSocket hs, sockaddr_in *addr);
int SocketListen(HSocket hs, int maxconn);
void SocketSend(HSocket hs, const char *ptr, int nbytes, transresult_t &rt);
void SocketRecv(HSocket hs, char *ptr, int nbytes, transresult_t &rt);
void SocketTryRecv(HSocket hs, char *ptr, int nbytes, int milliseconds, transresult_t &rt);
void SocketTrySend(HSocket hs, const char *ptr, int nbytes, int milliseconds, transresult_t &rt);
void SocketClearRecvBuffer(HSocket hs);
class CSockWrap
{
public:
CSockWrap(int tcpudp);
~CSockWrap();
void SetAddress(const char *ip, int port);
void SetAddress(sockaddr_in *addr);
int SetTimeOut(int recvtimeout, int sendtimeout, int lingertimeout);
int SetBufferSize(int recvbuffersize, int sendbuffersize);
int SetBlock(bool bblock);
HSocket GetHandle () { return m_hSocket;}
void Reopen(bool bForceClose);
void Close();
transresult_t Send(void *ptr, int nbytes);
transresult_t Recv(void *ptr, int nbytes );
transresult_t TrySend(void *ptr, int nbytes, int milliseconds);
transresult_t TryRecv(void *ptr, int nbytes, int milliseconds );
void ClearRecvBuffer();
protected:
HSocket m_hSocket;
sockaddr_in m_stAddr;
int m_tcpudp;
};
#endif