![]() |
Shadowrun: Awakened 29 September 2011 - Build 871
|
#include <WorldServer.h>
Public Member Functions | |
| bool | Initialize (void) |
| WorldServer (void) | |
| ~WorldServer (void) | |
Static Public Member Functions | |
| static void | ChatServerConnectionThread () |
| static void | ClientConnectionThread () |
| static RakPeerInterface * | getChatinterface (const int channelId) |
| Returns the chat server interface that contains the given channel id. If it is not valid, this will return null. | |
| static RakPeerInterface * | getClientInterface () |
| static RakPeerInterface * | getZoneInterface () |
| static void | ZoneServerConnectionThread () |
Definition at line 43 of file WorldServer.h.
| SraNetwork::WorldServer::WorldServer | ( | void | ) |
Definition at line 14 of file WorldServer.cpp.
{
}
| SraNetwork::WorldServer::~WorldServer | ( | void | ) |
Definition at line 18 of file WorldServer.cpp.
{
}
| void SraNetwork::WorldServer::ChatServerConnectionThread | ( | ) | [static] |
Thread for connecting&communicating with the chat process.
... is this safe anyway ?
Definition at line 69 of file WorldServer.cpp.
References SraNetwork::CHAT_SERVER_ADDR, SraNetwork::JoinChannelPacket::clientAddress, RakNet::Packet::data, RakNet::RakPeerInterface::DeallocatePacket(), SraNetwork::JoinChannelPacket::Deserialize(), SraNetwork::SraPacket::Deserialize(), getClientInterface(), SraNetwork::ID_CHAT_CHANNEL_REGISTER, ID_CONNECTION_LOST, ID_NEW_INCOMING_CONNECTION, RakNet::Packet::length, SraNetwork::m_rChatInterface, MEDIUM_PRIORITY, SraNetwork::ReplyPacket::messageOpCode, SraNetwork::ReplyPacket::OK, SraNetwork::SraPacket::opCode, packet, RakNet::RakPeerInterface::Receive(), RELIABLE_ORDERED, SraNetwork::ReplyPacket::replyCode, SraNetwork::ReplyPacket::replyMessage, RakNet::RakPeerInterface::Send(), SraNetwork::ReplyPacket::Serialize(), SraNetwork::SERVER_CHAT_PORT, RakNet::RakPeerInterface::SetMaximumIncomingConnections(), RakNet::RakPeerInterface::Shutdown(), RakNet::RakPeerInterface::Startup(), and stream.
Referenced by main().
{
printf("Listening for chat server ..... \n");
m_rChatInterface = RakPeerInterface::GetInstance();
SocketDescriptor chatSocket(SraNetwork::SERVER_CHAT_PORT, 0);
m_rChatInterface->Startup(2, &chatSocket, 1);
m_rChatInterface->SetMaximumIncomingConnections(2); //2 ?
bool running = true;
bool status = false;
while (running)
{
Packet *packet = m_rChatInterface->Receive();
if (packet)
{
if (packet->data[0] == ID_NEW_INCOMING_CONNECTION)
{
printf("Chat server connected\n");
status = true;
}
running = false;
m_rChatInterface->DeallocatePacket(packet);
}
Sleep(10);
}
if (!status)
{
printf("Error while accepting chat server connection\n");
m_rChatInterface->Shutdown(0);
return;
}
running=true;
while (running)
{
Packet *packet = m_rChatInterface->Receive();
if (packet)
{
//We are processing the chat packets right here, because we have only
//one chat server, so no need for tasks here.....maybe later ^^
if ( packet->data[0] == ID_CONNECTION_LOST )
{
printf("Lost connection to chat server!\n");
running = false;
}
else //if ( p->opCode == ID_CHAT_CHANNEL_REGISTER)
{
// Now for our own packets.
BitStream stream(packet->data, packet->length, false);
SraPacket sraPacket;
sraPacket.Deserialize(&stream);
switch (sraPacket.opCode)
{
case ID_CHAT_CHANNEL_REGISTER:
{
printf("Received a channel registration message from the chat server, forwarding it to the client!\n");
// We will only receive this packet if a
// player has joined a channel succesfully
// So create a forward message with the server ip.
//If the channel join failed, send a reply to the client
JoinChannelPacket jp;
jp.Deserialize(&stream);
RakPeerInterface *clientI = SraNetwork::WorldServer::getClientInterface();
BitStream stream;
ReplyPacket rp;
rp.replyCode = ReplyPacket::OK;
rp.replyMessage = SraNetwork::CHAT_SERVER_ADDR;
rp.messageOpCode = SraNetwork::ID_CHAT_CHANNEL_REGISTER;
rp.Serialize(&stream);
clientI->Send( &stream,
MEDIUM_PRIORITY, RELIABLE_ORDERED, 0, jp.clientAddress, false);
}
break;
}
}
//TODO: Check for other RakNet connection flags.
m_rChatInterface->DeallocatePacket(packet);
}
Sleep(10);
}
}
| void SraNetwork::WorldServer::ClientConnectionThread | ( | ) | [static] |
Thread for connecting clients with the server.
Definition at line 227 of file WorldServer.cpp.
References SraNetwork::MessagePump::AddMessage(), tbb::task::allocate_root(), RakNet::RakPeerInterface::DeallocatePacket(), SraNetwork::MessagePump::Instance, SraNetwork::m_rClientInterface, SraNetwork::MAX_CLIENTS, packet, RakNet::RakPeerInterface::Receive(), SraNetwork::SERVER_CLIENT_PORT, tbb::task::set_ref_count(), RakNet::RakPeerInterface::SetMaximumIncomingConnections(), and RakNet::RakPeerInterface::Startup().
Referenced by main().
{
printf("Listening for clients ....\n");
m_rClientInterface = RakPeerInterface::GetInstance();
//We are now using the RakNet security features:
//if (!m_rClientInterface->InitializeSecurity(NULL, NULL))//InitializeSecurity(public_key,private_key))
//{
// printf("Invalid keys!\n");
//}
SocketDescriptor clientSocket(SraNetwork::SERVER_CLIENT_PORT, 0);
m_rClientInterface->Startup(SraNetwork::MAX_CLIENTS, &clientSocket, 1);
m_rClientInterface->SetMaximumIncomingConnections(MAX_CLIENTS);
//Root task:
tbb::task& rootTask = *new( tbb::task::allocate_root() )tbb::empty_task;
rootTask.set_ref_count( 2 );
//int n = tbb::task_scheduler_init::default_num_threads();
//tbb::task_scheduler_init init(3);
//printf("Num Threads %d\n", n);
while (1)
{
Packet *packet = m_rClientInterface->Receive();
if (packet)
{
// Add message to the queue
MessagePump::Instance->AddMessage( packet );
m_rClientInterface->DeallocatePacket(packet);
}
Sleep(10);
}
}
| RakPeerInterface * SraNetwork::WorldServer::getChatinterface | ( | const int | channelId | ) | [static] |
Definition at line 37 of file WorldServer.cpp.
References SraNetwork::m_rChatInterface.
Referenced by SraNetwork::LeaveChannelMessage::process(), and SraNetwork::JoinChannelMessage::process().
{
//TODO: Support multiple chat server interfaces for
// load balancing.
return m_rChatInterface;
}
| RakPeerInterface * SraNetwork::WorldServer::getClientInterface | ( | ) | [static] |
Definition at line 28 of file WorldServer.cpp.
References SraNetwork::m_rClientInterface.
Referenced by ChatServerConnectionThread(), SraNetwork::LeaveChannelMessage::process(), SraNetwork::JoinChannelMessage::process(), SraNetwork::GetClientCharacterListMessage::process(), SraNetwork::CreateCharacterMessage::process(), and SraNetwork::ClientLoginMessage::process().
{
return m_rClientInterface;
}
| RakPeerInterface * SraNetwork::WorldServer::getZoneInterface | ( | ) | [static] |
One some places we might need to use the RakInterfaces to send messages to a zone or client, so we are using static variables. We can do this because the threads that are using those variables will only receive data!
TODO: Check for connection status before returning those interfaces!
Definition at line 23 of file WorldServer.cpp.
References SraNetwork::m_rZoneInterface.
{
return m_rZoneInterface;
}
| bool SraNetwork::WorldServer::Initialize | ( | void | ) |
Definition at line 44 of file WorldServer.cpp.
References SraNetwork::private_key, and SraNetwork::public_key.
{
//Load key pair
FILE* fp;
fopen_s(&fp, "sra_public.key","r");
if (!fp)
{
printf("Public key not found!\n");
return false;
}
fread(public_key, sizeof(public_key), 1, fp);
fclose(fp);
fopen_s(&fp, "sra_private.key", "r");
if (!fp)
{
printf("Private key not found!\n");
return false;
}
fread(private_key, sizeof(private_key), 1, fp);
fclose(fp);
return true;
}
| void SraNetwork::WorldServer::ZoneServerConnectionThread | ( | ) | [static] |
Thread for zones connecting with the server.
Definition at line 157 of file WorldServer.cpp.
References SraNetwork::SraZonePacket::clientID, SraNetwork::SraPacket2::data, RakNet::Packet::data, RakNet::RakPeerInterface::DeallocatePacket(), SraNetwork::ClientRegister::getAddressFromClientID(), SraNetwork::ClientRegister::getInstance(), HIGH_PRIORITY, SraNetwork::ID_CONNECT_TO_SRV, ID_CONNECTION_LOST, ID_NEW_INCOMING_CONNECTION, if(), SraNetwork::m_rClientInterface, SraNetwork::m_rZoneInterface, SraNetwork::SraPacket2::opcode, packet, SraNetwork::SraPacket2::pID, RakNet::RakPeerInterface::Receive(), RELIABLE_ORDERED, RakNet::RakPeerInterface::Send(), SraNetwork::SERVER_ZONE_PORT, RakNet::RakPeerInterface::SetMaximumIncomingConnections(), RakNet::RakPeerInterface::Shutdown(), RakNet::RakPeerInterface::Startup(), and SraNetwork::SraZonePacket::toZoneID.
Referenced by main().
{
printf("Listening for zones ....\n");
m_rZoneInterface = RakPeerInterface::GetInstance();
SocketDescriptor zoneSocket(SraNetwork::SERVER_ZONE_PORT, 0);
m_rZoneInterface->Startup(2, &zoneSocket, 1);
m_rZoneInterface->SetMaximumIncomingConnections(2);
bool running = true;
bool status = false;
while (running)
{
Packet *packet = m_rZoneInterface->Receive();
if (packet)
{
if (packet->data[0] == ID_NEW_INCOMING_CONNECTION)
{
printf("Zone server connected\n");
status = true;
}
running = false;
m_rZoneInterface->DeallocatePacket(packet);
}
Sleep(10);
}
if (!status)
{
printf("Error while accepting zone server connection\n");
m_rZoneInterface->Shutdown(0);
return;
}
running=true;
while (running)
{
Packet *packet = m_rZoneInterface->Receive();
if (packet)
{
//We are processing the zone packets right here, because we have only
//one zone server, so no need for tasks here.....maybe later ^^
SraZonePacket* p = (SraZonePacket*)packet->data;
if ( packet->data[0] == ID_CONNECTION_LOST ) {
printf("Lost connection to zone server!\n");
running = false;
} //TODO: Check for other RakNet connection flags.
else if ( p->toZoneID != -1)
{
printf("Zone answer received, forwarding to client\n");
//Send ack to client
SraPacket2 answer;
ZeroMemory(&answer,sizeof(answer));
answer.opcode = ID_CONNECT_TO_SRV;
answer.pID = -1;
SystemAddress *sa = const_cast<SystemAddress*>(ClientRegister::getInstance()->getAddressFromClientID(p->clientID));
//We are now sending the address of the zone server and the port:
//TODO Reimpl this
/*std::string str;
str << SraNetwork::ZONE_SERVER_ADDR << ":" << p->toZoneID;
memcpy( answer.data, str.str().c_str(), strlen(str.str().c_str()));*/
//wcscpy_s( answer.data , str.str().c_str());
printf("Sending zone answer : %ls to clientID %d", answer.data, p->clientID);
m_rClientInterface->Send( (char*)&answer, sizeof(answer), HIGH_PRIORITY, RELIABLE_ORDERED, 0,*sa, false);
}
m_rZoneInterface->DeallocatePacket(packet);
}
Sleep(10);
}
}
Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.