Jump to content

  • Log In with Google      Sign In   
  • Create Account


Multiplayer game architecture question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 YyYo   Members   -  Reputation: 137

Like
0Likes
Like

Posted 15 May 2014 - 03:26 AM

Hi...

 

I newbie to forum and game programming but I have a question( probably dummy question...)

 

I am working on a multiplayer game like a Checkers.

My architecture goes like this:

 

After both client sign in to game, and ready to go,

each client, in his turn makes a 'move', and send it to server to authorize it.

After server authorize movement, it then send 2 message:

1) first one for the player which make the 'move', telling him its move is granted and it now should 'wait' for the other opponent to play.

2) second message, goes to opponent, telling him what soldier move, and that now it his 'turn' to play.... bla bla bla

 

my silly question is: When a each client sign-in to game, a new socket created for him for communicating with server

How can I use the socket for BOTH, sending messages to server(like movement), and also for listening to responses from server(like 'your turn', 'wait'...)

is it possible or I should create another socket for 'listening' ?

 

 

Thanks for the help


Edited by YyYo, 15 May 2014 - 01:18 PM.


Sponsor:

#2 slicer4ever   Crossbones+   -  Reputation: 3482

Like
2Likes
Like

Posted 15 May 2014 - 05:42 AM



Hi...

 

I newbie to forum and game programming but I have a question( probably dummy question...)

 

I am working on a multiplayer game like a Checkers.

My architecture goes like this:

 

After both client sign in to game, and ready to go,

each client, in his turn makes a 'move', and send it to server to authorize it.

After server authorize movement, it then send 2 message:

1) first one for the player which make the 'move', telling him its move is granted and it now should 'wait' for the other opponent to play.

2) second message, goes to opponent, telling him what soldier move, and that now it his 'turn' to play.... bla bla bla

 

my silly question is: When a each client sign-in to game, a new socket created for him for communicating with server

How can I use the socket for BOTH, sending messages to server(like movement), and also for listening to responses from server(like 'your turn', 'wait'...)

is it possible or I should create another socket for 'listening' ?

 

 

Thanks for the help

simply call recv on the created socket.  once a socket is created, it's available for both sending and receiving data(unless you specifically disable 1 or the other).  note that recv is a blocking method, so you have to use something like select to check if data is waiting on a connection.

 

I'd recommend reading over beej's guide: http://beej.us/guide/bgnet/ if your not very familiar with networking.


Edited by slicer4ever, 15 May 2014 - 05:43 AM.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#3 Glass_Knife   Moderators   -  Reputation: 4092

Like
2Likes
Like

Posted 15 May 2014 - 07:36 AM


I'd recommend reading over beej's guide: http://beej.us/guide/bgnet/ if your not very familiar with networking.

 

Don't be mislead by the 90s webpage.  This is the place to start.


I think, therefore I am. I think? - "George Carlin"
Indie Game Programming

#4 hplus0603   Moderators   -  Reputation: 5163

Like
1Likes
Like

Posted 16 May 2014 - 07:57 PM

If you are using TCP, then you first create a "listening" socket. This socket is "ready" when some connection is available, and you retrieve a second socket, representing that connection, with accept(). Each time the "listening" socket is ready, you call accept() to generate another socket that represents a new connection. For two players, you need to accept() at least twice.

If you have more than one socket that may become "ready" (have input data or connections available) then you need to use select() to check all the sockets at once. Once select() says that a socket is readable, then one call to recv(), or one call to accept() for listening sockets, is guaranteed not to block.
enum Bool { True, False, FileNotFound };

#5 YyYo   Members   -  Reputation: 137

Like
0Likes
Like

Posted 17 May 2014 - 02:25 PM

Ok, I will use the connection socket for both send and recv with the server.

 

 

.


imply call recv on the created socket.  once a socket is created, it's available for both sending and receiving data(unless you specifically disable 1 or the other).  note that recv is a blocking method, so you have to use something like select to check if data is waiting on a connection

