This file is indexed.

/usr/include/pion/net/TCPConnection.hpp is in libpion-net-dev 4.0.7+dfsg-3.1ubuntu2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
// ------------------------------------------------------------------
// pion-net: a C++ framework for building lightweight HTTP interfaces
// ------------------------------------------------------------------
// Copyright (C) 2007-2008 Atomic Labs, Inc.  (http://www.atomiclabs.com)
//
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
//

#ifndef __PION_TCPCONNECTION_HEADER__
#define __PION_TCPCONNECTION_HEADER__

#ifdef PION_HAVE_SSL
	#ifdef PION_XCODE
		// ignore openssl warnings if building with XCode
		#pragma GCC system_header
	#endif
	#include <boost/asio/ssl.hpp>
#endif

#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <boost/function.hpp>
#include <boost/function/function1.hpp>
#include <pion/PionConfig.hpp>
#include <string>


namespace pion {	// begin namespace pion
namespace net {		// begin namespace net (Pion Network Library)

///
/// TCPConnection: represents a single tcp connection
/// 
class TCPConnection :
	public boost::enable_shared_from_this<TCPConnection>,
	private boost::noncopyable
{
public:

	/// data type for the connection's lifecycle state
	enum LifecycleType {
		LIFECYCLE_CLOSE, LIFECYCLE_KEEPALIVE, LIFECYCLE_PIPELINED
	};
	
	/// size of the read buffer
	enum { READ_BUFFER_SIZE = 8192 };
	
	/// data type for a function that handles TCP connection objects
	typedef boost::function1<void, boost::shared_ptr<TCPConnection> >	ConnectionHandler;
	
	/// data type for an I/O read buffer
	typedef boost::array<char, READ_BUFFER_SIZE>	ReadBuffer;
	
	/// data type for a socket connection
	typedef boost::asio::ip::tcp::socket			Socket;

#ifdef PION_HAVE_SSL
	/// data type for an SSL socket connection
	typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket>	SSLSocket;

	/// data type for SSL configuration context
	typedef boost::asio::ssl::context								SSLContext;
#else
	class SSLSocket {
	public:
		SSLSocket(boost::asio::io_service& io_service) : m_socket(io_service) {}
		inline Socket& next_layer(void) { return m_socket; }
		inline const Socket& next_layer(void) const { return m_socket; }
		inline Socket& lowest_layer(void) { return m_socket.lowest_layer(); }
		inline const Socket& lowest_layer(void) const { return m_socket.lowest_layer(); }
	private:
		Socket	m_socket;
	};
	typedef int		SSLContext;
#endif

	
	/**
	 * creates new shared TCPConnection objects
	 *
	 * @param io_service asio service associated with the connection
	 * @param ssl_context asio ssl context associated with the connection
	 * @param ssl_flag if true then the connection will be encrypted using SSL 
	 * @param finished_handler function called when a server has finished
	 *                         handling	the connection
	 */
	static inline boost::shared_ptr<TCPConnection> create(boost::asio::io_service& io_service,
														  SSLContext& ssl_context,
														  const bool ssl_flag,
														  ConnectionHandler finished_handler)
	{
		return boost::shared_ptr<TCPConnection>(new TCPConnection(io_service, ssl_context,
																  ssl_flag, finished_handler));
	}
	
	/**
	 * creates a new TCPConnection object
	 *
	 * @param io_service asio service associated with the connection
	 * @param ssl_flag if true then the connection will be encrypted using SSL 
	 */
	explicit TCPConnection(boost::asio::io_service& io_service, const bool ssl_flag = false)
		:
#ifdef PION_HAVE_SSL
		m_ssl_context(io_service, boost::asio::ssl::context::sslv23),
		m_ssl_socket(io_service, m_ssl_context),
		m_ssl_flag(ssl_flag),
#else
		m_ssl_context(0),
		m_ssl_socket(io_service),
		m_ssl_flag(false),
#endif
		m_lifecycle(LIFECYCLE_CLOSE)
	{
		saveReadPosition(NULL, NULL);
	}
	
	/**
	 * creates a new TCPConnection object for SSL
	 *
	 * @param io_service asio service associated with the connection
	 * @param ssl_context asio ssl context associated with the connection
	 */
	TCPConnection(boost::asio::io_service& io_service, SSLContext& ssl_context)
		:
#ifdef PION_HAVE_SSL
		m_ssl_context(io_service, boost::asio::ssl::context::sslv23),
		m_ssl_socket(io_service, ssl_context), m_ssl_flag(true),
#else
		m_ssl_context(0),
		m_ssl_socket(io_service), m_ssl_flag(false), 
#endif
		m_lifecycle(LIFECYCLE_CLOSE)
	{
		saveReadPosition(NULL, NULL);
	}
	
	/// returns true if the connection is currently open
	inline bool is_open(void) const {
		return const_cast<SSLSocket&>(m_ssl_socket).lowest_layer().is_open();
	}
	
	/// closes the tcp socket and cancels any pending asynchronous operations
	inline void close(void) {
		if (m_ssl_socket.lowest_layer().is_open())
			m_ssl_socket.lowest_layer().close();
	}

	/*
	Use close instead; basic_socket::cancel is deprecated for Windows XP.

	/// cancels any asynchronous operations pending on the socket
	inline void cancel(void) {
		m_ssl_socket.lowest_layer().cancel();
	}
	*/
	
	/// virtual destructor
	virtual ~TCPConnection() { close(); }
	
	/**
	 * asynchronously accepts a new tcp connection
	 *
	 * @param tcp_acceptor object used to accept new connections
	 * @param handler called after a new connection has been accepted
	 *
	 * @see boost::asio::basic_socket_acceptor::async_accept()
	 */
	template <typename AcceptHandler>
	inline void async_accept(boost::asio::ip::tcp::acceptor& tcp_acceptor,
							 AcceptHandler handler)
	{
		tcp_acceptor.async_accept(m_ssl_socket.lowest_layer(), handler);
	}

	/**
	 * accepts a new tcp connection (blocks until established)
	 *
	 * @param tcp_acceptor object used to accept new connections
	 * @return boost::system::error_code contains error code if the connection fails
	 *
	 * @see boost::asio::basic_socket_acceptor::accept()
	 */
	inline boost::system::error_code accept(boost::asio::ip::tcp::acceptor& tcp_acceptor)
	{
		boost::system::error_code ec;
		tcp_acceptor.accept(m_ssl_socket.lowest_layer(), ec);
		return ec;
	}
	
	/**
	 * asynchronously connects to a remote endpoint
	 *
	 * @param tcp_endpoint remote endpoint to connect to
	 * @param handler called after a new connection has been established
	 *
	 * @see boost::asio::basic_socket_acceptor::async_connect()
	 */
	template <typename ConnectHandler>
	inline void async_connect(boost::asio::ip::tcp::endpoint& tcp_endpoint,
							  ConnectHandler handler)
	{
		m_ssl_socket.lowest_layer().async_connect(tcp_endpoint, handler);
	}

	/**
	 * asynchronously connects to a (IPv4) remote endpoint
	 *
	 * @param remote_addr remote IP address (v4) to connect to
	 * @param remote_port remote port number to connect to
	 * @param handler called after a new connection has been established
	 *
	 * @see boost::asio::basic_socket_acceptor::async_connect()
	 */
	template <typename ConnectHandler>
	inline void async_connect(const boost::asio::ip::address& remote_addr,
							  const unsigned int remote_port,
							  ConnectHandler handler)
	{
		boost::asio::ip::tcp::endpoint tcp_endpoint(remote_addr, remote_port);
		async_connect(tcp_endpoint, handler);
	}
	
	/**
	 * connects to a remote endpoint (blocks until established)
	 *
	 * @param tcp_endpoint remote endpoint to connect to
	 * @return boost::system::error_code contains error code if the connection fails
	 *
	 * @see boost::asio::basic_socket_acceptor::connect()
	 */
	inline boost::system::error_code connect(boost::asio::ip::tcp::endpoint& tcp_endpoint)
	{
		boost::system::error_code ec;
		m_ssl_socket.lowest_layer().connect(tcp_endpoint, ec);
		return ec;
	}

	/**
	 * connects to a (IPv4) remote endpoint (blocks until established)
	 *
	 * @param remote_addr remote IP address (v4) to connect to
	 * @param remote_port remote port number to connect to
	 * @return boost::system::error_code contains error code if the connection fails
	 *
	 * @see boost::asio::basic_socket_acceptor::connect()
	 */
	inline boost::system::error_code connect(const boost::asio::ip::address& remote_addr,
											 const unsigned int remote_port)
	{
		boost::asio::ip::tcp::endpoint tcp_endpoint(remote_addr, remote_port);
		return connect(tcp_endpoint);
	}
	
	/**
	 * connects to a remote endpoint with hostname lookup
	 *
	 * @param remote_server hostname of the remote server to connect to
	 * @param remote_port remote port number to connect to
	 * @return boost::system::error_code contains error code if the connection fails
	 *
	 * @see boost::asio::basic_socket_acceptor::connect()
	 */
	inline boost::system::error_code connect(const std::string& remote_server,
											 const unsigned int remote_port)
	{
		// query a list of matching endpoints
		boost::system::error_code ec;
		boost::asio::ip::tcp::resolver resolver(m_ssl_socket.lowest_layer().get_io_service());
		boost::asio::ip::tcp::resolver::query query(remote_server,
			boost::lexical_cast<std::string>(remote_port),
			boost::asio::ip::tcp::resolver::query::numeric_service);
		boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query, ec);
		if (ec)
			return ec;

		// try each one until we are successful
		ec = boost::asio::error::host_not_found;
		boost::asio::ip::tcp::resolver::iterator end;
		while (ec && endpoint_iterator != end) {
			boost::asio::ip::tcp::endpoint ep(endpoint_iterator->endpoint());
			++endpoint_iterator;
			ec = connect(ep);
			if (ec)
				close();
		}

		return ec;
	}
	
