summaryrefslogtreecommitdiff
path: root/pjlib/src/pj++/ioqueue.hpp
blob: 5ecb34ced1c59c21d7ff6e9aff57ce38ad70887c (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* $Header: /pjproject/pjlib/src/pj++/ioqueue.hpp 4     8/24/05 10:29a Bennylp $ */
#ifndef __PJPP_IOQUEUE_H__
#define __PJPP_IOQUEUE_H__

#include <pj++/sock.hpp>
#include <pj++/pool.hpp>
#include <pj++/types.hpp>
#include <pj/ioqueue.h>

class PJ_IOQueue;

class PJ_IOQueue_Event_Handler
{
public:
    virtual ~PJ_IOQueue_Event_Handler()
    {
    }

    pj_ioqueue_key_t* get_key() const
    {
	return key_;
    }

protected:
    //
    // Override this to get notification from I/O Queue
    //
    virtual void on_read_complete(pj_ssize_t bytes_read)
    {
    }

    virtual void on_write_complete(pj_ssize_t bytes_sent)
    {
    }

    virtual void on_accept_complete(int status)
    {
    }

    virtual void on_connect_complete(int status)
    {
    }

protected:
    PJ_IOQueue_Event_Handler()
	: ioqueue_(NULL), key_(NULL)
    {
    }

private:
    PJ_IOQueue *ioqueue_;
    pj_ioqueue_key_t *key_;

    static void read_complete_cb(pj_ioqueue_key_t *key, pj_ssize_t bytes_read)
    {
	PJ_IOQueue_Event_Handler *handler = 
	    (PJ_IOQueue_Event_Handler*)pj_ioqueue_get_user_data(key);
	handler->on_read_complete(bytes_read);
    }

    static void write_complete_cb(pj_ioqueue_key_t *key, pj_ssize_t bytes_sent);
    static void accept_complete_cb(pj_ioqueue_key_t *key, int status);
    static void connect_complete_cb(pj_ioqueue_key_t *key, int status);

    friend class PJ_IOQueue;
};


class PJ_IOQueue
{
    typedef pj_ioqueue_t *B_;

public:
    typedef pj_ioqueue_key_t Key;

    enum Operation
    {
	OP_NONE	     = PJ_IOQUEUE_OP_NONE,
	OP_READ	     = PJ_IOQUEUE_OP_READ,
	OP_RECV_FROM = PJ_IOQUEUE_OP_RECV_FROM,
	OP_WRITE     = PJ_IOQUEUE_OP_WRITE,
	OP_SEND_TO   = PJ_IOQUEUE_OP_SEND_TO,
#if PJ_HAS_TCP
	OP_ACCEPT    = PJ_IOQUEUE_OP_ACCEPT,
	OP_CONNECT   = PJ_IOQUEUE_OP_CONNECT,
#endif
    };

    enum Status
    {
	IS_PENDING = PJ_IOQUEUE_PENDING
    };

    static PJ_IOQueue *create(PJ_Pool *pool, pj_size_t max_fd)
    {
	return (PJ_IOQueue*) pj_ioqueue_create(pool->pool_(), max_fd);
    }

    operator B_()
    {
	return (pj_ioqueue_t*)(PJ_IOQueue*)this;
    }

    pj_ioqueue_t *ioq_()
    {
	return (B_)this;
    }

    void destroy()
    {
	pj_ioqueue_destroy(this->ioq_());
    }

    Key *register_handle(PJ_Pool *pool, pj_oshandle_t hnd, void *user_data)
    {
	return pj_ioqueue_register(pool->pool_(), this->ioq_(), hnd, user_data);
    }

    Key *register_socket(PJ_Pool *pool, pj_sock_t hnd, void *user_data)
    {
	return pj_ioqueue_register(pool->pool_(), this->ioq_(), (pj_oshandle_t)hnd, user_data);
    }

    pj_status_t unregister(Key *key)
    {
	return pj_ioqueue_unregister(this->ioq_(), key);
    }

    void *get_user_data(Key *key)
    {
	return pj_ioqueue_get_user_data(key);
    }

    int poll(Key **key, pj_ssize_t *bytes_status, Operation *op, const PJ_Time_Val *timeout)
    {
	return pj_ioqueue_poll(this->ioq_(), key, bytes_status, (pj_ioqueue_operation_e*)op, timeout);
    }

#if PJ_HAS_TCP
    pj_status_t connect(Key *key, const pj_sockaddr_t *addr, int addrlen)
    {
	return pj_ioqueue_connect(this->ioq_(), key, addr, addrlen);
    }

    pj_status_t accept(Key *key, PJ_Socket *sock, pj_sockaddr_t *local, pj_sockaddr_t *remote, int *addrlen)
    {
	return pj_ioqueue_accept(this->ioq_(), key, &sock->get_handle(), local, remote, addrlen);
    }
#endif

    int read(Key *key, void *buf, pj_size_t len)
    {
	return pj_ioqueue_read(this->ioq_(), key, buf, len);
    }

    int recvfrom(Key *key, void *buf, pj_size_t len, pj_sockaddr_t *addr, int *addrlen)
    {
	return pj_ioqueue_recvfrom(this->ioq_(), key, buf, len, addr, addrlen);
    }

    int write(Key *key, const void *data, pj_size_t len)
    {
	return pj_ioqueue_write(this->ioq_(), key, data, len);
    }

    int sendto(Key *key, const void *data, pj_size_t len, const pj_sockaddr_t *addr, int addrlen)
    {
	return pj_ioqueue_sendto(this->ioq_(), key, data, len, addr, addrlen);
    }
};

#endif	/* __PJPP_IOQUEUE_H__ */