[go: up one dir, main page]

Menu

[r99]: / trunk / socket.h  Maximize  Restore  History

Download this file

150 lines (114 with data), 3.6 kB

  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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* $Id$
*
* EHS is a library for embedding HTTP(S) support into a C++ application
*
* Copyright (C) 2004 Zachary J. Hansen
*
* Code cleanup, new features and bugfixes: Copyright (C) 2010 Fritz Elfert
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation;
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* This can be found in the 'COPYING' file.
*
*/
#ifndef SOCKET_H
#define SOCKET_H
#ifdef _MSC_VER
# pragma warning(disable : 4786)
#endif
#ifdef HAVE_WINSOCK2_H
# include <winsock2.h>
#endif
#ifdef HAVE_WINDOWS_H
# include <windows.h>
#endif
#ifdef HAVE_TIME_H
# include <time.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
#include <cstdio>
#ifdef _WIN32
typedef unsigned long in_addr_t;
typedef size_t socklen_t;
# define sleep(seconds) (Sleep(seconds * 1000))
#endif
extern const char *net_strerror();
#ifdef _WIN32
# define net_errno WSAGetLastError()
#else
# define INVALID_SOCKET -1
# define net_errno errno
#endif
#include "networkabstraction.h"
/// plain socket implementation of NetworkAbstraction
class Socket : public NetworkAbstraction {
private:
Socket(const Socket &);
Socket & operator=(const Socket &);
protected:
/**
* Constructs a new Socket, connected to a client.
* @param fd The socket descriptor of this connection.
* @param peer The peer address of this socket
*/
Socket(ehs_socket_t fd, sockaddr_in *peer);
public:
/**
* Default constructor
*/
Socket();
virtual void RegisterBindHelper(PrivilegedBindHelper *helper);
virtual void Init(int port);
virtual ~Socket();
virtual void SetBindAddress(const char * bindAddress);
virtual ehs_socket_t GetFd() const { return m_fd; }
virtual int Read(void *buf, int bufsize);
virtual int Send(const void *buf, size_t buflen, int flags = 0);
virtual void Close();
virtual NetworkAbstraction *Accept();
/// Determines, whether the underlying socket is secure.
/// @return false, because this instance does not use SSL.
virtual bool IsSecure() const { return false; }
virtual void ThreadCleanup() { }
protected:
int GetPort() const;
std::string GetAddress() const;
std::string GetPeer() const;
/// The file descriptor of the socket on which this connection came in
ehs_socket_t m_fd;
/// Stores the peer address of the current connection
sockaddr_in m_peer;
/// Stores the bind address
sockaddr_in m_bindaddr;
/// Our bind helper
PrivilegedBindHelper *m_pBindHelper;
};
#endif // SOCKET_H