	/**
	 * asynchronously performs client-side SSL handshake for a new connection
	 *
	 * @param handler called after the ssl handshake has completed
	 *
	 * @see boost::asio::ssl::stream::async_handshake()
	 */
	template <typename SSLHandshakeHandler>
	inline void async_handshake_client(SSLHandshakeHandler handler) {
#ifdef PION_HAVE_SSL
		m_ssl_socket.async_handshake(boost::asio::ssl::stream_base::client, handler);
		m_ssl_flag = true;
#endif
	}

	/**
	 * asynchronously performs server-side SSL handshake for a new connection
	 *
	 * @param handler called after the ssl handshake has completed
	 *
	 * @see boost::asio::ssl::stream::async_handshake()
	 */
	template <typename SSLHandshakeHandler>
	inline void async_handshake_server(SSLHandshakeHandler handler) {
#ifdef PION_HAVE_SSL
		m_ssl_socket.async_handshake(boost::asio::ssl::stream_base::server, handler);
		m_ssl_flag = true;
#endif
	}
	
	/**
	 * performs client-side SSL handshake for a new connection (blocks until finished)
	 *
	 * @return boost::system::error_code contains error code if the connection fails
	 *
	 * @see boost::asio::ssl::stream::handshake()
	 */
	inline boost::system::error_code handshake_client(void) {
		boost::system::error_code ec;
#ifdef PION_HAVE_SSL
		m_ssl_socket.handshake(boost::asio::ssl::stream_base::client, ec);
		m_ssl_flag = true;
#endif
		return ec;
	}

