首先这个组件是用来管理网络连接的,在我们使用NOX时,常用的命令就是 “-i ptcp:127.0.0.1:6633”,也即是传到此模块中构造函数中的变量“interface”是“-i ptcp:127.0.0.1:6633”
构造函数:
Connection_manager::Connection_manager(const Component_context* ctxt, const std::list<std::string>& interfaces)
这里的interface参数决定了这个组件执行
接口:
Component* Connection_manager::instantiate(const Component_context* ctxt, const std::list<std::string>& interfaces)
生成Connection_manager对象实例;
void Connection_manager::configure()
函数体为空
void Connection_manager::install()
从这个函数中我们可以看到:
1.当interface是“ptcp”类型时,组件为服务器的功能,接受连接;
2.当interface是“tcp”类型时,组件为客户端的功能,向服务器发起连接;
void
Connection_manager::connect(type, host, port, key, cert, cafile)
type == TCP || type == SSL 作为客户端时调用, 连接到interface "%s:%s:%d:%s:%s:%s"
void
Connection_manager::listen(type, host, port, key, cert, cafile)
type == PTCP || type == PSSL 作为服务端时调用,监听interface "%s:%s:%d:%s:%s:%s"
void listen(Conn_t type, const std::string& bind_ip, const uint16_t& port,
const std::string& key,
const std::string& cert,
const std::string& cafile);
对网络监听的时候会首先调用这个listen函数,参数有(连接类型,ip字符串,端口号,私钥,证书,公证单位证书),然后根据连接类型,选择调用以下两个listen函数:
1.void listen(boost::shared_ptr<boost::asio::ip::tcp::acceptor>); //连接类型为PTCP
2.void listen(boost::shared_ptr<boost::asio::ip::tcp::acceptor>,
const std::string& key,
const std::string& cert,
const std::string& cafile); //连接类型为PSSL
void listen(boost::shared_ptr<boost::asio::ip::tcp::acceptor>);
每监听到一个连接,就保存在socket中(由语句boost::shared_ptr<tcp_socket> socket(new tcp_socket(io))得到),然后accept这个套接字,并将accept完成的句柄设为Connection_manager::handle_connect()
void Connection_manager::handle_connect(boost::shared_ptr<Async_stream> socket, Listen_callback cb, const boost::system::error_code& ec)
这个函数是在发生错误时,再调用cb函数,若无错误,就说明套接字accept成功,一个新accept的套接字将通过dispatch(New_connection_event(connection));将套接字对象发送到消息中,凡是接受这个消息的组件都可以通过调用connection对象实体与连接上的客户端通信;
注:不管是作为TCP还是PTCP最终都要将套接字发送到消息中,抛送到消息管理器中