Sign in to follow this  
Demx

RPG server - client architecture

Recommended Posts

hi,
i've been making a client - server architecture and id like to know if the design is ok [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

There are 2 structs that contain the player information

[code]

struct ClientPacket
{

Player p;

};

struct ServerPacket
{

Player players[5];

};
[/code]

The array Player players[5] contains the information of all the players

The server is basicly this :


[code]

class ServerAppFramework
{
public:
ServerAppFramework();
~ServerAppFramework();
bool IsConnected(){return m_bIsConnected;} // returns connection status
void StartListenClient(); // Listen to client
int SendMessagePort( SocketPort ); // Send message to all clients.
int RecClient(SOCKET sRecSocket); // receive message for a particulat socket
private:
bool m_bIsConnected; // true - connected false - not connected
int m_iServerPort;
list<SOCKET> m_vClientList; // All socket connected to client
SOCKET m_SClient;
SOCKET m_SListenClient; // socket listening for client calls
};
[/code]


Implementation of SendMessagePort( SocketPort ) :
This sends the packet to all clients

[code]

int ServerAppFramework::SendMessagePort( SocketPort sp)
{
int iStat = 0,i,j,k;
list<SOCKET>::iterator itl;

if(m_vClientList.size() == 0)
return 0;

for(i=0;i<5;i++)
{
spacket.players[i].id = player_server_data[i].id;
spacket.players[i].x = player_server_data[i].x;
spacket.players[i].y = player_server_data[i].y;
spacket.players[i].plane = player_server_data[i].plane;
spacket.players[i].xvel = player_server_data[i].xvel;
spacket.players[i].yvel = player_server_data[i].yvel;
spacket.players[i].curhp = player_server_data[i].curhp;
spacket.players[i].hp = player_server_data[i].hp;
spacket.players[i].level = player_server_data[i].level;
spacket.players[i].exp = player_server_data[i].exp;
spacket.players[i].stunned = player_server_data[i].stunned;
spacket.players[i].slowed = player_server_data[i].slowed;

}

for(itl = m_vClientList.begin();itl != m_vClientList.end();itl++)
{
iStat = send(*itl,(char const *)&spacket, sizeof(spacket),0);
if(iStat == -1)
m_vClientList.remove(*itl);
}

if(iStat == -1)
return 1;

return 0;

}

[/code]


The RecClient() functions recieves packets from the client and modifies the Player players[5].
The new Player players[5] is therefor sent to all clients for update.

[code]


int AppFramework::RecMessagePort()
{

char com=' ';
int iStat = 0, exit = 0, i;

//iStat = recv(conn,acRetData,4096,0);
iStat = recv(conn,(char *)&spacket,sizeof(spacket),0);
if(iStat == -1)
return 1;

if ( spacket.update == -1 )
{
for(i=0;i<5;i++)
{
players[i].id = spacket.players[i].id;
players[i].x = spacket.players[i].x;
players[i].y = spacket.players[i].y;
players[i].plane = spacket.players[i].plane;
players[i].xvel = spacket.players[i].xvel;
players[i].yvel = spacket.players[i].yvel;
players[i].curhp = spacket.players[i].curhp;
players[i].hp = spacket.players[i].hp;
players[i].level = spacket.players[i].level;
players[i].exp = spacket.players[i].exp;
players[i].stunned = spacket.players[i].stunned;
players[i].slowed = spacket.players[i].slowed;
}


return 0;
}

[/code]

I'd like to know if the methods im using to send and recieve packets are ok for a game

Share this post


Link to post
Share on other sites
Instead of the network class constructing the packet and reasing it, it might be better if the player class had methods like OutOfPacket and BuildPacket which take in/output the packet buffer thing and its size.

Share this post


Link to post
Share on other sites
It appears you are using TCP. TCP guarantees delivery* of each byte it accepts, in order, as a stream. It has no concept of packets or packet boundaries.

It does not guarantee that it will send all the bytes you request. That is, if you try to send() 100 bytes, TCP might accept 50 of them now. It is up to you to remember the other 50 bytes you want to send, or loop until they've all been sent (the latter will potentially block you much more than the former, though blocking in general is still possible either way).

Similarly, when you recv() data, there are no guarantees that it will arrive in the same sized "chunks" you sent it. For example, if you were able to successfully send() the lowercase alphabet in 2 letter couples (with the caveat above), the receiver might receive them all in one big group, or in several bunches at odd boundaries, or each letter in a "packet" of its own.

The only guarantee you have is that the data will eventually arrive*, in order.

If you want a packet based protocol, you must build it on top of TCP, perhaps using one or more of the following common building blocks:
[list]
[*] "Packets end with the byte sequence \r\n"
[list]
[*] Suitable for simple text protocols
[/list]
[*] "Every N bytes is a packet".
[list]
[*]This is if all your packets have some upper fixed length.
[/list]
[*] "A packet starts with a three byte, big endian length field. The packet ends when this number of additional bytes have been consumed from the stream".
[list]
[*] This allows variable length data.
[/list]
[*] "A packet starts with a two byte identifier. Packet length is inferred by the value of this identifier"
[list]
[*] In the last schema, each number might correspong with a packet identifier - byte 0 might represent a "JOIN" message, byte 1 might represent an INPUT message, and so on.
[list]
[*] "If the first byte is 0, then the packet is 10 additional bytes long."
[*] "If the first byte is 1, then the packet is 30 bytes long"
[*] (etc).
[/list]
[/list]
[/list]

Essentially, you need to be prepared to buffer all the data you cannot immediately send. Before sending new data, you should try send as much of the old data as possible. If you cannot clear the backlog, buffer the new data too. If the buffer gets big for an extended period of time, the client might be too slow to catch up and might need to be dropped.

You should also buffer all received data, until you can determine a full "packet" (as you have defined above). Remove the packet from the buffer, and process it (remember, you might have the start of the next packet in the buffer too).

[size=2]* Well, it guarantees delivery or an error code. What I really mean is it won't forget about any data you give it.[/size]

Share this post


Link to post
Share on other sites
Hidden
[quote]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]If you want a packet based protocol, you must build it on top of TCP, perhaps using one or more of the following common building blocks[/left][/size][/font][/color]
[/quote]

