Help im having mega laggs at local network!
Here is my client-code
#include <DarkGDK.h>
#include <MikeNet.h>
int FreeObject(){int i=1;while(dbObjectExist(i)){++i;}return i;}
struct ClientData
{
char* cNick;
int iObj;
bool bActive;
};
void DarkGDK(void)
{
unsigned short ConnectPort = 6789;
int Timeout = 10;
int NoThreads = 0;
int NoInstances = 1;
long long int RecvPacket = mnCreatePacket();
long long int SendPacket = mnCreatePacket();
mnSetMemorySize(SendPacket,1024);
int Result = mnStart(NoInstances,NoThreads);
if(Result == -1)
{ dbPrint("Error starting MikeNet");
dbWaitKey();
return;}
// dbPrint("Enter the IP that you would like to connect to: ");
char * ConnectIP = "127.0.0.1"; // dbInput();
int Connect = mnConnect(0,ConnectIP,ConnectPort,ConnectIP,ConnectPort,Timeout,true);
// delete ConnectIP;
if(Connect == 0)
{ dbPrint("Connection timed out! Check that the server is switched on");
dbWaitKey();
return;}
if(Connect == -1)
{ dbPrint("An error occurred whilst trying to connect");
dbWaitKey();
return;}
if(Connect == 1)
{
dbPrint("Connection was successful!");
dbPrintC("The local TCP IP is: ");
dbPrint(mnGetLocalIPTCP(0));
dbPrintC("The local TCP port is: ");
dbPrint(dbStr(mnGetLocalPortTCP(0)));
dbPrintC("The local UDP IP is: ");
dbPrint(mnGetLocalIPUDP(0));
dbPrintC("The local UDP port is: ");
dbPrint(dbStr(mnGetLocalPortUDP(0)));
dbPrintC("Your client ID is: ");
dbPrint(dbStr(mnGetClientID(0)));
dbPrint("Press any key to continue");
dbWaitKey();
}
const int MaximumClients = mnGetMaxClients(0);
const int MaximumOperations = mnGetMaxOperations(0);
ClientData Clients[ 10 ];
mnAddByte( SendPacket , 1 );
mnAddStringC( SendPacket , "Hi server!" , 0 , true );
// Setup the world for us to run around in
dbSyncOn();
dbSyncRate(40);
dbMakeMatrix(1,5000,5000,50,50);
// dbPositionMatrix(1,dbCameraPositionX(),dbCameraPositionY()-20,dbCameraPositionZ());
int debug = 0;
// Once connected, we loop until we become disconnected or darkgdk tells us to exit
while( (mnClientConnected(0,0) == 1) && (LoopGDK() == true) )
{
int TcpPackets = mnRecvTCP( 0 , RecvPacket , NULL );
if( TcpPackets > 0 )
{
switch( mnGetByte( RecvPacket ) )
{
case 6:
{
int ClientID = mnGetInt( RecvPacket );
Clients[ ClientID ].iObj = FreeObject();
dbMakeObjectCube( Clients[ ClientID ].iObj , 25.0f );
++debug;
break;
}
case 5:
{
int ClientID = mnGetInt( RecvPacket );
float x = mnGetFloat( RecvPacket );
float y = mnGetFloat( RecvPacket );
float z = mnGetFloat( RecvPacket );
dbText( 200 , 0 * 15 , dbStr( x ) );
dbText( 200 , 1 * 15 , dbStr( y ) );
dbText( 200 , 2 * 15 , dbStr( z ) );
dbPositionObject( Clients[ ClientID ].iObj , x , y , z );
dbText( 0 , 7 * 15 , dbStr( ClientID ) );
break;
}
}
}
if( dbKeyState( 17 ) )
{
mnAddByte( SendPacket , 3 );
mnSendTCP( 0 , SendPacket , NULL , false , false );
}
dbText( 0 , 0 * 15 , dbStr( dbObjectPositionX(debug) ) );
dbText( 0 , 1 * 15 , dbStr( dbObjectPositionY(debug) ) );
dbText( 0 , 2 * 15 , dbStr( dbObjectPositionZ(debug) ) );
dbText( 0 , 3 * 15 , dbStr( debug ) );
// Refresh screen
dbSync();
}
// If we have become disconnected from the server
dbPrint("Lost connection to server!");
dbSync();
dbWaitKey();
// Close all ports and deallocate all memory
mnFinish(-1);
mnDeletePacket(RecvPacket);
mnDeletePacket(SendPacket);
return;
}
and here is the server-code
#include <DarkGDK.h>
#include <MikeNet.h>
#include <SC_Collision.h>
#define OP_CHAT_MSG_ALL 1
#define OP_CHAT_MSG_TEAM 2
#define OP_PLAYER_MOVE_FORWARD 3
#define OP_PLAYER_MOVE_BACKWARD 4
#define OP_UPDATE_POSITION 5
#define OP_PLAYER_NEW 6
struct ClientData
{
char* cNick;
int iObj;
bool bActive;
};
int FreeObject(){int i=1;while(dbObjectExist(i)){++i;}return i;}
// Main code
void DarkGDK ( void )
{
dbSetWindowTitle( "- Server -" );
unsigned short LocalPort = 6789;
char * LocalIP = "127.0.0.1";
const int MaxClients = 50;
int MaxOperations = 1;
int UdpMode = UDPMODE_PER_CLIENT_PER_OPERATION;
int NoThreads = 0;
int NoInstances = 1;
int Result;
long long int RecvPacket = mnCreatePacket();
long long int SendPacket = mnCreatePacket();
mnSetMemorySize(SendPacket,1024);
Result = mnStart(NoInstances,NoThreads);
if(Result == -1)
{ dbPrint("Error starting MikeNet");
dbWaitKey();
return;}
Result = mnSetLocal(0,LocalIP,LocalPort,LocalIP,LocalPort);
if(Result == -1)
{ dbPrint("Error setting the local address");
dbWaitKey();
return;}
Result = mnStartServer(0,MaxClients,MaxOperations,UdpMode);
if(Result == 0)
{ dbPrint("Server started");
dbPrint();
dbPrint("Server local TCP port: ");
dbPrint(dbStr(mnGetLocalPortTCP(0)));
dbPrint("Server local UDP port: ");
dbPrint(dbStr(mnGetLocalPortUDP(0)));
} else {
dbPrint("Server failed to start");
dbWaitKey();
return;}
ClientData Clients[ MaxClients + 1];
for( int i = 0; i <= MaxClients + 1; ++i)
{
Clients[ i ].bActive = false;
}
// Now that the server is started we will loop checking for new connections
while( LoopGDK() == true )
{
int ClientJoined = mnClientJoined( 0 );
while( ClientJoined > 0 )
{
dbPrint("Client joined.");
Clients[ ClientJoined ].bActive = true;
Clients[ ClientJoined ].iObj = FreeObject();
dbMakeObjectCube( Clients[ ClientJoined ].iObj , 25.0f );
dbSyncOff( );
for( int ClientID = 1; ClientID <= MaxClients; ++ClientID )
{
if( Clients[ ClientID ].bActive == true )
{
mnAddByte ( SendPacket , OP_PLAYER_NEW );
mnAddInt ( SendPacket , ClientID );
}
}
ClientJoined = mnClientJoined( 0 );
}
int ClientLeft = mnClientLeft( 0 );
while( ClientLeft > 0 )
{
dbPrint("Client left.");
ClientLeft = mnClientLeft( 0 );
}
for( int ClientID = 1; ClientID <= MaxClients; ++ClientID )
{
if( Clients[ ClientID ].bActive )
{
int TcpPackets = mnRecvTCP( 0 , RecvPacket , ClientID );
if( TcpPackets > 0 )
{
switch( mnGetByte( RecvPacket ) )
{
case OP_PLAYER_MOVE_FORWARD:
{
dbMoveObject( Clients[ ClientID ].iObj , 5 );
}
case OP_CHAT_MSG_ALL:
{
break;
}
default:
{
break;
}
}
}
mnAddByte ( SendPacket , OP_UPDATE_POSITION );
mnAddInt ( SendPacket , ClientID );
mnAddFloat ( SendPacket , dbObjectPositionX( Clients[ ClientID ].iObj ) );
mnAddFloat ( SendPacket , dbObjectPositionY( Clients[ ClientID ].iObj ) );
mnAddFloat ( SendPacket , dbObjectPositionZ( Clients[ ClientID ].iObj ) );
mnSendTCPAll( 0 , SendPacket , false , false , NULL );
}
}
// Use less CPU
Sleep(1);
dbSync();
}
// Close all sockets and shutdown MikeNet before exiting
mnFinish(-1);
mnDeletePacket(SendPacket);
mnDeletePacket(RecvPacket);
}
im sending everything through TCP(cause thats more fit able for FPS to dont get any chokey movements.. right?
).
I have tried to turn off my firewall with no difference.
Keep it simple.
Questions? Mail me