#include <iostream>
#include <cstdlib>
#include "../Ganef/client/clientframework.h"
#include "../Ganef/client/clientdata.h"
class MyClientDataChild : public ClientData
{
private:
int x,y;
std::string name;
public:
MyClientDataChild(ClientFramework * f, ClientPacket * p) : ClientData(f,p)
{
x = p->readInt32();
y = p->readInt32();
name = p->readString();
std::cout << "MyClientDataChild(";
std::cout << "ID: " << getFrameworkId();
std::cout << ",x:"<<x<<",y:"<<y<<")" << std::endl;
}
virtual void updatePacket(ClientFramework * f, unsigned char updatetype, ClientPacket * packet)
{
}
static ClientData * constructMyClientData(ClientFramework * f, ClientPacket * p)
{
return new MyClientDataChild(f,p);
}
};
class MyClientData : public ClientData
{
private:
std::string name;
MyClientDataChild * children[3];
public:
MyClientData(ClientFramework * f, ClientPacket * p) : ClientData(f,p)
{
name = p->readString();
std::cout << "Constructing: MyClientData(ID: " << getFrameworkId() <<",name:"<<name<<")"<< std::endl;
children[0] = p->readObject<MyClientDataChild>(f);
children[1] = p->readObject<MyClientDataChild>(f);
children[2] = p->readObject<MyClientDataChild>(f);
}
virtual void updatePacket(ClientFramework * f, unsigned char updatetype, ClientPacket * packet)
{
}
static ClientData * constructMyClientData(ClientFramework * f, ClientPacket * p)
{
return new MyClientData(f,p);
}
};
class TestClientFramework : public ClientFramework
{
private:
bool is_running;
std::string server_hostname;
public:
TestClientFramework(const std::string & server_hostname) : ClientFramework(int(0)),server_hostname(server_hostname)
{
registerConstructor(0x424242, MyClientData::constructMyClientData);
}
~TestClientFramework()
{
};
protected:
virtual void writeHandshakeInitMessage( ClientPacket * fp ){}
virtual void onHandshakingDiscontinue( ClientPacket * fp ){}
virtual bool onHandshakeDescription( ClientPacket * fp, ClientPacket * reply )
{
return true;
}
virtual void onDisconnect( ) { std::cout << "Disconnected\n"; }
virtual void onConnect( ){ std::cout << "Connected\n"; }
public:
void mainLoop( )
{
std::cout << "Connecting to: " << server_hostname <<":"<< 8000<< std::endl;
initServerConnection(server_hostname, 8000);
is_running = true;
while(is_running)
{
keepAlive();
psleep(50);
}
}
};
int main(int argc, char *argv[])
{
std::string server_host("localhost");
if(argc > 1)
server_host = argv[1];
TestClientFramework app(server_host);
app.mainLoop();
}
#include <iostream>
#include <cstdlib>
#include <vector>
#include "../Ganef/server/serverframework.h"
class MyServerDataChild : public ServerData
{
private:
int x,y;
std::string name;
public:
MyServerDataChild(ServerFramework * fw, int x, int y, const std::string & name) : ServerData(fw, 10, false),x(x),y(y),name(name)
{
};
public:
virtual const unsigned int getClassId() const { return 0x123456; };
virtual void fillUpdatePacket(ServerPacket * packet, unsigned char type) const
{
}
virtual void fillCreateObjectPacket(ServerPacket * packet) const
{
packet->writeInt32(x);
packet->writeInt32(y);
packet->writeString(name);
}
virtual void clientPacket(Client * client, unsigned char type, ServerPacket * packet)
{
}
};
class MyServerData : public ServerData
{
private:
std::string name;
MyServerDataChild * child1;
MyServerDataChild * child2;
MyServerDataChild * child3;
public:
MyServerData(ServerFramework * fw, const std::string & name) : ServerData(fw, 10, false),name(name)
{
child1 = new MyServerDataChild(fw, 100, 150, "Child one");
child2 = new MyServerDataChild(fw, 130, 250, "Child two");
child3 = new MyServerDataChild(fw, 160, 350, "Child three");
};
public:
virtual const unsigned int getClassId() const { return 0x424242; };
virtual void fillUpdatePacket(ServerPacket * packet, unsigned char type) const
{
}
virtual void fillCreateObjectPacket(ServerPacket * packet) const
{
packet->writeString(name);
packet->writeObject(child1);
packet->writeObject(child2);
packet->writeObject(child3);
}
virtual void clientPacket(Client * client, unsigned char type, ServerPacket * packet)
{
}
};
class TestServerFramework : public ServerFramework
{
MyServerData * mydata;
public:
TestServerFramework() : ServerFramework(8000)
{
}
~TestServerFramework()
{
}
protected:
virtual bool onInitialHandshake(ServerPacket * handshake, ServerPacket * reply)
{
return true;
}
virtual void onClientConnected(Client * client)
{
std::cout << "Client: " << client->getId() << " connected\n";
mydata->sendCreateObject(client);
}
virtual void onClientDisconnected(Client * client)
{
std::cout << "Client: " << client->getId() << " disconnected\n";
}
virtual Client * createClient(const ipaddress &addr, int port)
{
return new Client(this, addr, port);
}
public:
void mainLoop()
{
std::cout << "TestServerFramework: Running\n";
mydata = new MyServerData(this, "My Parent Data Object");
while(true)
{
keepAlive();
psleep(100);
}
}
};
int main(int argc, char *argv[])
{
try
{
TestServerFramework framework;
framework.start();
framework.mainLoop();
}
catch(FrameworkError *err)
{
std::cout << "FrameworkError:" << err->msg << std::endl;
}
catch(PacketError *err)
{
std::cout << "PacketError:" << err->msg << std::endl;
}
return EXIT_SUCCESS;
}