	/**
	 * performs server-side SSL handshake for a new connection (blocks until finished)
	 *
	 * @return boost::system::error_code contains error code if the connection fails
	 *
	 * @see boost::asio::ssl::stream::handshake()
	 */
	inline boost::system::error_code handshake_server(void) {
		boost::system::error_code ec;
#ifdef PION_HAVE_SSL
		m_ssl_socket.handshake(boost::asio::ssl::stream_base::server, ec);
		m_ssl_flag = true;
#endif
		return ec;
	}
	
	/**
	 * asynchronously reads some data into the connection's read buffer 
	 *
	 * @param handler called after the read operation has completed
	 *
	 * @see boost::asio::basic_stream_socket::async_read_some()
	 */
	template <typename ReadHandler>
	inline void async_read_some(ReadHandler handler) {
#ifdef PION_HAVE_SSL
		if (getSSLFlag())
			m_ssl_socket.async_read_some(boost::asio::buffer(m_read_buffer),
										 handler);
		else
#endif		
			m_ssl_socket.next_layer().async_read_some(boost::asio::buffer(m_read_buffer),
										 handler);
	}
	
	/**
	 * asynchronously reads some data into the connection's read buffer 
	 *
	 * @param read_buffer the buffer to read data into
	 * @param handler called after the read operation has completed
	 *
	 * @see boost::asio::basic_stream_socket::async_read_some()
	 */
	template <typename ReadBufferType, typename ReadHandler>
	inline void async_read_some(ReadBufferType read_buffer,
								ReadHandler handler) {
#ifdef PION_HAVE_SSL
		if (getSSLFlag())
			m_ssl_socket.async_read_some(read_buffer, handler);
		else
#endif		
			m_ssl_socket.next_layer().async_read_some(read_buffer, handler);
	}
	
