本文整理汇总了C++中NetworkPacket::getPeerId方法的典型用法代码示例。如果您正苦于以下问题:C++ NetworkPacket::getPeerId方法的具体用法?C++ NetworkPacket::getPeerId怎么用?C++ NetworkPacket::getPeerId使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NetworkPacket
的用法示例。
在下文中一共展示了NetworkPacket::getPeerId方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: testConnectSendReceive
void TestConnection::testConnectSendReceive()
{
DSTACK("TestConnection::Run");
/*
Test some real connections
NOTE: This mostly tests the legacy interface.
*/
u32 proto_id = 0xad26846a;
Handler hand_server("server");
Handler hand_client("client");
Address address(0, 0, 0, 0, 30001);
Address bind_addr(0, 0, 0, 0, 30001);
/*
* Try to use the bind_address for servers with no localhost address
* For example: FreeBSD jails
*/
std::string bind_str = g_settings->get("bind_address");
try {
bind_addr.Resolve(bind_str.c_str());
if (!bind_addr.isIPv6()) {
address = bind_addr;
}
} catch (ResolveError &e) {
}
infostream << "** Creating server Connection" << std::endl;
con::Connection server(proto_id, 512, 5.0, false, &hand_server);
server.Serve(address);
infostream << "** Creating client Connection" << std::endl;
con::Connection client(proto_id, 512, 5.0, false, &hand_client);
UASSERT(hand_server.count == 0);
UASSERT(hand_client.count == 0);
sleep_ms(50);
Address server_address(127, 0, 0, 1, 30001);
if (address != Address(0, 0, 0, 0, 30001)) {
server_address = bind_addr;
}
infostream << "** running client.Connect()" << std::endl;
client.Connect(server_address);
sleep_ms(50);
// Client should not have added client yet
UASSERT(hand_client.count == 0);
try {
NetworkPacket pkt;
infostream << "** running client.Receive()" << std::endl;
client.Receive(&pkt);
infostream << "** Client received: peer_id=" << pkt.getPeerId()
<< ", size=" << pkt.getSize() << std::endl;
} catch (con::NoIncomingDataException &e) {
}
// Client should have added server now
UASSERT(hand_client.count == 1);
UASSERT(hand_client.last_id == 1);
// Server should not have added client yet
UASSERT(hand_server.count == 0);
sleep_ms(100);
try {
NetworkPacket pkt;
infostream << "** running server.Receive()" << std::endl;
server.Receive(&pkt);
infostream << "** Server received: peer_id=" << pkt.getPeerId()
<< ", size=" << pkt.getSize()
<< std::endl;
} catch (con::NoIncomingDataException &e) {
// No actual data received, but the client has
// probably been connected
}
// Client should be the same
UASSERT(hand_client.count == 1);
UASSERT(hand_client.last_id == 1);
// Server should have the client
UASSERT(hand_server.count == 1);
UASSERT(hand_server.last_id == 2);
//sleep_ms(50);
while (client.Connected() == false) {
try {
NetworkPacket pkt;
infostream << "** running client.Receive()" << std::endl;
client.Receive(&pkt);
infostream << "** Client received: peer_id=" << pkt.getPeerId()
//.........这里部分代码省略.........