Fawkes API
Fawkes Development Version
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
All
Classes
Namespaces
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Groups
Pages
socket.h
1
2
/***************************************************************************
3
* socket.h - Fawkes socket base class
4
*
5
* Created: Thu Nov 09 12:55:25 2006
6
* Copyright 2006 Tim Niemueller [www.niemueller.de]
7
*
8
****************************************************************************/
9
10
/* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; either version 2 of the License, or
13
* (at your option) any later version. A runtime exception applies to
14
* this software (see LICENSE.GPL_WRE file mentioned below for details).
15
*
16
* This program is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU Library General Public License for more details.
20
*
21
* Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22
*/
23
24
#ifndef __NETCOMM_SOCKET_SOCKET_H_
25
#define __NETCOMM_SOCKET_SOCKET_H_
26
27
#include <core/exception.h>
28
#include <core/exceptions/software.h>
29
30
#include <sys/socket.h>
31
#include <sys/types.h>
32
#include <netinet/in.h>
33
// just to be safe nobody else can do it
34
#include <sys/signal.h>
35
36
#ifdef POLL_IN
37
# undef POLL_IN
38
#endif
39
#ifdef POLL_OUT
40
# undef POLL_OUT
41
#endif
42
#ifdef POLL_PRI
43
# undef POLL_PRI
44
#endif
45
#ifdef POLL_RDHUP
46
# undef POLL_RDHUP
47
#endif
48
#ifdef POLL_ERR
49
# undef POLL_ERR
50
#endif
51
#ifdef POLL_HUP
52
# undef POLL_HUP
53
#endif
54
55
56
namespace
fawkes {
57
58
class
SocketException
:
public
Exception
59
{
60
public
:
61
SocketException
(
const
char
*msg,
int
_errno
);
62
SocketException
(
const
char
*msg);
63
};
64
65
class
Socket
66
{
67
public
:
68
69
static
const
short
POLL_IN
;
70
static
const
short
POLL_OUT
;
71
static
const
short
POLL_PRI
;
72
static
const
short
POLL_RDHUP
;
73
static
const
short
POLL_ERR
;
74
static
const
short
POLL_HUP
;
75
static
const
short
POLL_NVAL
;
76
77
Socket
(
int
domain,
int
type,
int
protocol,
float
timeout
= 0.f);
78
Socket
(
Socket
&socket);
79
virtual
~Socket
();
80
81
virtual
void
connect
(
const
char
*hostname,
const
unsigned
short
int
port);
82
virtual
void
connect
(
struct
sockaddr *addr_port,
unsigned
int
struct_size);
83
84
virtual
void
bind
(
const
unsigned
short
int
port);
85
virtual
void
bind
(
const
unsigned
short
int
port,
86
const
char
*hostname);
87
88
virtual
void
listen
(
int
backlog = 1);
89
virtual
Socket
*
accept
();
90
virtual
void
close
();
91
virtual
bool
available
();
92
93
virtual
size_t
read
(
void
*buf,
size_t
count,
bool
read_all =
true
);
94
virtual
void
write
(
const
void
*buf,
size_t
count);
95
virtual
void
send
(
void
*buf,
size_t
buf_len);
96
virtual
void
send
(
void
*buf,
size_t
buf_len,
97
const
struct
sockaddr *to_addr, socklen_t addr_len);
98
virtual
size_t
recv
(
void
*buf,
size_t
buf_len);
99
virtual
size_t
recv
(
void
*buf,
size_t
buf_len,
100
struct
sockaddr *from_addr, socklen_t *addr_len);
101
102
/** Clone socket.
103
* This method has to be implemented by subclass to correctly clone the instance.
104
* @return cloned socket
105
*/
106
virtual
Socket
*
clone
() = 0;
107
108
virtual
short
poll
(
int
timeout
= -1,
short
what =
POLL_IN
|
POLL_HUP
|
POLL_PRI
|
POLL_RDHUP
);
109
110
virtual
bool
listening
();
111
112
virtual
unsigned
int
mtu
();
113
114
/** Accept connection.
115
* This method works like accept() but it ensures that the returned socket is of
116
* the given type.
117
* @return socket to client
118
*/
119
template
<
class
SocketType>
120
SocketType *
accept
();
121
122
protected
:
123
Socket
();
124
125
int
sock_fd
;
126
float
timeout
;
127
struct ::sockaddr_in *
client_addr
;
128
unsigned
int
client_addr_len
;
129
130
};
131
132
133
template
<
class
SocketType>
134
SocketType *
135
Socket::accept
()
136
{
137
Socket
*s =
accept
();
138
if
(SocketType *ts = dynamic_cast<SocketType *>(s)) {
139
return
ts;
140
}
else
{
141
delete
s;
142
throw
TypeMismatchException(
"Socket types do not match"
);
143
}
144
}
145
146
}
// end namespace fawkes
147
148
#endif
src
libs
netcomm
socket
socket.h
Generated by
1.8.1.2