From: Na Na Date: 2012-12-24T08:45:33+09:00 Subject: Ruby game server woes Hi, The following is a bit of a brain dump, as I'm thinking about things I just don't have enough knowledge to think about clearly. I'm hoping in the mess someone might have some insights that will help me. I've been reading and thinking and researching for the last couple of days to work out what I should do. I had an idea to make a game (client/server model, small number of players at a time - 4 usually, 20 at most), and I thought I would use ruby for the game server. My thinking was, I can develop quickly with ruby, and where I need speed I can write a c extension. Best of both worlds, and I'll be able to make something in a much shorter amount of time! To make matters slower, I'm using javascript+html5's canvas for the client. It's a bit slow, but there's plenty of optimisations I have not yet made that will ease the burden, so I'm not stressed there. To communicate between server and client I'm using websockets. To implement that on the ruby side, I'm using eventmachine (em) with em-websocket. That has been working fine, until recently - now, I need more control over how cpu time is allocated between server game loop and network. As I understand it, em loops itself repeatedly to check for new messages, and then act on those messages. To get a game loop running, I put in a periodic timer so that it would call the main game loop repeatedly. However, this design is inefficient. I am noticing wasted cpu cycles that I want to make use of. Network traffic is low, so it would suit me much better if I were to have the main game loop occasionally check for new network packets, rather than the other way. So that's fine, I expect with more work I can ditch em and implement something myself. The problem is, I'm now having second thoughts about ruby. I thought I'd come here to get some second thoughts, and maybe to help me with information I don't have. Grouped, my thoughts are (in no particular order): * Javascript - Looking at this chart (http://benchmarksgame.alioth.debian.org/u32/benchmark.php) javascript v8 runs a lot faster. That leaves me wondering if I should have gone with nodejs in the end. I am not at all eager on the idea of using javascript where I don't need to. However, one advantage is I could put some server code into the client for predictive behaviour to reduce network traffic. That would be a nice advantage, but not critical for my project. I know benchmarks should be taken with a grain of salt, but the speed different is quite large. And then it leaves me wondering, should I just bite the bullet and rewrite the server in c/c++? And then I think, why not just use c extensions in ruby? And then I wonder if c extensions in ruby will be much slower than a raw c app, and then I'm left at the limits of my experience and knowledge! * fibers - Doing some experimenting, I haven't found a way to put the EM loop into a fiber. I had thought perhaps I could have the game loop as the main part of the program, and have it occasionally resume the EM fiber, which would then yield back to the main game loop after checking for new messages. Trouble is, this didn't quite work - that is, I'd create the fiber enclosing the em loop and assign it to a variable, but the variable would be nil, leaving me to suspect some code in EM itself was destroying the fiber. Maybe I need to look back into this. * processes - is there a way to create a new process of a separate ruby app, and communicate via those processes in an efficient way (ie, not just through writing to disk and reading from disk, or a shared sql database)? If so, I could run EM in one process, and have it communicate back information to the main ruby app. I'm not aware of a way to do this, though. Threads are an obvious choice, but I'm using MRI ruby, and it seems that the game ends up with the same speed issues using threads. If there's a way to launch a unique process and have them communicate, then I can also take advantage of multiple cores. So essentially, my question is this - given all these streams of thoughts, are there any insights you can give me on how to approach this? I lack enough knowledge to make an informed choice. Thanks for your time! -- Posted via http://www.ruby-forum.com/.