-
Notifications
You must be signed in to change notification settings - Fork 182
/
Socket.cpp
78 lines (54 loc) · 1.6 KB
/
Socket.cpp
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
/***************************************************/
/*! \class Socket
\brief STK internet socket abstract base class.
This class provides common functionality for TCP and UDP internet
socket server and client subclasses.
by Perry R. Cook and Gary P. Scavone, 1995--2023.
*/
/***************************************************/
#include "Socket.h"
namespace stk {
Socket :: Socket()
{
soket_ = -1;
port_ = -1;
}
Socket :: ~Socket()
{
this->close( soket_ );
#if defined(__OS_WINDOWS__)
WSACleanup();
#endif
}
void Socket :: close( int socket )
{
if ( !isValid( socket ) ) return;
#if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
::close( socket );
#elif defined(__OS_WINDOWS__)
::closesocket( socket );
#endif
}
void Socket :: setBlocking( int socket, bool enable )
{
if ( !isValid( socket ) ) return;
#if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
int tmp = ::fcntl( socket, F_GETFL, 0 );
if ( tmp >= 0 )
tmp = ::fcntl( socket, F_SETFL, enable ? (tmp &~ O_NONBLOCK) : (tmp | O_NONBLOCK) );
#elif defined(__OS_WINDOWS__)
unsigned long non_block = !enable;
ioctlsocket( socket, FIONBIO, &non_block );
#endif
}
int Socket :: writeBuffer(int socket, const void *buffer, long bufferSize, int flags )
{
if ( !isValid( socket ) ) return -1;
return send( socket, (const char *)buffer, bufferSize, flags );
}
int Socket :: readBuffer(int socket, void *buffer, long bufferSize, int flags )
{
if ( !isValid( socket ) ) return -1;
return recv( socket, (char *)buffer, bufferSize, flags );
}
} // stk namespace