Sunday, December 21, 2014

Networking (Week 50 + Week 51)

Where do I start?
Well I started with assignment 3 last week, which consists of making a 3D game using DirectX. I became so overwhelmed (and still am) by the task that I basically did nothing for two weeks! Sure I read about programming patterns and networking and I did dibble in some network programming.

So that’s why I have no blog post from last week. I didn’t do anything worth blogging about.  So a few days ago I finally started working on the assignment.
I have made some basic server network code and a packet managing system and I have also merged my code with the code from our teacher Tommi.

I started out with networking because that’s hard to add when the game is already done. So my basic plan is this layout:



So the client or player does something. That is made into a packet of data that is stored in a queue that the packet handler / manager take care of.  
Next step is that this is sent down to the lower network level that actual take care of the sending and receiving data using UDP. So the packet is sent to the server and put into a queue by the packet handler on the server side and then the server reads and handles the data and updates the game logic and creates a new packet and sends it back to the client or to all clients depending on the logic inside.

As you can see in the picture above the client has its on local game logic. This is so the player can still collide and move around in the world without feedback from the server. This so the player don’t have to wait for half a second if a packet is lost along the way.

When the server has handled everything and the packet is back to the client there will be a check if the client position/ state is different from the server’s calculations. If it is then the client will be corrected to the server data.

Each packet is made up of a packet header and the actual data from the client or the server depending on where it’s created. I haven’t created the client side of the network yet so the code is not tested but this is how a header looks like:


The reason I made a header is so I can keep track of who created the packet. What type of packet it is. When it was created and how big it is and if it’s an outgoing or incoming packet. I don’t know if all this is needed but it’s a start and I’m no experienced network programmer.


If you can't read it then just ask me here and I will allow you access.

Monday, December 8, 2014

Planning the next step. (Week 49)



So this week I haven't done much actual programming. I have just been reading up about different programming patterns and I have also been planning about how to create a server solution for the game we need to make in assignment 3.

Making a game with a multiplayer solution is only needed if you aim for a higher grade, which I do.

So I'm reading up about different things to think about when it comes to game networking. I first found this site with an introduction to multiplayer game programming it only explains the basics of how it works on the surface but then I found this new site and it's quite good at explaining the different problems http://gafferongames.com/networking-for-game-programmers/

So after reading that I have a rough plan on how I want to go around this. I will create a Server/client solution with UDP networking because as far as I have understood its far more effective for games with UDP than using TCP protocol, because TCP always need to make sure every packet gets delivered and that can clog up the traffic and in worst case scenarios create lag in the game if the game always have to wait for each packet being delivered.

I have also understood that since UDP just shoots packets like a machine gun and that I need to somehow check what packets I get and when they were sent and check that against the packets I already received because with UDP there is no guarantee that the first packet sent is the first you receive. I need to do this so check so the players will have a smoother gaming experience.

Other stuff I have been reading up on this week is on this site:
http://gameprogrammingpatterns.com/contents.html it’s a must read for anyone who wants to be a serious programmer. I have read through about half of it and it’s really amazing and it’s easy to read and follow the author. I especially like the part about the command pattern and I’m thinking of incorporate that into my programming.

Why you might ask? Well I have always wondered how teams can program in the same programs without getting all tangled up in each other’s code and I have learned from this site about something called decoupling. It’s when you program in a way that all the modules you make can just be put together without really needing to know much about the other components in the program.
 I want to push myself into learning more advanced programming techniques like this and I feel the command pattern is a good place to start.  Found here 

Monday, December 1, 2014

Done with the Web Server! (Week 48)

So I managed to finish my web server. 

When I started coding I managed to get something up and working in a few hours and I blogged about it last week, but then I realized that there was much more to assignment 2 so I got down to business.

First of all I had to figure out how a web browser operates. Something I knew nothing about. My classmate Jonas explained how the browser sends a “GET” request (e.g. GET / HTTP1.1) to the server and how search paths works when you only use your IP-address in the search field of the browser.

Understanding that I managed to get the information sent by the web browser and saved that info into a buffer. From the buffer I could read what kind of things the browser requested and supported.

I started with managing a simple get request.  When someone enters my web server IP-address I answer by sending header information about the server.

http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

Only a few of these header fields has to be included in the header info the server sends. Content type and the content length is something that needs to be included.

The info that is sent has to be EXACTLY correct or the page never finish loading or don’t load at all. I thought it didn’t matter because I only sent strings with text in them.  It took about a week figuring out that this was what caused most of my troubles. This is what I finally included as my header info:


Another big problem I had was to figure out how to handle timeouts or disconnects from the client side. First I went around this problem by just sending information to the client to see if it arrives, like a “ping” but that felt strange to do every loop so I kept looking for other ways and I found this website


explaining that there is no real sure way to know that the client has lost connection unless the server and client are both programmed to handle this. But since I only make the server side I can’t do that.


So I went with just keeping a timer that updates only when a user sends data to the server. If no data is sent for five minutes the server cuts the connection to the client

























..…but in our assignment it says that we have to respect the browsers connection header so I extracted the information of the connection status from the web browser that is stored in my receiving data buffer and I set the client to keep alive if it’s a keep alive connection and to closed if it’s a closed connection.


I chose to include both capital and small letters in my control because I noticed that the browsers sent different information.

That’s it for this week. I will start with assignment 3 next week. Making a 3D engine and a game….Alone! so that’s a challenge.