• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
YyYo

Multiplayer game architecture question

16 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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 [url="http://manpages.courier-mta.org/htmlman2/select.2.html"]select[/url] to check if data is waiting on a connection.

 

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

Edited by slicer4ever
2

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

 

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
1

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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).

0

Share this post


Link to post
Share on other sites
How is the rest of your game currently implemented? Are you using a game oriented library, or Swing, or something else?
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

2

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  
Followers 0