	/**
	 * reads some data into the connection's read buffer (blocks until finished)
	 *
	 * @param ec contains error code if the read fails
	 * @return std::size_t number of bytes read
	 *
	 * @see boost::asio::basic_stream_socket::read_some()
	 */
	inline std::size_t read_some(boost::system::error_code& ec) {
#ifdef PION_HAVE_SSL
		if (getSSLFlag())
			return m_ssl_socket.read_some(boost::asio::buffer(m_read_buffer), ec);
		else
#endif		
			return m_ssl_socket.next_layer().read_some(boost::asio::buffer(m_read_buffer), ec);
	}
	
	/**
	 * reads some data into the connection's read buffer (blocks until finished)
	 *
	 * @param read_buffer the buffer to read data into
	 * @param ec contains error code if the read fails
	 * @return std::size_t number of bytes read
	 *
	 * @see boost::asio::basic_stream_socket::read_some()
	 */
	template <typename ReadBufferType>
	inline std::size_t read_some(ReadBufferType read_buffer,
								 boost::system::error_code& ec)
	{
#ifdef PION_HAVE_SSL
		if (getSSLFlag())
			return m_ssl_socket.read_some(read_buffer, ec);
		else
#endif		
			return m_ssl_socket.next_layer().read_some(read_buffer, ec);
	}
	
	/**
	 * asynchronously reads data into the connection's read buffer until
	 * completion_condition is met
	 *
	 * @param completion_condition determines if the read operation is complete
	 * @param handler called after the read operation has completed
	 *
	 * @see boost::asio::async_read()
	 */
	template <typename CompletionCondition, typename ReadHandler>
	inline void async_read(CompletionCondition completion_condition,
						   ReadHandler handler)
	{
#ifdef PION_HAVE_SSL
		if (getSSLFlag())
			boost::asio::async_read(m_ssl_socket, boost::asio::buffer(m_read_buffer),
									completion_condition, handler);
		else
#endif		
			boost::asio::async_read(m_ssl_socket.next_layer(), boost::asio::buffer(m_read_buffer),
									completion_condition, handler);
	}
			
	/**
	 * asynchronously reads data from the connection until completion_condition
	 * is met
	 *
	 * @param buffers one or more buffers into which the data will be read
	 * @param completion_condition determines if the read operation is complete
	 * @param handler called after the read operation has completed
	 *
	 * @see boost::asio::async_read()
	 */
	template <typename MutableBufferSequence, typename CompletionCondition, typename ReadHandler>
	inline void async_read(const MutableBufferSequence& buffers,
						   CompletionCondition completion_condition,
						   ReadHandler handler)
	{
#ifdef PION_HAVE_SSL
		if (getSSLFlag())
			boost::asio::async_read(m_ssl_socket, buffers,
									completion_condition, handler);
		else
#endif		
			boost::asio::async_read(m_ssl_socket.next_layer(), buffers,
									completion_condition, handler);
	}
	
