I would like to improve my servers code.

Started by
1 comment, last by GameDev.net 17 years, 7 months ago
I realize some of this is awful, especially the "send an update about all players to all players every millisecond", I was wondering how I can improve my code. The code:

#include <winsock2.h>
#include <iostream>
#include <vector>
#include "networking.h"
#include "logic.h"
#include "timer.h"
#include "Entities/GameObject.h"
#include "Entities/player.h"

int main(){
	GameTimer timer;
	timer.Update();
	GameLogic logic;
	std::vector<GameObject*> SceneGraph;
	SuperServerSocket Server(5001);
	for (;;){
		timer.Update();
		logic.UpdateGame(timer.GetDifference(), &SceneGraph, &timer);
	
		if (Server.AcceptConnection()) std::cout << "Connection Accepted.\n";
		for (int i = 0; i < (int)Server.Clients.size(); i++){
			while (Server.RecvData(i, sizeof(PacketType) + sizeof(int)) > -1){
				PacketType pType;
				int size;
				pType = Server.UnpackPacketType();
				size = Server.UnpackInt();
				if (size > 0) Server.RecvData(i, size);
		
				switch (pType){
					case CONNECT:
						std::cout << "Received Connection Request Packet.\n";
						Server.PackData(CONNECT);
						Server.PackData(0);
						Server.SendData(i);
						Server.ClearOutBuffer();
						
						for (int x = 0; x < SceneGraph.size(); x++){
							Server.PackData(ADDPLAYER);
							Server.PackData((int)sizeof(int));
							Server.PackData(SceneGraph[x]->GetID());

							Server.PackData(UPDATEPLAYERPOS);
							Server.PackData((int)(sizeof(int) + (sizeof(float) * 2)));
							Server.PackData(SceneGraph[x]->GetID());
							Server.PackData(SceneGraph[x]->GetPosition()[0]);
							Server.PackData(SceneGraph[x]->GetPosition()[1]);
							
							Server.PackData(UPDATEPLAYERVELOCITY);
							Server.PackData((int)(sizeof(int) + (sizeof(float) * 2)));
							Server.PackData(SceneGraph[x]->GetID());
							Server.PackData(SceneGraph[x]->GetVelocity()[0]);
							Server.PackData(SceneGraph[x]->GetVelocity()[1]);

							Server.SendData(i);
							Server.ClearOutBuffer();
							
						}
						
						SceneGraph.push_back(new player(vec2(10, 10), i));
						
						Server.Clients.Character = (player *)SceneGraph[SceneGraph.size() - 1];
						
						for (int x = 0; x < (int)Server.Clients.size(); x++){
							Server.PackData(ADDPLAYER);
							Server.PackData((int)sizeof(int));
							Server.PackData(i);
							Server.SendData(x);
							Server.ClearOutBuffer();
						}
						
						
						break;
					
					case HEARTBEAT:
						
						break;
						
					case ADDPLAYER:
						break;
						
					case KEYSTATE:
						{
							bool keys[4];
							keys[0] = Server.UnpackBool();
							keys[1] = Server.UnpackBool();
							keys[2] = Server.UnpackBool();
							keys[3] = Server.UnpackBool();
							Server.Clients.Character->UpdateKeyState(keys);
						}
						break;
						
					case DISCONNECT:
						
						break;
						
					default:
						
						break;
				}
				Server.ClearInBuffer();
			}
			for (int x = 0; x < SceneGraph.size(); x++){
				Server.PackData(UPDATEPLAYERPOS);
				Server.PackData((int)(sizeof(int) + (sizeof(float) * 2)));
				Server.PackData(SceneGraph[x]->GetID());
				Server.PackData(SceneGraph[x]->GetPosition()[0]);
				Server.PackData(SceneGraph[x]->GetPosition()[1]);
				Server.SendData(i);
				Server.ClearOutBuffer();
			}
		}
		Sleep(1);
	}
	return 0;
}

Advertisement
definitely multithreading

redundant calls to same api to pack data, etc.

Kuphryn
You only have to build the 'everyones position/state' data once after all the
incomming commands are processed. If its within the same subnet you could even use a 'broadcast' mode (send one packet that all computers on a LAN....)

If you were to customize it each time (like send only a data subset -- within Line of sight or within a box radius of the object) then you would have to rebuild the data to send to an object.


All the calls to pack data might be wasteful if you have fixed message data structures. I use STRUCTs and insert the data into the elements directly and the have a send fuction that accepts a buffer pointer (pointing at the temp msg STRUCT) and a buffer length ( sizeof(STRUCTX_TYPE) ) a decoder on the recieving end overlays the incoming buffer with the same STRUCT to allow extracting the values directly.



Also shouldnt you be sending updates for object velocities somewhere????

This topic is closed to new replies.

Advertisement