-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCBaseSocket.h
87 lines (71 loc) · 2.93 KB
/
CBaseSocket.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
/*
Copyright [2010] [Richard Bross]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//! base class for socket
//
// Created: Richard A. Bross
//
#ifndef _CBASESOCKET_H
#define _CBASESOCKET_H
#include "XPlat.h"
#include "CXPlatCriticalSection.h"
#include <sys/un.h>
// This define really has nothing to do with kernel 2.4. It is here because
// Linux sockets behave a bit differently from Windows and some other platforms.
// Specifically, on other platforms if you do a ::select call on a socket,
// and close the socket in another thread, the ::select will unblock. Not on Linux.
// So you have to timeout on Linux and then check the socket itself.
#define KERNEL_2_4
//! Base socket class implements low level Berkley socket functionality
class CBaseSocket
{ //! constructor
public:
CBaseSocket();
virtual ~CBaseSocket();
//! methods
public:
virtual BOOL Create(int af, //! Address family. Only tested with AF_INET.
int type, //! Stream or datagram (SOCK_STREAM or SOCK_DRAM)
int protocol, //! Usually 0 for inet sockets
UINT nPort = 0, //! Port
LPCSTR lpAddr = NULL, //! Address. For inet in the standard xxx.xxx.xxx.xxx format, for AF_UNIX the path
BOOL bReuse = FALSE, //! Allow reuse of this socket
BOOL bBind = TRUE, //! Default to bind the socket. Not needed if this is the result of an ::accept
int iLinger = -1, //! -1 = system default, 0 = DONT_LINGER, else the timeout
BOOL bNoTcpDelay = FALSE);//! FALSE = system default, TRUE = disable Nagle algorithm and send partial data
//! Make this a blocking socket
virtual BOOL SetBlockingMode(BOOL bBlock = TRUE);
//! Close socket
virtual void CloseSocket();
//! Return a socket address structure with info currently bound to the socket
virtual struct sockaddr_in *GetSocketName();
//! Return the socket address as a string
virtual const char *GetSocketAddressAsString();
//! Return the last create error
virtual int GetCreateError();
//! Get socket type
virtual int GetSocketType();
protected:
//! Unprotected close socket - called by CloseSocket
virtual void UnprotectedCloseSocket();
protected:
SOCKET sSocket;
CXPlatCriticalSection cCriticalSocket;
BOOL bBlocking;
struct sockaddr_in sTAddr;
int iCreateError;
string sUnixPath;
int iAF; // Address family
private:
char szAddress[16];
};
#endif //! !_CSOCKET_H