	/**
	 * reads data into the connection's read buffer until completion_condition
	 * is met (blocks until finished)
	 *
	 * @param completion_condition determines if the read operation is complete
	 * @param ec contains error code if the read fails
	 * @return std::size_t number of bytes read
	 *
	 * @see boost::asio::read()
	 */
	template <typename CompletionCondition>
	inline std::size_t read(CompletionCondition completion_condition,
							boost::system::error_code& ec)
	{
#ifdef PION_HAVE_SSL
		if (getSSLFlag())
			return boost::asio::async_read(m_ssl_socket, boost::asio::buffer(m_read_buffer),
										   completion_condition, ec);
		else
#endif		
			return boost::asio::async_read(m_ssl_socket.next_layer(), boost::asio::buffer(m_read_buffer),
										   completion_condition, ec);
	}
	
	/**
	 * reads data from the connection until completion_condition is met
	 * (blocks until finished)
	 *
	 * @param buffers one or more buffers into which the data will be read
	 * @param completion_condition determines if the read operation is complete
	 * @param ec contains error code if the read fails
	 * @return std::size_t number of bytes read
	 *
	 * @see boost::asio::read()
	 */
	template <typename MutableBufferSequence, typename CompletionCondition>
	inline std::size_t read(const MutableBufferSequence& buffers,
							CompletionCondition completion_condition,
							boost::system::error_code& ec)
	{
#ifdef PION_HAVE_SSL
		if (getSSLFlag())
			return boost::asio::read(m_ssl_socket, buffers,
									 completion_condition, ec);
		else
#endif		
			return boost::asio::read(m_ssl_socket.next_layer(), buffers,
									 completion_condition, ec);
	}
	
	/**
	 * asynchronously writes data to the connection
	 *
	 * @param buffers one or more buffers containing the data to be written
	 * @param handler called after the data has been written
	 *
	 * @see boost::asio::async_write()
	 */
	template <typename ConstBufferSequence, typename WriteHandler>
	inline void async_write(const ConstBufferSequence& buffers, WriteHandler handler) {
#ifdef PION_HAVE_SSL
		if (getSSLFlag())
			boost::asio::async_write(m_ssl_socket, buffers, handler);
		else
#endif		
			boost::asio::async_write(m_ssl_socket.next_layer(), buffers, handler);
	}	
		
	/**
	 * writes data to the connection (blocks until finished)
	 *
	 * @param buffers one or more buffers containing the data to be written
	 * @param ec contains error code if the write fails
	 * @return std::size_t number of bytes written
	 *
	 * @see boost::asio::write()
	 */
	template <typename ConstBufferSequence>
	inline std::size_t write(const ConstBufferSequence& buffers,
							 boost::system::error_code& ec)
	{
#ifdef PION_HAVE_SSL
		if (getSSLFlag())
			return boost::asio::write(m_ssl_socket, buffers,
									  boost::asio::transfer_all(), ec);
		else
#endif		
			return boost::asio::write(m_ssl_socket.next_layer(), buffers,
									  boost::asio::transfer_all(), ec);
	}	
	
	
	/// This function should be called when a server has finished handling
	/// the connection
	inline void finish(void) { if (m_finished_handler) m_finished_handler(shared_from_this()); }

	/// returns true if the connection is encrypted using SSL
	inline bool getSSLFlag(void) const { return m_ssl_flag; }

	/// sets the lifecycle type for the connection
	inline void setLifecycle(LifecycleType t) { m_lifecycle = t; }
	
	/// returns the lifecycle type for the connection
	inline LifecycleType getLifecycle(void) const { return m_lifecycle; }
	
	/// returns true if the connection should be kept alive
	inline bool getKeepAlive(void) const { return m_lifecycle != LIFECYCLE_CLOSE; }
	
	/// returns true if the HTTP requests are pipelined
	inline bool getPipelined(void) const { return m_lifecycle == LIFECYCLE_PIPELINED; }

	/// returns the buffer used for reading data from the TCP connection
	inline ReadBuffer& getReadBuffer(void) { return m_read_buffer; }
	