I don't understand the advantages of using a [color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]packet [/left][/size][/font][/color][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]based [/left][/size][/font][/color][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]protocol. The system i'm using isn't giving problems with the sending or recieving of data.[/left][/size][/font][/color]

Other than that is the following method :

[color=#000088]for[/color][color=#666600]([/color][color=#000000]itl [/color][color=#666600]=[/color][color=#000000] m_vClientList[/color][color=#666600].[/color][color=#000088]begin[/color][color=#666600]();[/color][color=#000000]itl [/color][color=#666600]!=[/color][color=#000000] m_vClientList[/color][color=#666600].[/color][color=#000088]end[/color][color=#666600]();[/color][color=#000000]itl[/color][color=#666600]++)[/color]
[color=#666600]{[/color]
[color=#000000]iStat [/color][color=#666600]=[/color][color=#000000] send[/color][color=#666600](*[/color][color=#000000]itl[/color][color=#666600],([/color][color=#000088]char[/color][color=#000000] [/color][color=#000088]const[/color][color=#000000] [/color][color=#666600]*)&[/color][color=#000000]spacket[/color][color=#666600],[/color][color=#000000] [/color][color=#000088]sizeof[/color][color=#666600]([/color][color=#000000]spacket[/color][color=#666600]),[/color][color=#006666]0[/color][color=#666600]);[/color]
[color=#000088]if[/color][color=#666600]([/color][color=#000000]iStat [/color][color=#666600]==[/color][color=#000000] [/color][color=#666600]-[/color][color=#006666]1[/color][color=#666600])[/color]
[color=#000000]m_vClientList[/color][color=#666600].[/color][color=#000000]remove[/color][color=#666600](*[/color][color=#000000]itl[/color][color=#666600]);[/color]
[color=#666600]}[/color]

stable for sending the data to all the clients ?

Share this post


Link to post
Hidden
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][b][left]Quote[/left][/b][/size][/font][/color]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]

If you want a packet based protocol, you must build it on top of TCP, perhaps using one or more of the following common building blocks[/left][/size][/font][/color]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]I don't understand the advantages of using a packet based protocol.. t[/left][/size][/font][/color][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]he system i'm using isn't giving problems with the sending or recieving of data.[/left][/size][/font][/color]


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]Other than that is the following method :[/left][/size][/font][/color]

[color=#000088][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]for[/left][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]([/left][/size][/font][/color][color=#000000][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]itl [/left][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]=[/left][/size][/font][/color][color=#000000][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left] m_vClientList[/left][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left].[/left][/size][/font][/color][color=#000088][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]begin[/left][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]();[/left][/size][/font][/color][color=#000000][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]itl [/left][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]!=[/left][/size][/font][/color][color=#000000][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left] m_vClientList[/left][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left].[/left][/size][/font][/color][color=#000088][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]end[/left][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]();[/left][/size][/font][/color][color=#000000][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]itl[/left][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]++)[/left][/size][/font][/color]
[color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]{[/left][/size][/font][/color]
[color=#000000][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]iStat [/left][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]=[/left][/size][/font][/color][color=#000000][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left] send[/left][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left](*[/left][/size][/font][/color][color=#000000][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]itl[/left][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left],([/left][/size][/font][/color][color=#000088][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]char[/left][/size][/font][/color][color=#000000][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][/size][/font][/color][color=#000088][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]const[/left][/size][/font][/color][color=#000000][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]*)&[/left][/size][/font][/color][color=#000000][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]spacket[/left][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left],[/left][/size][/font][/color][color=#000000][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][/size][/font][/color][color=#000088][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]sizeof[/left][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]([/left][/size][/font][/color][color=#000000][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]spacket[/left][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]),[/left][/size][/font][/color][color=#006666][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]0[/left][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]);[/left][/size][/font][/color]
[color=#000088][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]if[/left][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]([/left][/size][/font][/color][color=#000000][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]iStat [/left][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]==[/left][/size][/font][/color][color=#000000][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]-[/left][/size][/font][/color][color=#006666][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]1[/left][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left])[/left][/size][/font][/color]
[color=#000000][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]m_vClientList[/left][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left].[/left][/size][/font][/color][color=#000000][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]remove[/left][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left](*[/left][/size][/font][/color][color=#000000][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]itl[/left][/size][/font][/color][color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]);[/left][/size][/font][/color]
[color=#666600][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]}[/left][/size][/font][/color]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]stable for sending the data to all the clients ? [/left][/size][/font][/color]

Share this post


Link to post
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][b][left]Quote[/left][/b][/size][/font][/color]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]

If you want a packet based protocol, you must build it on top of TCP, perhaps using one or more of the following common building blocks[/left][/size][/font][/color]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]I don't understand the advantages of using a packet based protocol .. [/left][/size][/font][/color][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left] the system i'm using isn't giving problems with the sending or recieving of data.[/left][/size][/font][/color]


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]Other than that is the following method :[/left][/size][/font][/color]


[color=#000088]for[/color][color=#666600]([/color][color=#000000]itl [/color][color=#666600]=[/color][color=#000000] m_vClientList[/color][color=#666600].[/color][color=#000088]begin[/color][color=#666600]();[/color][color=#000000]itl [/color][color=#666600]!=[/color][color=#000000] m_vClientList[/color][color=#666600].[/color][color=#000088]end[/color][color=#666600]();[/color][color=#000000]itl[/color][color=#666600]++)[/color]
[color=#666600]{[/color]
[color=#000000]iStat [/color][color=#666600]=[/color][color=#000000] send[/color][color=#666600](*[/color][color=#000000]itl[/color][color=#666600],([/color][color=#000088]char[/color][color=#000000] [/color][color=#000088]const[/color][color=#000000] [/color][color=#666600]*)&[/color][color=#000000]spacket[/color][color=#666600],[/color][color=#000000] [/color][color=#000088]sizeof[/color][color=#666600]([/color][color=#000000]spacket[/color][color=#666600]),[/color][color=#006666]0[/color][color=#666600]);[/color]
[color=#000088]if[/color][color=#666600]([/color][color=#000000]iStat [/color][color=#666600]==[/color][color=#000000] [/color][color=#666600]-[/color][color=#006666]1[/color][color=#666600])[/color]
[color=#000000]m_vClientList[/color][color=#666600].[/color][color=#000000]remove[/color][color=#666600](*[/color][color=#000000]itl[/color][color=#666600]);[/color]
[color=#666600]}[/color]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]stable for sending the data to all the clients ? [/left][/size][/font][/color]

Share this post


Link to post
Share on other sites
my favorite architecture is TCP using this rough idea:
server sends a start game message/welcome
player sends server commands (i.e, I want to move to X,Y,Z)
server reads commands, updates player instance through a component controller, sends confirmation of packet retrieval + updates on any components that have changed since last update sent.
while the server was doing that, the user starts making a list of new commands (as it waits) if a command that over rides another comes in, it will remove that command and place itself in (i.e. update where I want to be going) this will form a list of commands, which on retrial of the confirmation will be sent.
as the client was doing this, the server was doing the same, but in updates for components. (edit) you make a player packet instance for each player on the server and the client only has the server it connects to. (/edit)

In this system, the server does all of the processing (i.e. A*) and the client receives updates on components (i.e. location of an entity) and sends requests... With this system hacking is harder as clients have no direct access to restricted data and the clients system can balance the CPU load more heavily as they are not doing complex calculations, so you could optimize a graphical component, making it look better/require less of the GPU.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this