写在前面:前一篇文字http://blog.csdn.net/linyanwen99/article/details/8274754已经把相关原理讲解了,这里就不再赘述,直接上源码
一.crossdomain.h头文件的声明,如下:
#ifndef CROSSDOMAIN_H
#define CROSSDOMAIN_H
#include
#include
#include
#include
#include
using namespace boost;
class CrossDomain{
private:
struct Server;
shared_ptr
CrossDomain(asio::io_service& io_service,const std::string& local_port);
static CrossDomain* s_instance;
public:
static void create(asio::io_service& io_service,const std::string& local_port){
s_instance = new CrossDomain(io_service,local_port);
}
static CrossDomain* instance();
void startServer();
};
#endif
转载请注明出处:山水间博客,http://blog.csdn.net/linyanwen99/article/details/8274812
二.crossdomain.cpp的实现,如下:
#include "crossdomain.h"
using boost::asio::ip::tcp;
using boost::uint8_t;
CrossDomain* CrossDomain::s_instance = NULL;
struct CrossDomainImpl:public boost::enable_shared_from_this
public:
static const unsigned MaxReadSize = 22;
typedef shared_ptr
static CrossDomainImplPtr create(asio::io_service& io_service){
return CrossDomainImplPtr(new CrossDomainImpl(io_service));
}
tcp::socket& getSocket(){
return m_socket;
}
void start(){
std::cout<<"[CrossDomainImpl::start()]"<
}
~CrossDomainImpl(){
close();
}
void close(){
if(m_socket.is_open()){
m_socket.close();
}
}
private:
CrossDomainImpl(asio::io_service& io_service):m_socket(io_service){
}
void start_read_some(){
std::cout<<"CrossDomainImpl::start_read_some()"<
,bind(&CrossDomainImpl::handle_read_some,shared_from_this()
,asio::placeholders::error()));
}
void handle_read_some(const system::error_code& err){
std::cout<<"CrossDomainImpl::handle_read_some()"<
std::string str(m_readbuf);
std::string reply("invalid");
if(str == "
reply = "anything you wanna send back to client...";
}
asio::async_write(m_socket,asio::buffer("hello server"),bind(&CrossDomainImpl::handle_write
,shared_from_this(),asio::placeholders::error));
}
}
void handle_write(const system::error_code& error){
std::cout<<"CrossDomain handle_write,gonna close"<
}
tcp::socket m_socket;
char m_readbuf[MaxReadSize];
};
转载请注明出处:山水间博客,http://blog.csdn.net/linyanwen99/article/details/8274812
struct CrossDomain::Server{
private:
CrossDomain* m_facade;
tcp::acceptor m_acceptor;
bool m_listened;
std::string m_local_port;
public:
Server(asio::io_service& io_service,const std::string& local_port)
:m_acceptor(io_service),m_listened(false),m_local_port(local_port)
{
//intend to leave it blank;
}
~Server(){
if(m_acceptor.is_open()){
std::cout<<"close server acceptor"<
}
}
void start_server(){
std::cout<<"CrossDomain start_server..."<
std::cout<<"try to listen"<
tcp::endpoint ep(tcp::endpoint(tcp::v4(),atoi(m_local_port.c_str())));
m_acceptor.open(ep.protocol());
m_acceptor.bind(ep);
m_acceptor.listen();
}catch(const system::error_code& ec){
std::cout<<"m_local_port: "<
}catch(...){
std::cout<<"unknown error while trying to listen..."<
}
m_listened = true;
std::cout<<"listen port "<
/**
*说明:这里需要创建一个CrossDomainImplPtr的实例,然后调用m_acceptor的async_accept()函数等待连接
*连接成功后,由CrossDomainImplPtr的实例处理相关的数据收发,然后重新等待接收新的连接,实现很简单,这里略去
*/
std::cout<<"[Server::start_server()] end of Server::start_server() function..."<
private:
void handle_accept(CrossDomainImpl::CrossDomainImplPtr pserver_impl,const system::error_code& err){
std::cout<<"CrossDomain handle_accept..."<
std::cout<<"CrossDomain everythine ok,start..."<
start_server();
}else{
pserver_impl->close();
}
}
};
CrossDomain::CrossDomain(asio::io_service& io_service,const std::string& local_port)
:m_pserver(new Server(io_service,local_port)){
}
CrossDomain* CrossDomain::instance(){
if(!s_instance){
return NULL;
}
return s_instance;
}
void CrossDomain::startServer(){
if(m_pserver != NULL){
m_pserver->start_server();
}
}
三.main.cpp的实现,如下:
#include "crossdomain.h"
int main(int argc,char** argv){
asio::io_service ios;
std::string local_port = "9999";
CrossDomain::create(ios,local_port);
if(CrossDomain::instance() != NULL){
CrossDomain::instance()->startServer();
}
ios.run();
return 0;
}
四.运行结果,如下:
参考文献:http://blog.csdn.net/sunyurun/article/details/8235304
转载请注明出处:山水间博客,http://blog.csdn.net/linyanwen99/article/details/8274812