1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
class Client : public ZCom_Control
{
public:
// constructor - gets called when the client is created with new Client(...)
Client()
{
// this will allocate the sockets and create local bindings
if ( !ZCom_initSockets( true, 0, 0, 0 ) )
{
printf("Failed to initialize sockets!\n");
// exit program immediately
exit(255);
}
// string shown in log output
ZCom_setDebugName("ZCOM_CLI");
}
protected:
// called when client finished entering zoidlevel
void ZCom_cbZoidResult(ZCom_ConnID _id, eZCom_ZoidResult _result, zU8 _new_level, ZCom_BitStream &_reason)
{
if (_result == eZCom_ZoidEnabled)
printf("Client entered Zoidlevel: %d\n", _new_level);
else
printf("Client failed entering Zoidlevel: %d\n", _new_level);
}
// called when the connecting ends
void ZCom_cbConnectResult( ZCom_ConnID _id, eZCom_ConnectResult _result, ZCom_BitStream &_reply )
{
// connection successful?
if (_result == eZCom_ConnAccepted)
{
ZCom_requestDownstreamLimit(_id, 100, 500);
printf("Connected! Let's request Zoidlevel 1...\n");
ZCom_requestZoidMode(_id, 1);
}
else
{
printf("Connection failed!\n");
ZoidCom::Sleep(2000);
// when connection failed, exit program
exit(255);
}
}
// called when a connection closed
void ZCom_cbConnectionClosed( ZCom_ConnID _id, eZCom_CloseReason _reason, ZCom_BitStream &_reasondata )
{
printf("Connection closed!\n");
ZoidCom::Sleep(2000);
exit(0);
}
// called when data has been received
void ZCom_cbDataReceived( ZCom_ConnID _id, ZCom_BitStream &_data )
{
// we assume that clients will only send strings
// so no special handling, just output of incoming data
printf("Received from server: %s\n", _data.getStringStatic());
}
// unused callbacks are empty
void ZCom_cbNodeRequest_Dynamic( ZCom_ConnID _id, ZCom_ClassID _requested_class, ZCom_BitStream *_announcedata, eZCom_NodeRole _role, ZCom_NodeID _net_id ) {}
void ZCom_cbConnectionSpawned( ZCom_ConnID _id ) {}
bool ZCom_cbConnectionRequest( ZCom_ConnID _id, ZCom_BitStream &_request, ZCom_BitStream &_reply ){return false;}
bool ZCom_cbZoidRequest( ZCom_ConnID _id, zU8 _requested_level, ZCom_BitStream &_reason) {return false;}
bool ZCom_cbDiscoverRequest(const ZCom_Address &_addr, ZCom_BitStream &_request, ZCom_BitStream &_reply) {return false;}
void ZCom_cbNodeRequest_Tag( ZCom_ConnID _id, ZCom_ClassID _requested_class, ZCom_BitStream *_announcedata, eZCom_NodeRole _role, zU32 _tag ) {}
void ZCom_cbDiscovered( const ZCom_Address & _addr, ZCom_BitStream &_reply ) {}
}; |
Partager