ENet网络库教程翻译(二)

2014-11-24 02:30:41 · 作者: · 浏览: 4
.packet -> dataLength,
event.packet -> data,
event.peer -> data,
event.channelID);
/* Clean up the packet now that we're done using it. */
enet_packet_destroy (event.packet);
break;
caseENET_EVENT_TYPE_DISCONNECT:
printf ("%s disconected.\n", event.peer -> data);
/* Reset the peer's client information. */
event.peer -> data = NULL;
}
}
...
...
...
给ENet peer发送一个数据包
ENet中的数据包用enet_packet_create()创建,数据包的大小必须指定。可选择的,初始化数据可能会复制到数据包里。
某些标志可能也会应用在enet_packet_create()上来控制一些数据包特性:
ENET_PACKET_FLAG_RELIABLE 指定了数据包必须用可靠的数据传输。一个可靠的数据包必定可靠的 传输,一系列重试的尝试会进行直到数据包发送到的外部主机的验证消息被接收到了。如果一系列尝试后还是没有任何的答复,ENet将假定peer已经断开连接并且强制重制连接。如果标志没有指定,数据包将假定为一个不可靠的数据包,将不会有重试机制。
一个数据包可能会需要改变大小(扩张或者截取),使用enet_packet_resize().
一个数据包使用enet_peer_send()来发送到外部主机。enet_peer_send() 接收一个通道参数来发送数据包到指定的peer。一旦数据包使用ENet 的enet_peer_send()来处理,ENet将处理它的释放并且不需要使用 enet_packet_destroy()。
也可以使用enet_host_broadcast()来发送数据包给所有链接到指定host peer上的peers通过指定的通道ID。
数据包队列将会发送enet_host_service()。另外,enet_host_flush()将发送数据包队列并且不分发它。
/* Create a reliable packet of size 7 containing "packet\0" */
ENetPacket * packet =enet_packet_create ("packet",
strlen ("packet") + 1,
ENET_PACKET_FLAG_RELIABLE);
/* Extend the packet so and append the string "foo", so it now */
/* contains "packetfoo\0" */
enet_packet_resize (packet, strlen ("packetfoo") + 1);
strcpy (& packet -> data [strlen ("packet")],"foo");
/* Send the packet to the peer over channel id 0. */
/* One could also broadcast the packet by */
/* enet_host_broadcast (host, 0, packet); */
enet_peer_send (peer, 0, packet);
...
...
...
/* One could just use enet_host_service() instead. */
enet_host_flush (host);
断开连接
peers使用enet_peer_disconnect()将会平缓的断开连接。一个断开连接的请求将会发送到外部主机,ENet将会等待来自远程主机的确认在完成断开之前。事件状态ENET_EVENT_TYPE_DISCONNECT 将会产生一旦断开成功。一旦在参数时间内没有应答peer将会强制断开
enet_peer_reset()将会简直快开一个peer,外部主机将不会得到断开的消息,将在外部主机上超时,没有事件生成。
ENetEvent event;
enet_peer_disconnect (peer, 0);
/* Allow up to 3 seconds for the disconnect to succeed
and drop any packets received packets.
/
while (enet_host_service (client, & event, 3000) > 0)
{
switch (event.type)
{
case ENET_EVENT_TYPE_RECEIVE:
enet_packet_destroy (event.packet);
break;
case ENET_EVENT_TYPE_DISCONNECT:
puts ("Disconnection succeeded.");
return;
...
...
...
}
}
/* We've arrived here, so the disconnect attempt didn't */
/* succeed yet. Force the connection down. */
enet_peer_reset (peer);
...
...
...
连接到Enet主机
与远程主机的连接通过enet_host_connect()来初始化。它接收了一个远程的主机的地址为参数和一个分配给通讯的通道数量为参数来连接。如果N个通道被使用,那么序号是从0 - N-1,peer变量代表尝试连接的数量,null表示没有可用的peer,当连接成功,ENET_EVENT_TYPE_CONNECT 类型将被生成。如果连接超时或者各种原因失败,那么ENET_EVENT_TYPE_DISCONNECT类型会生成。
ENetAddress address;
ENetEvent event;
ENetPeer *peer;
/* Connect to some.server.net:1234. */
enet_address_set_host (& address, "some.server.net");
address.port = 1234;
/* Initiate the connection, allocating the two channels 0 and 1. */
peer = enet_host_connect (client, & address, 2, 0);
if (peer == NULL)
{
fprintf (stderr,
"No available peers for initiating an ENet connection.\n");
exit (EXIT_FAILURE);
}
/* Wait up to 5 seconds for the connec