spiochannel.hpp
1.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
/*
* Copyright 2007 Stephen Liu
* For license terms, see the file COPYING along with this library.
*/
#ifndef __spiochannel_hpp__
#define __spiochannel_hpp__
class SP_Session;
class SP_Buffer;
#ifdef WIN32
typedef struct spwin32buffer sp_evbuffer_t;
#else
typedef struct evbuffer sp_evbuffer_t;
#endif
struct iovec;
class SP_IOChannel {
public:
virtual ~SP_IOChannel();
// call by an independence thread, can block
// return -1 : terminate session, 0 : continue
virtual int init( int fd ) = 0;
// run in event-loop thread, cannot block
// return the number of bytes received, or -1 if an error occurred.
virtual int receive( SP_Session * session ) = 0;
// run in event-loop thread, cannot block
// return the number of bytes sent, or -1 if an error occurred.
virtual int transmit( SP_Session * session );
protected:
static sp_evbuffer_t * getEvBuffer( SP_Buffer * buffer );
// returns the number of bytes written, or -1 if an error occurred.
virtual int write_vec( struct iovec * iovArray, int iovSize ) = 0;
};
class SP_IOChannelFactory {
public:
virtual ~SP_IOChannelFactory();
virtual SP_IOChannel * create() const = 0;
};
class SP_DefaultIOChannelFactory : public SP_IOChannelFactory {
public:
SP_DefaultIOChannelFactory();
virtual ~SP_DefaultIOChannelFactory();
virtual SP_IOChannel * create() const;
};
class SP_DefaultIOChannel : public SP_IOChannel {
public:
SP_DefaultIOChannel();
~SP_DefaultIOChannel();
virtual int init( int fd );
virtual int receive( SP_Session * session );
protected:
virtual int write_vec( struct iovec * iovArray, int iovSize );
int mFd;
};
#endif