Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (534)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [KryoNet] Game Loop  (Read 4454 times)
0 Members and 1 Guest are viewing this topic.
Offline Conner

Innocent Bystander





« Posted 2012-04-08 03:32:29 »

Perhaps this is obvious, and/or I'm simply way too tired...but how should my server's game loop be handled when using KryoNet?  

My main game logic takes place using an entity system, so I'm not sure how to integrate KryoNet's message handling with this.  For instance, when the user sends a IWantToMovePacket, should I handle the movement in the receiving (KryoNet's) thread?  Couldn't that cause synchronization problems? Or is it exactly that, and I need to synchronize something (and if so, what)?

My problems would be solved if KryoNet could be run in the same thread as the rest of the game, but I don't think that would work.

Any help would be greatly appreciated!
Offline ChexWithRaisins

Senior Newbie


Projects: 1


SphereCCG.com


« Reply #1 - Posted 2012-04-08 05:46:24 »

From what I understand you don't want the Server to handle the task in the receiving because this can block it from receiving additional inputs.  Here's what I did for my game; it may help.

I extended the Kryonet Connection object:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public class UserConnection extends Connection
{
    //any other parameters you might want (user name, etc...)
   private ExecutorService tasks = Executors.newSingleThreadExecutor();

   
   
    public void addTask(Runnable newTask) {
        tasks.execute(newTask);
    }


Then make the server object return your UserConnection object.  In your Server code override new connection to:
1  
2  
3  
4  
5  
protected Connection newConnection() {

                return new UserConnection();
            }
        });


Then override received:
1  
2  
3  
4  
5  
6  
7  
8  
public void received(final Connection connection, final Object object) {
                ((UserConnection) connection).addTask(new Runnable() {

                    public void run() {
                        //game logic here

                    }
                });

Help Beta test SphereCCG!
Offline deathpat
« Reply #2 - Posted 2012-04-08 15:17:28 »

hi,

I did this a different way in my game : when I receive a message, I put it in a ConcurrentLinkedQueue somewhere.
Then at the beginning of my game loop, I read this queue and process the messages. This way I don't have any synchronization issue and I know for sure when my messages are being processed during the game loop :

1  
protected ConcurrentLinkedQueue<AbstractMessage> clientMessageQueue;


listener :
1  
2  
3  
4  
5  
6  
public void received(Connection con, Object msg) {
   super.received(con, msg);
   if (msg instanceof AbstractMessage) {
      clientMessageQueue.add((AbstractMessage)msg);
   }
}


to be called in the game loop :
1  
2  
3  
4  
5  
6  
public void processMessages() {
   AbstractMessage msg;
   while ((msg = clientMessageQueue.poll()) != null) {
      msg.process();
   }
}


hope this helps Smiley

work in progress : D A E D A L U S
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 201



« Reply #3 - Posted 2012-04-08 17:55:46 »

1  
2  
3  
   while ((msg = clientMessageQueue.poll()) != null) {
      msg.process();
   }


Using a threadsafe queue is a fantastic concurrency pattern, but watch out what you do while the queue is null.  You might be interested in BlockingQueue if your behavior is simply to wait til there's a message available.  Even when it does need to do something periodically inbetween mesages, I like sending "tick" messages periodically down the message queue rather than code it into the message loop itself.

Offline deathpat
« Reply #4 - Posted 2012-04-08 18:13:50 »


Using a threadsafe queue is a fantastic concurrency pattern, but this particular bit will spin the CPU to 100% while the queue is empty, will tend to starve the thread that does feed it


I'm not sure to understand your point here, if the queue is empty, the poll method returns null so the loop exits directly ... why would it spin the CPU to 100% ?


You might be interested in BlockingQueue as a replacement.


A Blocking queue is exactly what I don't want, the processMessages() is supposed to be called in the game loop ... so using a BlockingQueue will block the main game loop. I want the processMessage() method to exit immediately if the queue is empty.

work in progress : D A E D A L U S
Offline sproingie

JGO Kernel


Medals: 201



« Reply #5 - Posted 2012-04-08 18:17:10 »

I read the logic in reverse, and edited my post, but not in time, sorry Sad  Queues are a great concurrency pattern, and the non-blocking approach is indeed better when you know you have a batch to process rather than a constant stream with delays between.

(I thought SMF was supposed to warn you if there was a reply while you were editing, but I guess that's just for posts)

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (35 views)
2014-07-24 01:59:36

Riven (33 views)
2014-07-23 21:16:32

Riven (21 views)
2014-07-23 21:07:15

Riven (24 views)
2014-07-23 20:56:16

ctomni231 (55 views)
2014-07-18 06:55:21

Zero Volt (47 views)
2014-07-17 23:47:54

danieldean (38 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!