File: handler.cpp

package info (click to toggle)
gnash 0.8.4-3~lenny1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 20,356 kB
  • ctags: 17,677
  • sloc: cpp: 115,589; ansic: 25,104; xml: 12,898; sh: 10,150; makefile: 5,588; lisp: 2,750; python: 549; exp: 73
file content (299 lines) | stat: -rw-r--r-- 7,172 bytes parent folder | download
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// 
//   Copyright (C) 2008 Free Software Foundation, Inc.
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
// 
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//

#ifdef HAVE_CONFIG_H
#include "gnashconfig.h"
#endif

#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
#include <algorithm>
#include <string>
#include <deque>
#include <list>
#include <map>

#include "log.h"
#include "network.h"
#include "buffer.h"
#include "utility.h"
#include "dsodefs.h" //For DSOEXPORT.

#include "rtmp.h"
#include "rtmp_server.h"
#include "http.h"

using namespace gnash;
using namespace std;
using namespace boost;

namespace gnash
{

map<int, Handler *> DSOEXPORT handlers;

Handler::Handler()
    : _die(false), _netfd(0)
{
//    GNASH_REPORT_FUNCTION;
}

Handler::~Handler()
{
//    GNASH_REPORT_FUNCTION;
    closeConnection();
    _die = true;
    notifyout();
    notifyin();
}

bool
Handler::push(amf::Buffer *data, fifo_e direction)
{
//    GNASH_REPORT_FUNCTION;
    if (direction == Handler::OUTGOING) {
	_outgoing.push(data);
	return true;
    }
    if (direction == Handler::INCOMING) {
	_incoming.push(data);
	return true;
    }
    
    return false;
}

// Push bytes on the outgoing FIFO
bool
Handler::push(gnash::Network::byte_t *data, int nbytes, fifo_e direction)
{
//    GNASH_REPORT_FUNCTION;
    amf::Buffer *ptr = new amf::Buffer;
    ptr->copy(data, nbytes);
    return push(ptr, direction);
}

// Pop the first date element off the FIFO
amf::Buffer *
Handler::pop(fifo_e direction)
{
//    GNASH_REPORT_FUNCTION;
    amf::Buffer *buf = NULL;
    
    if (direction == Handler::OUTGOING) {
	if (_outgoing.size()) {
	    buf = _outgoing.pop();
	    return buf;	
	}
    }
    if (direction == Handler::INCOMING) {
	if (_incoming.size()) {
	    buf = _incoming.pop();
	    return buf;	
	}
    }

    return buf;
}

// Peek at the first data element without removing it
amf::Buffer *
Handler::peek(fifo_e direction)
{
//    GNASH_REPORT_FUNCTION;
    if (direction == Handler::OUTGOING) {
	if (_outgoing.size()) {
	    return _outgoing.peek();
	}
    }
    if (direction == Handler::INCOMING) {
	if (_incoming.size()) {
	    return _incoming.peek();
	}
    }    
    return 0;
}

// Return the size of the queues
size_t
Handler::size(fifo_e direction)
{
//    GNASH_REPORT_FUNCTION;
    if (direction == Handler::OUTGOING) {
	return _outgoing.size();
    }
    if (direction == Handler::INCOMING) {
	return _incoming.size();
    }
    
    return 0;			// we should never actually get to here
}

// Return the size of the queues
void
Handler::clear(fifo_e direction)
{
//    GNASH_REPORT_FUNCTION;
    if (direction == Handler::OUTGOING) {
	_outgoing.clear();
    }
    if (direction == Handler::INCOMING) {
	_incoming.clear();
    }    
}

// Dump internal data.
void
Handler::dump()
{
//    GNASH_REPORT_FUNCTION;
    _incoming.dump();
    _outgoing.dump();    
}

// start the two thread handlers for the queues
bool
Handler::start(thread_params_t *args)
{
    GNASH_REPORT_FUNCTION;
//    Handler *hand = reinterpret_cast<Handler *>(args->handle);
    
    _incoming.setName("Incoming");
    _outgoing.setName("Outgoing");
    
    log_debug(_("Starting Handlers for port %d, tid %ld"),
	      args->port, get_thread_id());

    if (args->port == 4080) {			// FIXME: hack alert!
	boost::thread handler(boost::bind(&httphandler, args));
    }
    if (args->port == RTMP_PORT) {
	boost::thread handler(boost::bind(&rtmp_handler, args));
    }
    
    boost::thread outport(boost::bind(&netout_handler, args));
    boost::thread inport(boost::bind(&netin_handler, args));
// We don't want to wait for the threads to complete, we
// want to return to the main program so it can spawn another
// thread for the next incoming connection.    
// 	inport.join();    
// 	outport.join();
// 	handler.join();
//     if (_die) {
// 	log_debug("Handler done...");
//     }
    return true;
}
    
extern "C" {
void
netin_handler(Handler::thread_params_t *args)
{
//    GNASH_REPORT_FUNCTION;

    Handler *hand = reinterpret_cast<Handler *>(args->handle);

    log_debug("Starting to wait for data in net for fd #%d", args->netfd);
    
    do {
	amf::Buffer *buf = new amf::Buffer;
	size_t ret = hand->readNet(args->netfd, buf->reference(), buf->size(), 1);
	// the read timed out as there was no data, but the socket is still open.
 	if (ret == 0) {
	    log_debug("no data yet for fd #%d, continuing...", args->netfd);
 	    continue;
 	}
	// ret is "no position" when the socket is closed from the other end of the connection,
	// so we're done.
	if ((ret == string::npos) || (ret == 0xffffffff)) {
	    log_debug("socket for fd #%d was closed...", args->netfd);
	    break;
	}
	// We got data. Resize the buffer if necessary.
	if (ret > 0) {
	    if (ret < NETBUFSIZE) {
		buf->resize(ret);
	    }
	    hand->push(buf);
	    hand->notify();
	} else {
	    log_debug("no more data for fd #%d, exiting...", args->netfd);
	    hand->die();
	    break;
	}
    } while (!hand->timetodie());
    // We're done. Notify the other threads the socket is closed, and tell them to die.
    log_debug("Net In handler done for fd #%d...", args->netfd);
    hand->notify();
    hand->closeNet(args->netfd);
//    hand->dump();
}

void
netout_handler(Handler::thread_params_t *args)
{
//    GNASH_REPORT_FUNCTION;
    int ret = 0;
    Handler *hand = reinterpret_cast<Handler *>(args->handle);

    log_debug("Starting to wait for data in que for fd #%d", args->netfd);
    
    do {
	// Don't look for any more packets in the que cause we're done
 	if (hand->timetodie()) {
 	    break;
	}
	hand->waitout();
	while (hand->outsize()) {
	    amf::Buffer *buf = hand->popout();
//	    log_debug("FIXME: got data in Outgoing que");
//	    buf->dump();
//	    ret = hand->writeNet(buf->reference(), buf->size(), 15);
// 	    if (buf->size() != gnash::NETBUFSIZE) {
// 			log_debug("Got smaller packet, size %d", buf->size());		
// 	    }
	    ret = hand->writeNet(args->netfd, buf);
	    delete buf;
	}
    } while (ret > 0);
    hand->die();
    log_debug("Net Out handler done for fd #%d...", args->netfd);
    hand->notifyin();
    hand->closeNet(args->netfd);
#if 0
    map<int, Handler *>::iterator hit = handlers.find(args->netfd);
    if ((*hit).second) {
	log_debug("Removing handle %x for fd #%d: ",
		  (void *)hand), args->netfd;
	handlers.erase(args->netfd);
    }
    delete hand;
#endif
}
    
} // end of extern C

} // end of gnash namespace


// local Variables:
// mode: C++
// indent-tabs-mode: t
// End: