I have the client talking to the server. I can type into the edit box and hit enter, selecting the text from the edit box and sending a packet containing the string to the server. The server displays the string from the client. The problem is, when I try to add this line to the server
"net::SOutPacket outpacket;"
to create the packet to send to the client, the server freezes when I try to send the packet from the client. When I use the SOutPacket command on the client it works fine.
Code: Select all
class ServerNetCallback : public net::INetCallback
{
// We will store a pointer to the net manager.
net::INetManager* netManager;
core::stringc str;
public:
ServerNetCallback(net::INetManager* netManagerIn) : netManager(netManagerIn) {}
virtual void handlePacket(net::SInPacket& packet)
{
packet.deCompressPacket();
packet >> str;
stringw mess = str.c_str();
root->getElementFromId(EDIT_ID, true)->setText(mess.c_str());
if(netManager->getConnectionStatus() != net::EICS_FAILED)
{
net::SOutPacket outpacket; //program crashes on this line
outpacket << str.c_str();
outpacket.compressPacket();
netManager->sendOutPacket(outpacket);
}
}
};
Code: Select all
class ServerNetCallback : public net::INetCallback
{
// We will store a pointer to the net manager.
net::INetManager* netManager;
// Here we will store the cannon's power and angle.
// Remember that f32 is just a typedef for float.
f32 cannonPower;
f32 cannonAngle;
public:
ServerNetCallback(net::INetManager* netManagerIn) : netManager(netManagerIn) {}
// Our handlePacket function.
virtual void handlePacket(net::SInPacket& packet)
{
// The packets will use a single char to store
// the packet identifier, remember to use the
// smallest possible datatype for storing your
// packet identifiers. c8 is a typedef for char.
c8 packetid;
packet >> packetid;
// Here we will switch based on the packet id.
switch((E_PACKET_TYPE)packetid)
{
case EPT_ROTATION:
// We obtain the cannon angle from the packet.
packet >> cannonAngle;
std::cout << "The cannon angle is now " << cannonAngle << std::endl;
break;
case EPT_POWER:
// Same here.
packet >> cannonPower;
std::cout << "The cannon power is now " << cannonPower << std::endl;
break;
case EPT_MESSAGE:
// Now this is a special case (Pun intended, or is it even a pun?... nevermind),
// The client has sent us a message as a string, so we will just print that
// to the console.
core::stringc message;
packet >> message;
// We can obtain a unique "player id" from a packet identifying the client that
// sent it. More details about the player id in a later example, for now we will
// just print it out with the message so we know whos saying what.
std::cout << "Client " << packet.getPlayerId() << " says: " << message.c_str();
std::cout << std::endl;
break;
}
// After handling a packet, we will send an updated status of the cannon to all clients.
net::SOutPacket rotationPacket;
// The packet id is the first thing that goes in a packet. Note that I am casting it to a char,
// because that is what we want to store it as, to save space. Be careful to use the same types
// when sending and receiving, don't send as a char and receive as an int, it will cause trouble.
rotationPacket << (c8)EPT_ROTATION;
rotationPacket << cannonAngle;
// Send the packet to all connected clients.
netManager->sendOutPacket(rotationPacket);
// Send a power update too.
net::SOutPacket powerPacket;
powerPacket << (c8)EPT_POWER;
powerPacket << cannonPower;
netManager->sendOutPacket(powerPacket);
}
};
does it have something to do with "netManager->update(1000);"
Sorry in advance for the fragmented text, I'm just trying to get as much info as possible, and before anyone says it I did use the Search feature and I'm still a little confused. Any help, would be welcome. Just point me in the right direction to the answers.