	/**
	 * saves a read position bookmark
	 *
	 * @param read_ptr points to the next character to be consumed in the read_buffer
	 * @param read_end_ptr points to the end of the read_buffer (last byte + 1)
	 */
	inline void saveReadPosition(const char *read_ptr, const char *read_end_ptr) {
		m_read_position.first = read_ptr;
		m_read_position.second = read_end_ptr;
	}
	
	/**
	 * loads a read position bookmark
	 *
	 * @param read_ptr points to the next character to be consumed in the read_buffer
	 * @param read_end_ptr points to the end of the read_buffer (last byte + 1)
	 */
	inline void loadReadPosition(const char *&read_ptr, const char *&read_end_ptr) const {
		read_ptr = m_read_position.first;
		read_end_ptr = m_read_position.second;
	}

	/// returns an ASIO endpoint for the client connection
	inline boost::asio::ip::tcp::endpoint getRemoteEndpoint(void) const {
		boost::asio::ip::tcp::endpoint remote_endpoint;
		try {
			// const_cast is required since lowest_layer() is only defined non-const in asio
			remote_endpoint = const_cast<SSLSocket&>(m_ssl_socket).lowest_layer().remote_endpoint();
		} catch (boost::system::system_error& /* e */) {
			// do nothing
		}
		return remote_endpoint;
	}

	/// returns the client's IP address
	inline boost::asio::ip::address getRemoteIp(void) const {
		return getRemoteEndpoint().address();
	}

	/// returns the client's port number
	inline unsigned short getRemotePort(void) const {
		return getRemoteEndpoint().port();
	}
	
	/// returns reference to the io_service used for async operations
	inline boost::asio::io_service& getIOService(void) {
		return m_ssl_socket.lowest_layer().get_io_service();
	}

	/// returns non-const reference to underlying TCP socket object
	inline Socket& getSocket(void) { return m_ssl_socket.next_layer(); }
	
	/// returns non-const reference to underlying SSL socket object
	inline SSLSocket& getSSLSocket(void) { return m_ssl_socket; }

	/// returns const reference to underlying TCP socket object
	inline const Socket& getSocket(void) const { return const_cast<SSLSocket&>(m_ssl_socket).next_layer(); }
	
	/// returns const reference to underlying SSL socket object
	inline const SSLSocket& getSSLSocket(void) const { return m_ssl_socket; }

	
protected:
		
	/**
	 * protected constructor restricts creation of objects (use create())
	 *
	 * @param io_service asio service associated with the connection
	 * @param ssl_context asio ssl context associated with the connection
	 * @param ssl_flag if true then the connection will be encrypted using SSL 
	 * @param finished_handler function called when a server has finished
	 *                         handling	the connection
	 */
	TCPConnection(boost::asio::io_service& io_service,
				  SSLContext& ssl_context,
				  const bool ssl_flag,
				  ConnectionHandler finished_handler)
		:
#ifdef PION_HAVE_SSL
		m_ssl_context(io_service, boost::asio::ssl::context::sslv23),
		m_ssl_socket(io_service, ssl_context), m_ssl_flag(ssl_flag),
#else
		m_ssl_context(0),
		m_ssl_socket(io_service), m_ssl_flag(false), 
#endif
		m_lifecycle(LIFECYCLE_CLOSE),
		m_finished_handler(finished_handler)
	{
		saveReadPosition(NULL, NULL);
	}
	

private:

	/// data type for a read position bookmark
	typedef std::pair<const char*, const char*>		ReadPosition;

	
	/// context object for the SSL connection socket
	SSLContext					m_ssl_context;

	/// SSL connection socket
	SSLSocket					m_ssl_socket;

	/// true if the connection is encrypted using SSL
	bool						m_ssl_flag;

	/// buffer used for reading data from the TCP connection
	ReadBuffer					m_read_buffer;
	
	/// saved read position bookmark
	ReadPosition				m_read_position;
	
	/// lifecycle state for the connection
	LifecycleType				m_lifecycle;

	/// function called when a server has finished handling the connection
	ConnectionHandler			m_finished_handler;
};


/// data type for a TCPConnection pointer
typedef boost::shared_ptr<TCPConnection>	TCPConnectionPtr;


}	// end namespace net
}	// end namespace pion

#endif