• 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
Chindril

Handling disconnections with multi-threaded

4 posts in this topic

This question is a bit specific to C++ since it is the language I use for my network game.

My game server runs the network in the main loop, and every network messages are sent in a queue. Then I have a bunch of Workers (each one running in their own thread) that takes the network messages and do the appropriated job (Lets call them WorkerJob).

I have 2 classes representing a client. I have the class User, which represent the network connection itself, and Player, which represent the logged player. A User can have a Player (if logged). Every WorkerJob has a pointer to the User that sent the network message.

Now the issue is that when I received a network disconnection (main thread), I need to force a logout of the Player and clean it as well as the User. The problem arise if I have WorkerJobs currently processing or in the queue that have pointers to this specific User.

What I'm trying to do is on a player's logout (either manually or when connection is lost in any way), I need to remove the queued network messages from this player, wait until the WorkerJobs are done and THEN cleanup my pointers. However I cannot wait since the disconnection happens in the main thread and blocking there is out of the question.

Anyone has experience with something similar and could give me tips on pitfalls to avoid ?

Thanks,
0

Share this post


Link to post
Share on other sites
I used a timestamp to identify the connection a message is comming from. So if a user disconnects and connects very fast it is possible to identify that a message that should be send to the user is related to a connection that does not exists anymore even if it is the same user. This is a scenario for a web-server where you hold down the reload button.
The web-server gets a request the networking thread processes the incoming HTTP-Request and sends the already parsed request to the loader stage.
While the loader prepares the answer the closing happens and the loader sends a document for a connection that does not exist any more. The communication stales at some time.
The timestamp gets imporant because the memory management may give you the same pointer for the same client but for a new request. If the answer from the loader stage arrives you do not know whether it is for an old request of for the new one.
The sender drops the message if the timestamp in the loader stage answer message is not the same as the timestamp of the connection.
1

Share this post


Link to post
Share on other sites
You can solve this problem either by reference counting the player structure (so the processors will still process messages, until all messages are gone) or locking each user message queue, and locking, flushing, and posting a "user disconnected -- kill this user" message on that queue.

You probably still need some reference counting if you allow multiple worker threads to process messages for the same user -- a better approach might be to use a boost::asio::strand (or emulate that same functionality for whatever threading you use.) This means messages for a particular user are always funneled through a single thread, so you just post the "this user disconnected" message, and then messages will be processed in order until the disconnect is reached.
1

Share this post


Link to post
Share on other sites
@Tribad: My case is a bit different than a HTTP server. My connections are persistant and if disconnected I will drop the corresponding messages. What I'll probably do if mark the user as disconnected as soon as it happens, but keep it in memory until all it's messages are processed / drop and then cleanup the user.

@hplus0603: I'm probably gonna change my raw pointers to boost::shared_ptr in this case so they are reference counted. This should fix my problem of sharing and memory deletion. About boost::asio::strand, your idea is very interesting. If I can manage to have all the user's messages processed in the same thread it would fix a whole lot of my problems. I never touched boost::asio however (I'm using boost::thread for my threading), and I'll need to spend a lot of time to understand it before implementing it in my code. I might try to implement something like that myself as the logic itself is pretty straightforward.

Thanks to both of you, Edited by Chindril
0

Share this post


Link to post
Share on other sites
Just to let you guys know, I implemented the solution of my last post and it seems to be working very well so far. I'll need much more testing and profiling to be sure it's 100% stable but for now I'll stick with this. Thanks for your help.
0

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