/
Socket.h
91 lines (73 loc) · 2.6 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
/**************************************************************
* This file is part of the MPWide communication library
*
* Written by Derek Groen with thanks going out to Steven Rieder,
* Simon Portegies Zwart, Joris Borgdorff, Hans Blom and Tomoaki Ishiyama.
* for questions, please send an e-mail to:
* djgroennl@gmail.com
* MPWide is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* MPWide is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MPWide. If not, see <http://www.gnu.org/licenses/>.
* **************************************************************/
#ifndef MPW_Socket_class
#define MPW_Socket_class
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <string>
#include <arpa/inet.h>
const int MAXHOSTNAME = 200;
const int MAXCONNECTIONS = 5;
const int WINSIZE = 1*1024*1024;
#define MPWIDE_SOCKET_RDMASK 1
#define MPWIDE_SOCKET_WRMASK 2
class Socket
{
public:
Socket();
virtual ~Socket();
// Server initialization
bool create();
bool bind ( const int port );
bool listen() const;
bool accept();
// Client initialization
bool connect ( const std::string host, const int port );
// Data Transimission
bool send (const char* s, long long int size ) const;
int recv (char* s, long long int size ) const;
// Light-weight, non-blocking
int isend (const char* s, long long int size ) const;
int irecv (char* s, long long int size ) const;
// Check if the socket is readable / writable. Timeout is 2 minutes.
int select_me (int mask) const;
int select_me (int mask, int timeout_val) const;
int select_me (int mask, int timeout_s, int timeout_u) const;
void set_non_blocking(bool);
void set_no_delay(bool);
void setWin(int size);
bool is_valid() const { return m_sock != -1; }
void close();
int getSock() const { return m_sock; }
private:
int m_sock;
sockaddr_in m_addr;
#ifdef MSG_NOSIGNAL
static const int tcp_send_flag = MSG_NOSIGNAL;
#else //OSX Case
static const int tcp_send_flag = SO_NOSIGPIPE;
#endif
};
int Socket_select(int rsock, int wsock, int mask, int timeout_s, int timeout_u);
#endif