Friday, December 19, 2008

boost::asio - Network Library modified by Terry - 3

boost::asio 내 맘대로 바꾼 버전 3번째.
client 부분임.

client 특징:
- 거의 모든 기능이 session 에 들어가 있기 때문에 초 간단함.
- connect 를 async / blocking 두 가지 모두 지원하지만 blocking 을 추천.
- 기본적으로 ioservice 가 iocp 를 사용하기 때문에, thread 를 돌림.
- 사용하는 쪽에서는 끝날 때까지 loop 돌려주다가 join 호출해서 종료하면 됨.

client.h

#pragma once

#ifndef _ASIO_CLIENT_H_
#define _ASIO_CLIENT_H_

#include "session.h"

class asio_client
{
private:
boost::asio::io_service& io_service_;
tcp::resolver::iterator endpoint_iterator_;
session_ptr session_;
boost::thread t_;

void* cb_class_;
cb_connect cb_connect_;

bool started_;

public:
asio_client(boost::asio::io_service& io_service, tcp::resolver::iterator endpoint_iterator) :
io_service_(io_service), endpoint_iterator_(endpoint_iterator),
cb_class_(0), cb_connect_(0),
started_(0)
{
session_ptr new_session(new session(io_service));
session_ = new_session;

//connect();
}

void connect()
{
tcp::endpoint endpoint = *endpoint_iterator_;
session_->socket().async_connect(endpoint,
boost::bind(&asio_client::handle_connect,
this,
boost::asio::placeholders::error,
++endpoint_iterator_)
);
}

void connect_blocking()
{
boost::system::error_code error;
session_->socket().connect(*endpoint_iterator_, error);

if(error)
{
cerr << "Error! failed to connect to a patch server : << " << started_ =" true;" started_ ="="" t_ =" boost::thread(boost::bind(&boost::asio::io_service::run," started_ ="="" cb_class_ =" cb_class;" cb_connect_ =" f_connect;">set_callback(cb_class, f_parser);
}

protected:
void handle_connect(const boost::system::error_code& error, tcp::resolver::iterator endpoint_iterator)
{
if(error)
{
cerr << "\n -Error- failed to connect to a patch server : << " <<>do_async_read();
}

void do_close()
{
cout << "Close socket" <<>socket().close();
}
};

typedef boost::shared_ptr asio_client_ptr;

#endif//_ASIO_CLIENT_H_

No comments:

Post a Comment