void Connection_Manager::send_dispatch_by_id(const string& data, uint32_t sid) { CConnection* conn = get_connection(sid); if(conn == NULL) connecting_by_id(data, sid); else conn->send(data); }
void Connection_Manager::connecting_by_id(const string& data, uint32_t sid) { Server_Node_Map::iterator it = nodes_.find(sid); if(it != nodes_.end()) { CConnection* conn = NULL; if(it->second.conn == NULL) //建立一条新的TCP服务器连接 { conn = CONNECTION_POOL.pop_obj(); conn->set_index(0); conn->set_server_type(it->second.server_type); conn->set_server_id(it->second.server_id); //选取连接地址,目标地址为电信用电信网卡进行连接,依次类推 Inet_Addr src_addr, dst_addr; get_address_pair(it->second, src_addr, dst_addr); src_addr.set_port(0); //随机一个端口作为客户机端口 if(src_addr.get_ip() != INADDR_ANY) { src_addr.set_port(15000 + rand() % 15000); } CORE_INFO("connecting " << dst_addr << ", by local addr = " << src_addr << ", dst sid = " << sid); if(conn->connect(src_addr, dst_addr) != 0) { conn->close(); CONNECTION_POOL.push_obj(conn); CORE_DEBUG("push conn = " << this); CORE_ERROR("connect " << dst_addr << "failed!!"); return ; } it->second.conn = conn; } else { conn = it->second.conn; } if(conn->get_state() != CConnection::CONN_CONNECTED) //处于连接状态,缓冲到LIST中 it->second.strms.push_back(data); else conn->send(data); } else { CORE_ERROR("sid = " << sid << " error!!"); } }
void Connection_Manager::send_dispatch_by_id(CCorePacket& packet, uint32_t sid) { CConnection* conn = get_connection(sid); if(conn == NULL) { GAIN_BINSTREAM(strm); string data; *strm << packet; strm->bin_to_string(data); connecting_by_id(data, sid); RETURN_BINSTREAM(strm); } else { conn->send(packet); } }