My game is a turn base game, between 2 players, means that only one player has the privlege to play.

why is it bad for the client to block after 'recv()' method? 
When a player is in 'TURN' state, he plays its move, and send it to server, and then goes to 'WAIT' state(by calling recv()) and block until the server 'ACK' to him.
Is it bad decision? or should I do something else?
 
I am sorry for the newbie question but its realy my first game
 
Thx

Edited by YyYo, 17 May 2014 - 02:26 PM.


#6 slicer4ever   Crossbones+   -  Reputation: 3482

Like
1Likes
Like

Posted 17 May 2014 - 03:25 PM

 

Ok, I will use the connection socket for both send and recv with the server.

 

 

.


imply call recv on the created socket.  once a socket is created, it's available for both sending and receiving data(unless you specifically disable 1 or the other).  note that recv is a blocking method, so you have to use something like select to check if data is waiting on a connection

My game is a turn base game, between 2 players, means that only one player has the privlege to play.

why is it bad for the client to block after 'recv()' method? 
When a player is in 'TURN' state, he plays its move, and send it to server, and then goes to 'WAIT' state(by calling recv()) and block until the server 'ACK' to him.
Is it bad decision? or should I do something else?
 
I am sorry for the newbie question but its realy my first game
 
Thx

 

that might be acceptable if your networking is on it's own thread, however if they aren't, blocking means your application can't do anything until it returns, not even quit.  your users can't look around the map, if it is a window application, it can't handle window messages, it just sits and waits forever until you receive some data on that socket.  if you are using tcp you'll at least get a connection closed if the other sides stops talking, but if you are using udp you'll sit forever.

 

in short, don't do it.


Edited by slicer4ever, 18 May 2014 - 07:05 AM.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#7 YyYo   Members   -  Reputation: 137

Like
0Likes
Like

Posted 18 May 2014 - 12:57 AM

OK, now i understand.

My game also has a Chat option, so BLOCKING until recv() is not an option.

I am writing the game with Java, and look for equivalent for select() method with no luck.

 

Is there anything close to select() method in java? or should I try to implement something using a thread

 

Thx


Edited by YyYo, 18 May 2014 - 12:57 AM.


#8 Angus Hollands   Members   -  Reputation: 713

Like
0Likes
Like

Posted 18 May 2014 - 05:41 AM

If you're using UDP, why not use a non-blocking socket?



#9 YyYo   Members   -  Reputation: 137

Like
0Likes
Like

Posted 18 May 2014 - 05:45 AM

I am using TCP, but even if i use UDP with non-blocking socket, how can you implement a state where the client need to wait until a response from server without blocking?

You will implement a loop(true) until some text can be recv() ?


Edited by YyYo, 18 May 2014 - 05:46 AM.


#10 Angus Hollands   Members   -  Reputation: 713

Like
0Likes
Like

Posted 18 May 2014 - 08:05 AM

I am using TCP, but even if i use UDP with non-blocking socket, how can you implement a state where the client need to wait until a response from server without blocking?

You will implement a loop(true) until some text can be recv() ?

If performing async IO, you can simply use a Queue or callbacks when the operation succeeds. So, when messages are received, put them in a message queue, and the chat application can poll that to see if it has any unhandled messages. If so, it can simply take them and display them.

 

Alternatively, define a callback to be executed when a message is received, (which could write to the display, or whatever you like).



#11 rip-off   Moderators   -  Reputation: 8114

Like
0Likes
Like

Posted 18 May 2014 - 02:53 PM

How is the rest of your game currently implemented? Are you using a game oriented library, or Swing, or something else?

#12 YyYo   Members   -  Reputation: 137

Like
0Likes
Like

Posted 19 May 2014 - 12:16 AM

No I am not using a new game library.

Its a multiplayer game, something like Checkers, implemented in Java.

The client application runs on Android.

