|
/*
Author: Gabriyel Wong C.K. (gabriyel@gmail.com)
Multiplayer networking code (server) for integration into any game engine.
Code is adopted and modified from Dave Andrew's tutorial on integrating
Raknet and Irrlicht
Copyright (C) 2005 Gabriyel Wong.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <PacketEnumerations.h>
#include <RakNetworkFactory.h>
#include <NetworkTypes.h>
#include <RakServerInterface.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <iostream>
#include "MyPlayer.h"
const unsigned char PACKET_ID_LINE = 100;
void updateStateofClients(RakServerInterface * server, PlayerID clientToExclude, Player p)
{
RakNet::BitStream dataStream;
dataStream.Write(PACKET_ID_LINE); // Magic number
dataStream.Write(clientToExclude.binaryAddress); // This is to notify all about the new player who joined
dataStream.Write(p.position[0]); // ToDo: Automatic destruction
dataStream.Write(p.position[1]);
dataStream.Write(p.position[2]);
dataStream.Write(p.orientation[0]);
dataStream.Write(p.orientation[1]);
dataStream.Write(p.orientation[2]);
dataStream.Write(p.missiles);
dataStream.Write(p.speed);
dataStream.Write(p.health);
server->Send(&dataStream, HIGH_PRIORITY, RELIABLE_ORDERED, 0, clientToExclude, true);
}
void HandlePacket(RakServerInterface * server, Packet * p)
{
unsigned char packetID;
RakNet::BitStream dataStream((const char*)p->data, p->length, false);
dataStream.Read(packetID);
Player inPlayer;
switch(packetID)
{
case PACKET_ID_LINE:
// Note that the clients don't need to include their own IDs
// when they send our packets because the server is able to identify them
// and then attach their address in subsequent packets. Hence the server
// doesn't need to read in any client address.
dataStream.Read(inPlayer.position[0]);
dataStream.Read(inPlayer.position[1]);
dataStream.Read(inPlayer.position[2]);
dataStream.Read(inPlayer.orientation[0]);
dataStream.Read(inPlayer.orientation[1]);
dataStream.Read(inPlayer.orientation[2]);
dataStream.Read(inPlayer.missiles);
dataStream.Read(inPlayer.speed);
dataStream.Read(inPlayer.health);
updateStateofClients(server, p->playerId, inPlayer);
break;
default:
std::cout << "Unhandled packet (not a problem): " << int(packetID) << std::endl;
}
}
int main()
{
RakServerInterface * server = RakNetworkFactory::GetRakServerInterface();
Packet * packet = NULL;
int port = 10000;
if(server->Start(32, 0, 0, port)) // Allows 32 connected players
{
std::cout << "Server started successfully. Listening on port " << port << "... " << std::endl;
std::cout << "Press a key to close server." << std::endl;
}
else
{
std::cout << "There was an error starting the server." << std::endl;
system("pause");
return 0;
}
while(kbhit() == false)
{
Sleep(1);
packet = server->Receive();
if(packet != NULL)
{
HandlePacket(server, packet);
server->DeallocatePacket(packet);
}
}
server->Disconnect(300);
RakNetworkFactory::DestroyRakServerInterface(server);
std::cout << "Server closed successfully." << std::endl;
system("pause");
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.