目前正在看Enet,传输过程如下:
循环中,enet_peer_send()函数的目的是将需要传输的数据包packet放入待发送的队列中
enet_host_service()函数是真正的数据发送函数(接收端函数也是这个)
ENetPacket *packet=enet_packet_create(NULL,ret,ENET_PACKET_FLAG_RELIABLE);
memcpy(packet->data,buff,ret);
enet_peer_send(server,0,packet);
enet_host_service(client,NULL,5);
ENetPacket* enet_packet_create(const void * data,size_t dataLength,enet_uint32 flags )
#include <packet.c>
Creates a packet that may be sent to a peer.
创建时会有flags,各个flags表示:
Enumerator
ENET_PACKET_FLAG_RELIABLE packet must be received by the target peer and resend attempts should be made until the packet is delivered
ENET_PACKET_FLAG_UNSEQUENCED packet will not be sequenced with other packets not supported for reliable packets
ENET_PACKET_FLAG_NO_ALLOCATE packet will not allocate data, and user must supply it instead
ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT packet will be fragmented using unreliable (instead of reliable) sends if it exceeds the MTU
ENET_PACKET_FLAG_SENT whether the packet has been sent from all queues it has been entered into
<span style="font-family: Arial, Helvetica, sans-serif;">int enet_peer_send(ENetPeer * peer,enet_uint8 channelID,ENetPacket * packet )</span>
#include <peer.c>
Queues a packet to be sent.
调用flags过程如下:
if ((packet -> flags & (ENET_PACKET_FLAG_RELIABLE | ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT)) == ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT &&
channel -> outgoingUnreliableSequenceNumber < 0xFFFF)
{
commandNumber = ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT;
startSequenceNumber = ENET_HOST_TO_NET_16 (channel -> outgoingUnreliableSequenceNumber + 1);
}
else
{
commandNumber = ENET_PROTOCOL_COMMAND_SEND_FRAGMENT | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
startSequenceNumber = ENET_HOST_TO_NET_16 (channel -> outgoingReliableSequenceNumber + 1);
}