[ENet-discuss] ENET Client getting backed up with Reliable Packets

Jeffrey Holtz jholtz at accuratetechnologies.com
Tue Jun 22 14:51:47 PDT 2004


Thought that I'd post this around and see if anyone has any ideas.
Simple Console App.  BTW I have also a file to allow ethereal to decode
the ENET Protocol if anyone is interested:

I believe that I'm just not setting up my test server and client
properly to handle the command flow.  What I have is a client that tries
to send 1024 reliable packets of 100 bytes of data each as fast as he
can. He ends up able to send 327 reliable packets.  Then since we have
not received an ACK back from the server he queues the remaining
packets.  If I slow this down by either setting the enet_host_server
timeout value to ~100ms or inserting a Sleep(100) in my loop then the
server has time to send the ACK and so the entire 1024 packets are sent.
I believe what I need is somewhy to detect that there are packets still
to be transferred out and not exit out of my program.  Does anyone have
any ideas about this?

Jeff

Jeffrey B. Holtz
Senior Software Engineer
Accurate Technologies Inc.
47199 Cartier Dr, Wixom, MI 48393
ph:  248-848-9200
fax: 248-848-9016
jholtz at accuratetechnologies.com

// Server.c
#include "stdafx.h"
#include "enet/enet.h"

#define MAX_CLIENT_NAME 100
char ClientName[MAX_CLIENT_NAME];
const int   ServerPort=1234;