The server is just a simple TCP server(decided not to use UDP since its not realtime game) which sends/received messages using Json.

 

The Client responsible for player movements, and sends to Server the move for authentication(client received an ACK).

The server sends to opponent the other player movement.

 

BTW, there is no select() in Java(there is the option to use NIO or MINA)

I decided that each client will have 2 threads: one for reading and one for writing, to mimic non-blocking I/O

 

Please feel free to advice since its my first time implementing a game.

 

Thx


Edited by YyYo, 19 May 2014 - 12:19 AM.


#13 hplus0603   Moderators   -  Reputation: 5163

Like
0Likes
Like

Posted 19 May 2014 - 01:04 PM

why is it bad for the client to block after 'recv()' method?


Because the client may also want to respond to user interaction (in the UI) or send chat messages or (if mobile) respond to battery event, network status events, etc.

there is no select() in Java


You can use nio Selectors. Or, since you are on Android, you can use one of the many asynchronous networking libraries that exist. Or just use AsyncTask.
enum Bool { True, False, FileNotFound };

#14 YyYo   Members   -  Reputation: 137

Like
0Likes
Like

Posted 20 May 2014 - 05:59 AM

I was thinking about using AsyncTask but, according to Docs: AsyncTasks should ideally be used for short operations (a few seconds at the most.)

and my client app can wait, in worst case if opponent not make a move, for 15 seconds before turn switch back to him.

 

More over, when the Activity is destroyed, the AsyncTask will still runs, and if you do stuff in onPostExecute() and try to refer to views of the destroyed Activity, you will receive an Exception.


Edited by YyYo, 20 May 2014 - 06:01 AM.


#15 hplus0603   Moderators   -  Reputation: 5163

Like
0Likes
Like

Posted 20 May 2014 - 02:13 PM

when the Activity is destroyed, the AsyncTask will still runs, and if you do stuff in onPostExecute() and try to refer to views of the destroyed Activity, you will receive an Exception


Yes, threaded code requires that you use proper safe coding and notification of users. Even if an AsyncTask doesn't support cancellation, it can support detecting that the view has gone away and stop talking to it.
enum Bool { True, False, FileNotFound };

#16 YyYo   Members   -  Reputation: 137

Like
0Likes
Like

Posted 21 May 2014 - 09:28 AM

Ok I will use AsyncTask.

I currently stuck in a critical decision point, and I really need your help!

I already said its my first game implementation and I don't know if my architecture is correct.

Note: I am using Java/Android.

 

In my Activity, I have 2 objects:

1) Game - which is the game's board and all the game rules including the game loop.
   the player plays according to GameState.

2) ConnectionThread with 2 mainly methods:

2.1) onRead() runs in endless loop and wait for server response.

       When a response received it notify both Game object and Activity.
      A response is used to notify player if he should WAIT/TURN, or to update opponent's soldiers after moving.      

 

2.2) onWrite(String Message) just sends a message to server, and exits

 

Game & Activity assign a callback to Connection's onRead() method so they could be notified if new message received

And Game also has ConnectionThread reference, so it could use its onWrite() method to sends about his moves

 

Is this technique is the right way to implement, or should I do something else?

 

Thx


Edited by YyYo, 21 May 2014 - 09:30 AM.


#17 hplus0603   Moderators   -  Reputation: 5163

Like
2Likes
Like

Posted 21 May 2014 - 04:19 PM

It's impossible to tell based on your description. As long as you solve synchronization such that you never get deadlocks, you're probably OK. Also, you don't want to be spawning a new thread each time you have anything to send to the server, but rather re-use a worker thread that pulls data to send from a queue.

Also, you generally don't want to be notifying more than one piece of the application about incoming data. Instead, it's common to use a model-view-controller system where the network acts as controller, and affects the model; the view then gets notified by the model when data changes. One set of data might be "whose turn is it?" and when that changes, the view changes how it displays itself.


enum Bool { True, False, FileNotFound };




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS