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

using apache, custom server, and php for real-time game?

11 posts in this topic

this might be a bit weird to go about things, but let me just put it this way: I have a public web server, I can run custom applications on said server, but i can't open public ports for my programs(so i only have apache+php). This is what i want to do(and i've done it for a non-persistant games in the past) use php to communicate with my custom server software(i know how to do this), and keep a connection open through apache/php to my clients, and stream data through that connection. what i don't know, is that if i keep these connections open, well apache start to drop client requests while other clients are connected? my php scripts communicate to my server by opening a localhost connection to the server, is apache/php intelligent enough to put a connection to "sleep" while it's waiting for a response from the server? I don't know what to google to find information about doing such a thing, has anyone tried this before?
1

Share this post


Link to post
Share on other sites
thanks frob, long-polling looks like what i want. however, looking over the wiki article on how it's implemented, from what i understand, when it receives a response, it opens a new connection to the server. is this the only practical way to continuously stream data? i've been running alot of tests, and it would seem so. Since i can't seem to force apache to flush small bits of data on demand(the only way i've figured out how to get apache to send a stream of data is via padding it with alot of blank data.)
0

Share this post


Link to post
Share on other sites
You're trying to force a square peg through a round hole.

If you need a persistent connection with real-time data updates, you need a TCP connection, not a HTTP connection. If you HAVE to do it with servers and clients you don't have full control over, look into Websockets and see if they are supported in your environment. Without websockets, COMET-style requests are your best bet.

Typically, you'll want to double-buffer the requests.
2

Share this post


Link to post
Share on other sites

You're trying to force a square peg through a round hole.

If you need a persistent connection with real-time data updates, you need a TCP connection, not a HTTP connection. If you HAVE to do it with servers and clients you don't have full control over, look into Websockets and see if they are supported in your environment. Without websockets, COMET-style requests are your best bet.

Typically, you'll want to double-buffer the requests.

 

yea, i know this isn't the ideal scenario to get things working, but i don't have a dedicated public server that i have access to at the moment, so i'm trying to cut the sides off that square peg to make it fit.

 

this is a c# application, not a browser application, so i have full control over how the client connects.

 

I like the idea of double-buffering requests, so if i understand what your saying correctly, i attempt to maintain two active connections with the server, so when i get a response, the other connection is still their while i attempt to re-create another connection.

0

Share this post


Link to post
Share on other sites

i attempt to maintain two active connections with the server, so when i get a response, the other connection is still their while i attempt to re-create another connection.

Yup. It works best when the server can know that there are two requests incoming, and returns the data for the first request when the second request comes in. If you're using plain PHP, you may need to use shared state through something like memcache to make that work, and you'd need to be polling or something to actually sequence it right. It's a right mess when all you have is a "single process per request" model.
0

Share this post


Link to post
Share on other sites

i attempt to maintain two active connections with the server, so when i get a response, the other connection is still their while i attempt to re-create another connection.

Yup. It works best when the server can know that there are two requests incoming, and returns the data for the first request when the second request comes in. If you're using plain PHP, you may need to use shared state through something like memcache to make that work, and you'd need to be polling or something to actually sequence it right. It's a right mess when all you have is a "single process per request" model.

well, what i was thinking is doing something like so:

1. client makes request to server, php takes the request, and passes it to my custom server.

2. custom server receives request from php(which begins waiting for a response from the server, before responding to the client.)

3. my curstom server checks who is requesting the data, and adds the request into that user's queue.

4. once the server has data to submit, it selects the first available php request, and submits the data(then the php request hands that back to the client).

5. if no request is availble, the data is buffered while waiting for a request to come in.

 

do you see anything wrong with this approach?

Edited by slicer4ever
0

Share this post


Link to post
Share on other sites

 

i attempt to maintain two active connections with the server, so when i get a response, the other connection is still their while i attempt to re-create another connection.

Yup. It works best when the server can know that there are two requests incoming, and returns the data for the first request when the second request comes in. If you're using plain PHP, you may need to use shared state through something like memcache to make that work, and you'd need to be polling or something to actually sequence it right. It's a right mess when all you have is a "single process per request" model.

well, what i was thinking is doing something like so:

1. client makes request to server, php takes the request, and passes it to my custom server.

2. custom server receives request from php(which begins waiting for a response from the server, before responding to the client.)

3. my curstom server checks who is requesting the data, and adds the request into that user's queue.

4. once the server has data to submit, it selects the first available php request, and submits the data(then the php request hands that back to the client).

5. if no request is availble, the data is buffered while waiting for a request to come in.

 

do you see anything wrong with this approach?

 

Everything.

Ok, let's be blunt: I would not allow this approach in my office. It is crap. So many things can go wrong it is scary.

Communicate over TCP. Find another hosting option.

Sorry for not helping with this response.

-1

Share this post


Link to post
Share on other sites

 

 

i attempt to maintain two active connections with the server, so when i get a response, the other connection is still their while i attempt to re-create another connection.

Yup. It works best when the server can know that there are two requests incoming, and returns the data for the first request when the second request comes in. If you're using plain PHP, you may need to use shared state through something like memcache to make that work, and you'd need to be polling or something to actually sequence it right. It's a right mess when all you have is a "single process per request" model.

well, what i was thinking is doing something like so:

1. client makes request to server, php takes the request, and passes it to my custom server.

2. custom server receives request from php(which begins waiting for a response from the server, before responding to the client.)

