00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00046
#ifndef CCXX_SOCKETPORT_H_
00047
#define CCXX_SOCKETPORT_H_
00048
00049
#ifndef CCXX_ADDRESS_H_
00050
#include <cc++/address.h>
00051
#endif
00052
00053
#ifndef CCXX_SOCKET_H_
00054
#include <cc++/socket.h>
00055
#endif
00056
00057
#ifdef CCXX_NAMESPACES
00058
namespace ost {
00059
#endif
00060
00061 class __EXPORT
SocketPort;
00062 class __EXPORT
SocketService;
00063
00083 class __EXPORT
SocketPort :
public Socket,
public TimerPort
00084 {
00085
private:
00086
SocketPort *next, *prev;
00087
SocketService *service;
00088
#ifndef WIN32
00089
struct timeval porttimer;
00090
#ifdef USE_POLL
00091
struct pollfd * ufd;
00092
#endif
00093
#else
00094
HANDLE event;
00095
#endif
00096
bool detect_pending;
00097
bool detect_output;
00098
bool detect_disconnect;
00099
00100
friend class SocketService;
00101
00102
protected:
00111
SocketPort(
SocketService *svc,
TCPSocket &tcp);
00112
#ifdef CCXX_IPV6
00113
SocketPort(
SocketService *svc,
TCPV6Socket &tcp);
00114
#endif
00115
00124
SocketPort(
SocketService *svc,
const IPV4Address &ia,
tpport_t port);
00125
#ifdef CCXX_IPV6
00126
SocketPort(
SocketService *svc,
const IPV6Address &ia,
tpport_t port);
00127
#endif
00128
00142
SocketPort(
SocketService *svc,
const IPV4Host &ih,
tpport_t port);
00143
#ifdef CCXX_IPV6
00144
SocketPort(
SocketService *svc,
const IPV6Host &ih,
tpport_t port);
00145
#endif
00146
00152
void attach(
SocketService* svc );
00153
00154
00159
virtual ~
SocketPort();
00160
00165
void setDetectPending(
bool );
00166
00170 bool getDetectPending(
void )
const
00171
{
return detect_pending; }
00172
00177
void setDetectOutput(
bool );
00178
00182 bool getDetectOutput(
void )
const
00183
{
return detect_output; }
00184
00189 virtual void expired(
void)
00190 {
return;};
00191
00196 virtual void pending(
void)
00197 {
return;};
00198
00203 virtual void output(
void)
00204 {
return;};
00205
00210 virtual void disconnect(
void)
00211 {
return;};
00212
00223 Error connect(
const IPV4Address &ia,
tpport_t port);
00224
#ifdef CCXX_IPV6
00225
Error connect(
const IPV6Address &ia,
tpport_t port);
00226
#endif
00227
00237 inline ssize_t send(
const void *buf, size_t len)
00238 {
return _IORET64 ::send(so, (
const char *)buf, _IOLEN64 len, 0);};
00239
00248 inline ssize_t receive(
void *buf, size_t len)
00249 {
return _IORET64 ::recv(so, (
char *)buf, _IOLEN64 len, 0);};
00250
00259 inline ssize_t peek(
void *buf, size_t len)
00260 {
return _IORET64 ::recv(so, (
char *)buf, _IOLEN64 len, MSG_PEEK);};
00261
00262
public:
00270
void setTimer(
timeout_t timeout = 0);
00271
00279
void incTimer(
timeout_t timeout);
00280 };
00281
00294 class __EXPORT
SocketService :
public Thread,
private Mutex
00295 {
00296
private:
00297
#ifndef WIN32
00298
fd_set connect;
00299
int iosync[2];
00300
int hiwater;
00301
#else
00302
00303
class Sync;
00304 Sync* sync;
00305
#endif
00306
int volatile count;
00307
SocketPort*
volatile first, *last;
00308
00314
void attach(
SocketPort *port);
00320
void detach(
SocketPort *port);
00321
00325
void run(
void);
00326
00327
friend class SocketPort;
00328
00329
protected:
00335 virtual void onUpdate(
unsigned char buf)
00336 {
return;};
00337
00343 virtual void onEvent(
void)
00344 {
return;};
00345
00353 virtual void onCallback(SocketPort *port)
00354 {
return;};
00355
00356
public:
00367
void update(
unsigned char flag = 0xff);
00368
00377
SocketService(
int pri = 0, size_t stack = 0,
const char *
id = NULL);
00378
00383
virtual ~
SocketService();
00384
00391 inline int getCount(
void)
const
00392
{
return count;};
00393 };
00394
00395
#ifdef CCXX_NAMESPACES
00396
}
00397
#endif
00398
00399
#endif
00400