[ENet-discuss] sorry for the post...
Eduardo Del Balso
e_delbalso at hotmail.com
Thu Mar 24 22:31:03 PST 2005
Hey guys,
I hate to post a "Please help me" email to a discussion list, but I'm really
at my wit's end. I'm wrapping the enet library into a class so that it'll
fit within the architecture of my program, but the functionality is off, and
I just can't figure out why.
If anybody has the heart to read through this, I can't see what I've done
wrong. What happens is when I launch this as the server, it just polls for
packets (using enet_host_service()). I then launch another instance as the
client, which tries to connect. Now what happens is that the connect from
the client receives the connect event as a reply from the server, but the
server never generates the local CONNECT event, so my server loop doesn't
detect a connected client, which is bad. I can't figure out why my
enet_host_service( ... ) doesn't read the connect event on the server side.
I will paste my code below. It's really simple, I stripped it to the bare
bones to try and find the problem. PLEASE, if anybody has any
advice/remarks, throw me a bone, I'm at my wit's end.
Thanks!
here is my main():
#include "NetworkEngine.h"
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::cin;
using std::endl;
int main()
{
char Input;
cout << "s or c? ";
cin >> Input;
CNetworkEngine::Instance()->init( (Input=='s'?NE_SERVER:NE_CLIENT) );
switch( Input )
{
case 'c':
CNetworkEngine::Instance()->connect( "127.0.0.1" );
break;
case 's':
while( true )
{
CNetworkEngine::Instance()->receive();
}
break;
default:
break;
};
CNetworkEngine::Instance()->shutdown();
return 0;
}
**********************
here is my NetworkEngine.h
#pragma once
#include "singleton.h"
#include "enet.h"
enum NEError
{
NE_SUCCESS,
NE_ERROR
};
enum NEType
{
NE_CLIENT,
NE_SERVER
};
#define NE_SERVER_PORT 1234;
class CNetworkEngine : public CSingleton<CNetworkEngine>
{
friend CSingleton<CNetworkEngine>;
public:
CNetworkEngine(void);
~CNetworkEngine(void);
NEError init( NEType );
NEError shutdown();
NEError connect( char * );
NEError disconnect();
NEError receive();
private:
bool m_initialized;
bool m_hostCreated;
bool m_isConnectedToServer;
NEType m_type;
ENetHost * m_client;
ENetHost * m_server;
ENetPeer *m_peer;
};
****************************
here is my NetworkEngine.cpp
#include ".\networkengine.h"
#include "enet.h"
#include <iostream>
using std::cout;
using std::endl;
CNetworkEngine::CNetworkEngine(void)
{
m_initialized = false;
m_hostCreated = false;
m_isConnectedToServer = false;
}
CNetworkEngine::~CNetworkEngine(void)
{
shutdown();
}
NEError CNetworkEngine::init( NEType type )
{
if( enet_initialize() != 0 )
return NE_ERROR;
m_initialized = true;
m_type = type;
if( type == NE_SERVER )
{
cout << "Creating Server...\n";
ENetAddress address;
cout << "Setting host to " << (int)ENET_HOST_ANY;
address.host = ENET_HOST_ANY;
address.port = 1235;
m_server = enet_host_create( &address, 5, 0, 0 );
if( m_server == NULL )
{
return NE_ERROR;
}
m_hostCreated = true;
}
else if( type == NE_CLIENT )
{
cout << "Creating client...\n";
m_client = enet_host_create( NULL, 1, 57600/8, 14400/8 );
if( m_client == NULL )
{
return NE_ERROR;
}
m_hostCreated = true;
}
else
{
return NE_ERROR;
}
return NE_SUCCESS;
}
NEError CNetworkEngine::shutdown()
{
cout << "Shutting down...\n";
if( m_isConnectedToServer )
disconnect();
if( m_hostCreated )
enet_host_destroy( (m_type == NE_CLIENT ? m_client : m_server) );
if( m_initialized )
enet_deinitialize();
return NE_SUCCESS;
}
NEError CNetworkEngine::connect( char * add )
{
if( m_type != NE_CLIENT )
{
cout << "Cannot connect if not client...\n";
return NE_ERROR;
}
ENetAddress address;
ENetEvent event;
enet_address_set_host( & address, "127.0.0.1" );
address.port = NE_SERVER_PORT;
m_peer = enet_host_connect( m_client, & address, 2);
if( m_peer == NULL )
{
cout << "connect failed!\n";
return NE_ERROR;
}
/* Wait up to 5 seconds for the connection attempt to succeed. */
if (enet_host_service (m_client, & event, 5000) > 0 &&
event.type == ENET_EVENT_TYPE_CONNECT)
{
cout << "Connection to " << add << ":1234 succeeded.";
m_isConnectedToServer = true;
}
else
{
enet_peer_reset (m_peer);
cout << "Connection to some.server.net:1234 failed.";
}
return NE_SUCCESS;
}
NEError CNetworkEngine::disconnect()
{
if( m_type != NE_CLIENT )
{
cout << "Cannot disconnect if not client\n";
return NE_ERROR;
}
ENetEvent event;
cout << "Disconnecting...\n";
enet_peer_disconnect( &m_client -> peers[0] );
while (enet_host_service (m_client, & event, 3000) > 0)
{
switch (event.type)
{
case ENET_EVENT_TYPE_RECEIVE:
cout << "Dropping received packets...\n";
enet_packet_destroy (event.packet);
break;
case ENET_EVENT_TYPE_DISCONNECT:
cout << "Disconnection succeeded.";
return NE_SUCCESS;
}
}
cout << "Disconnect failed. Forcing reset.\n";
enet_peer_reset(& m_client -> peers [0] );
return NE_SUCCESS;
}
NEError CNetworkEngine::receive()
{
ENetEvent event;
int ret = 1;
while( ret > 0 )
{
ret = enet_host_service( m_server, &event, 3000 );
switch( event.type )
{
case ENET_EVENT_TYPE_CONNECT:
cout << "A new client connected from "
<< event.peer -> address.host << ":"
<< event.peer -> address.port << ".\n";
/* Store any relevant client information here. */
event.peer -> data = "Client information";
break;
case ENET_EVENT_TYPE_RECEIVE:
cout << "A packet of length "
<< event.packet -> dataLength
<< " containing "
<< event.packet -> data
<< " was received from "
<< event.peer -> data
<< " on channel "
<< event.channelID
<< ".\n";
/* Clean up the packet now that we're done using it. */
enet_packet_destroy (event.packet);
break;
case ENET_EVENT_TYPE_DISCONNECT:
cout << event.peer -> data << " disconected.\n";
/* Reset the peer's client information. */
event.peer -> data = NULL;
break;
case ENET_EVENT_TYPE_NONE:
cout << "Waiting...\n";
break;
}
}
return NE_SUCCESS;
}
More information about the ENet-discuss
mailing list