3. my curstom server checks who is requesting the data, and adds the request into that user's queue.

4. once the server has data to submit, it selects the first available php request, and submits the data(then the php request hands that back to the client).

5. if no request is availble, the data is buffered while waiting for a request to come in.

 

do you see anything wrong with this approach?

 

Everything.

Ok, let's be blunt: I would not allow this approach in my office. It is crap. So many things can go wrong it is scary.

Communicate over TCP. Find another hosting option.

Sorry for not helping with this response.

well, perhaps if you'd actually point out why/how "so many things can go wrong", or provide some examples, i'd actually have a better idea of why such things could go wrong, instead of just walking in, and saying "Nope, find another way, but i'm not going to give you any reason why it won't work."  I think i've made it clear that i'd choose another path if i could, so instead of re-iterating a known fact, you might actually be....helpful and tell me why this path is so adamant to failing,

Edited by slicer4ever
0

Share this post


Link to post
Share on other sites

 

 

 

i attempt to maintain two active connections with the server, so when i get a response, the other connection is still their while i attempt to re-create another connection.

Yup. It works best when the server can know that there are two requests incoming, and returns the data for the first request when the second request comes in. If you're using plain PHP, you may need to use shared state through something like memcache to make that work, and you'd need to be polling or something to actually sequence it right. It's a right mess when all you have is a "single process per request" model.

well, what i was thinking is doing something like so:

1. client makes request to server, php takes the request, and passes it to my custom server.

2. custom server receives request from php(which begins waiting for a response from the server, before responding to the client.)

3. my curstom server checks who is requesting the data, and adds the request into that user's queue.

4. once the server has data to submit, it selects the first available php request, and submits the data(then the php request hands that back to the client).

5. if no request is availble, the data is buffered while waiting for a request to come in.

 

do you see anything wrong with this approach?

 

Everything.

Ok, let's be blunt: I would not allow this approach in my office. It is crap. So many things can go wrong it is scary.

Communicate over TCP. Find another hosting option.

Sorry for not helping with this response.

well, perhaps if you'd actually point out why/how "so many things can go wrong", or provide some examples, i'd actually have a better idea of why such things could go wrong, instead of just walking in, and saying "Nope, find another way, but i'm not going to give you any reason why it won't work."  I think i've made it clear that i'd choose another path if i could, so instead of re-iterating a known fact, you might actually be....helpful and tell me why this path is so adamant to failing,

 

Oh, it will work. I haven't said that it won't. Let me remember what I thought about while reading about this idea.

 

1. You need to keep open connection AND fire additional requests per each game client action. I don't know how often you have to update data for other players to know about it, but opening a new TCP connection for every mouse click feels like an issue. Might be fine if it is a chess-like game.

 

2. You need to know that your streaming connection is dead to create streaming connection. Detect lost connection. Possible approaches would be:

  • Synchronization over file: write to file "connection streaming" = true at the start of stream, delete file at the end.
  • Same with in-memory cache like apc, memcache, or even DB.
  • Do that by notifying your custom server about start and end.

All three of above will fail as soon as you loose connection for some reason. On possible solution is to use posix_getpid function to retrieve process number for current PHP request. Then you can later check if that process is still running. Though I am not sure what to do if several connections are running in the same process.

 

Somewhat reliable solution would be to keep pinging your custom server at regular interval and consider stream dead if no ping happens in set amount of time.

 

Or, you can trust the client to create stream again if it thinks that stream is dead.

 

3. You have to create a new TCP connection to your custom localhost server per every HTTP connection from your game. Make sure your custom server can handle it. Also there is default 60 second socket close timeout (which waits for socket in case it is not closed). If you over-saturate number of incoming connections, you can no longer accept new connections until timeout expires. So you may not get new updates from player if every update means a new connection. Apache might just help you there with keep-alive connections. However, you get no persistence between PHP requests: all your variables and open connections are lost as soon as request ends, so you have to re-open them.

 

Well, if a game requires rare updates, no more than 10 per second from all connected clients (this guess is based on "470 sockets by default at any given time"), it should be enough. Otherwise you need to modify networking settings for your server.

 

4. Your PHP script should dumb-redirect all the data to your server and dumb-pump data back over streaming connection, no other stuff. I think this is obvious. All the synchronization logic can be in a single place, on your custom server.

 

5. You will have to do authentication for every request. Not only for logging in, but also just for telling requests apart. PHP sessions might work fine, you will just need cookie support on your game client and you have to keep in mind that you probably can't send a cookie over stream connection in usual way - some custom work will be needed there. Or, you can do some token-based identification and trust your client to send correct tokens, ensure expiration, etc.

 

These are few points from top of my head, therefore I suggested to avoid doing TCP over PHP.

But it is doable, I am not arguing about that :)

1

Share this post


Link to post
Share on other sites

...snip...

 

thank you for the informative post=-)!, i believe i can overcome several of these issues.  it's mostly issue #3 that has me the most worried, apache intentionally dropping connections due to over-saturation well be a leading problem in making this work I think.

 

I'm building my server in a way that in the future when i can get a dedicated server, I'll be able to switch to communicating with it directly(i'm trying to keep http connections to being as transparent as possible in terms of game logic/communication.)

 

as for point 5, authentication is very thin(actual matchs won't normally last longer than 5 minutes, and their well be no concept of users, so no password/token tables) i simply pass a unique id for the game, and another unique id to represent the user for that game.

Edited by slicer4ever
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