int _tmain(int argc, _TCHAR* argv[])
{
	/* Setup Console App to contain a large buffer for debug
purposes */
	HANDLE hStdout; 
	CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
	COORD dwSize;
	hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
	// Get the current screen buffer size and window position. 
	if (! GetConsoleScreenBufferInfo(hStdout, &csbiInfo)) 
		return 1; 
	dwSize.X=120;
	dwSize.Y=1024*4;// 4 K
	if (! SetConsoleScreenBufferSize(hStdout,dwSize))
		return 1; 

	if (enet_initialize() != 0)
	{
		fprintf (stderr, "An error occurred while initializing
ENet.\n");
		return EXIT_FAILURE;
	}
	atexit (enet_deinitialize);

	// Create Server
	ENetAddress address;
	ENetHost * server;
	ENetEvent event;

    /* Bind the server to the default localhost.     */
    /* A specific host address can be specified by   */
    /* enet_address_set_host (& address, "x.x.x.x"); */
    address.host = ENET_HOST_ANY;
    /* Bind the server to port 1234. */
	address.port = ServerPort;

	server = enet_host_create (& address /* the address to bind the
server host to */, 
                                 32      /* allow up to 32 clients
and/or outgoing connections */,
                                  0      /* assume any amount of
incoming bandwidth */,
                                  0      /* assume any amount of
outgoing bandwidth */);
	if (server == NULL)
	{
        fprintf (stderr, 
                 "An error occurred while trying to create an ENet
server host.\n");
        exit (EXIT_FAILURE);
	}

    
	for (;;)
	{
		if (enet_host_service (server, & event,1000) < 0)
			break;

		switch (event.type)
		{
		case ENET_EVENT_TYPE_CONNECT:
			{
				ENetAddress enetAddress;
				char * ClientData = (char
*)malloc(MAX_CLIENT_NAME);
				memset(ClientData,0x00,MAX_CLIENT_NAME);
				memset(ClientName,0x00,MAX_CLIENT_NAME);
				enetAddress.host =
event.peer->address.host;
				enetAddress.port =
event.peer->address.port;
				if
(!enet_address_get_host(&enetAddress,ClientName,MAX_CLIENT_NAME))
		            sprintf (ClientData, "%s:%u", ClientName,
event.peer->address.port);
				else
				    sprintf (ClientData, "%x:%u\n",
event.peer->address.host, event.peer->address.port);

				printf ("A new client connected from
%s.\n", ClientData);
				/* Store any relevant client information
here. */
				event.peer -> data = ClientData;
			}
			break;

		case ENET_EVENT_TYPE_RECEIVE:
			{
				printf ("Received:  %d bytes in %u ms
from peer ID %u\n",
						event.packet ->
dataLength,
						event.peer ->
roundTripTime,
						event.peer ->
incomingPeerID);

				/* Clean up the packet now that we're
done using it. */
				enet_packet_destroy (event.packet);
			}
			break;
           
		case ENET_EVENT_TYPE_DISCONNECT:
			{
				/* Reset the peer's client information.
*/
				if (event.peer->data != NULL)
				{
					printf ("Peer <%s> disconnected
\n", event.peer->data);
					free(event.peer->data);
					event.peer -> data = NULL;
				}
				else
					printf ("Unknown Peer
disconnected\n");
			}
		}
	}

	// Cleanup Server
	enet_host_destroy(server);

	return 0;
}


// Client.c
#include "stdafx.h"
#include "enet/enet.h"

#define SEND_BUFFER_SIZE 100
const char *ServerIP="10.1.60.7";
const int   ServerPort=1234;
int _tmain(int argc, _TCHAR* argv[])
{
	/* Setup Console App to contain a large buffer for debug
purposes */
	HANDLE hStdout; 
	CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
	COORD dwSize;
	hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
	// Get the current screen buffer size and window position. 
	if (! GetConsoleScreenBufferInfo(hStdout, &csbiInfo)) 
		return 1; 
	dwSize.X=120;
	dwSize.Y=1024*4;// 4 Kb
	if (! SetConsoleScreenBufferSize(hStdout,dwSize))
        return 1; 

	// Initialize Enet
	if (enet_initialize () != 0)
	{
		fprintf (stderr, "An error occurred while initializing
ENet.\n");
		return EXIT_FAILURE;
	}
	atexit (enet_deinitialize);

	// Create Client
	ENetPeer *peer;
	ENetHost * client;
	ENetAddress address;
	ENetEvent event;

	client = enet_host_create (NULL /* create a client host */,
								1 /*
only allow 1 outgoing connection */,
								0 /*
Allow Maximum downstream bandwidth */,
								0);/*
Allow Maximum upstream bandwidth */
	if (client == NULL)
	{
		fprintf (stderr, "An error occurred while trying to
create an ENet client host.\n");
		exit (EXIT_FAILURE);
	}

	/* Connect to ServerIP:ServerPort. */
	enet_address_set_host (& address, ServerIP);
	address.port = ServerPort;

	/* Initiate the connection, allocating the two channels 0 and 1.
*/
	peer = enet_host_connect (client, & address, 2);    
	if (peer == NULL)
	{
		fprintf (stderr, "No available peers for initiating an
ENet connection.\n");
		exit (EXIT_FAILURE);
	}

	/* Wait up to 5 seconds for the connection attempt to succeed.*/
	if ((enet_host_service (client, & event, 5000) > 0) && 
		(event.type==ENET_EVENT_TYPE_CONNECT))
	{
		char pData[SEND_BUFFER_SIZE];
		memset(pData,0x00,SEND_BUFFER_SIZE);
		printf ("Connection to %s:%d
succeeded.\n",ServerIP,ServerPort);

		for (int nPacketNumber=0; nPacketNumber<1024;
nPacketNumber++)
		{
			sprintf(pData,"%04d",nPacketNumber);

			/* Create a reliable packet of size
SEND_BUFFER_SIZE containing "packet\0" */
			ENetPacket * pPacket = enet_packet_create
(pData, SEND_BUFFER_SIZE, ENET_PACKET_FLAG_RELIABLE);

			/* Send the packet to the peer over channel id
0. */
			/* One could also broadcast the packet by
*/
			/* enet_host_broadcast (host, 3, packet);
*/
			enet_peer_send (peer, 0, pPacket);

			// Execute Send/Receive Service
			if (enet_host_service (client, & event, 0) < 0)
				break;
		}
	}
	else
	{
		printf("Connection to %s:%d failed.", ServerIP,
ServerPort);

		/* Either the 5 seconds are up or a disconnect event was
*/
		/* received. Reset the peer in the event the 5 seconds
*/
		/* had run out without any significant event.
*/
		if (event.type != ENET_EVENT_TYPE_DISCONNECT)
			enet_peer_reset (peer);

	}

	// Cleanup Client
	enet_host_destroy(client);

	return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.puremagic.com/pipermail/enet-discuss/attachments/20040622/493c9593/attachment-0001.html


More information about the ENet-discuss mailing list