void WebClientApp::setup()
{
	gl::enable( GL_TEXTURE_2D );
	
	mFont			= Font( "Georgia", 24 );
	mFrameRate		= 0.0f;
	mFullScreen		= false;
	mHost			= "libcinder.org";
	mPort			= 80;
	
	mHttpRequest = HttpRequest( "GET", "/", HttpVersion::HTTP_1_0 );
	mHttpRequest.setHeader( "Host", mHost );
	mHttpRequest.setHeader( "Accept", "*/*" );
	mHttpRequest.setHeader( "Connection", "close" );

	mParams = params::InterfaceGl::create( "Params", ivec2( 200, 150 ) );
	mParams->addParam( "Frame rate",	&mFrameRate,					"", true );
	mParams->addParam( "Full screen",	&mFullScreen ).key( "f" );
	mParams->addParam( "Host",			&mHost );
	mParams->addParam( "Port",			&mPort,							"min=0 max=65535 step=1 keyDecr=p keyIncr=P" );
	mParams->addButton( "Write", bind(	&WebClientApp::write, this ),	"key=w" );
	mParams->addButton( "Quit", bind(	&WebClientApp::quit, this ),	"key=q" );
	
	mClient = TcpClient::create( io_service() );
	mClient->connectConnectEventHandler( &WebClientApp::onConnect, this );
	mClient->connectErrorEventHandler( &WebClientApp::onError, this );
	mClient->connectResolveEventHandler( &WebClientApp::onResolve, this );
}
void HttpClientApp::setup()
{
	gl::enable( GL_TEXTURE_2D );
	
	mBytesRead		= 0;
	mContentLength	= 0;
	mFont			= Font( "Georgia", 24 );
	mFrameRate		= 0.0f;
	mFullScreen		= false;
	mHost			= "127.0.0.1";
	mIndex			= 0;
	
	mHttpRequest = HttpRequest( "GET", "/", HttpVersion::HTTP_1_1 );
	mHttpRequest.setHeader( "Host",			mHost );
	mHttpRequest.setHeader( "Accept",		"*/*" );

	mParams = params::InterfaceGl::create( "Params", ivec2( 200, 150 ) );
	mParams->addParam( "Frame rate",	&mFrameRate,			"", true );
	mParams->addParam( "Full screen",	&mFullScreen ).key( "f" );
	mParams->addParam( "Image index",	&mIndex,				"min=0 max=3 step=1 keyDecr=i keyIncr=I" );
	mParams->addParam( "Host",			&mHost );
	mParams->addButton( "Write",		[ & ]() { write(); },	"key=w" );
	mParams->addButton( "Quit",			[ & ]() { quit(); },	"key=q" );
	
	mClient = TcpClient::create( io_service() );
	mClient->connectConnectEventHandler( &HttpClientApp::onConnect, this );
	mClient->connectErrorEventHandler( &HttpClientApp::onError, this );
	mClient->connectResolveEventHandler( &HttpClientApp::onResolve, this );
}
void WebClientApp::write()
{
	// This sample is meant to work with only one session at a time
	if ( mSession && mSession->getSocket()->is_open() ) {
		return;
	}
		
	mText.push_back( "Connecting to:\n" + mHost + ":" + toString( mPort ) );
	
	mClient->connect( mHost, (uint16_t)mPort );		
}
void TcpClientApp::setup()
{	
	gl::enableAlphaBlending();
	gl::enable( GL_TEXTURE_2D );
	
	mFont		= Font( "Georgia", 24 );
	mFrameRate	= 0.0f;
	mFullScreen	= false;
	mHost		= "localhost";
	mPort		= 2000;
	mRequest	= "echo";
		
	mParams = params::InterfaceGl::create( "Params", Vec2i( 200, 150 ) );
	mParams->addParam( "Frame rate",	&mFrameRate,					"", true );
	mParams->addParam( "Full screen",	&mFullScreen,					"key=f" );
	mParams->addParam( "Host",			&mHost );
	mParams->addParam( "Port",			&mPort,
					  "min=0 max=65535 step=1 keyDecr=p keyIncr=P" );
	mParams->addParam( "Request",		&mRequest );
	mParams->addButton( "Write", bind(	&TcpClientApp::write, this ),	"key=w" );
	mParams->addButton( "Quit", bind(	&TcpClientApp::quit, this ),	"key=q" );
	
	// Initialize a client by passing a boost::asio::io_service to it.
	// ci::App already has one that it polls on update, so we'll use that.
	// You can use your own io_service, but you will have to manage it 
	// manually (i.e., call poll(), poll_one(), run(), etc).
	mClient = TcpClient::create( io_service() );

	// Add callbacks to work with the client asynchronously.
	// Note that you can use lambdas.
	mClient->connectConnectEventHandler( &TcpClientApp::onConnect, this );
	mClient->connectErrorEventHandler( &TcpClientApp::onError, this );
	mClient->connectResolveEventHandler( [ & ]()
	{
		mText.push_back( "Endpoint resolved" );
	} );
}
void TcpClientApp::write()
{
	// This sample is meant to work with only one session at a time.
	if ( mSession && mSession->getSocket()->is_open() ) {
		// Write data is packaged as a ci::Buffer. This allows 
		// you to send any kind of data. Because it's more common to
		// work with strings, the session object has static convenience 
		// methods for converting between std::string and ci::Buffer.
		Buffer buffer = TcpSession::stringToBuffer( mRequest );
		mSession->write( buffer );
	} else {	
		// Before we can write, we need to establish a connection 
		// and create a session. Check out the onConnect method.
		mText.push_back( "Connecting to: " + mHost + ":" + toString( mPort ) );
		mClient->connect( mHost, (uint16_t)mPort );
	}
}
void HttpClientApp::write()
{
	if ( mSession && mSession->getSocket()->is_open() ) {
		return;
	}
	
	// Reset download stats
	mBytesRead		= 0;
	mContentLength	= 0;
	
	// Update request body
	string index	= toString( mIndex );
	mFilename		= index;
	mHttpRequest.setBody( HttpRequest::stringToBuffer( index ) );
	
	mText.push_back( "Connecting to:\n" + mHost + ":2000" );
	
	// Ports <1024 are restricted to root
	mClient->connect( mHost, 2000 );
}