Thursday, 4 February 2016

Low-Level Programming_Post14

Low-Level Programming: Post 14 04/02/2016

February 4th 2016 is the date of the fifteenth lesson on Low-Level Programming.

Despite being a little late for the lesson, I missed only two slides at the beginning, one of which was the overview for the lesson.

In this week's lesson, we were continuing on the topic about Networking. Below are the main points from the lesson...

  • Networking Cont: Sockets/Packets.
  • Server-Client Setup: How do traditional server client setups function?
  • Peer to Peer Networking: Peer to Peer infrastructure and usages.
Networking

IP Addresses
  • Unique ID: All hosts that support data transfers over a network need an unique address. As covered last week, the IP addresses solves this issue.
  • Public IP: Public IP’s are accessible from the internet, where as private IP’s are reserved for Local hosts. Remember; a private IP will not be routed externally.
Domain Name Systems
  • DNS: IP addresses aren't very user friendly. Instead we have a technology that allows IP -> human readable names.
  • Domain Names: Domain name queries are sent to DNS servers where they return the unique IP of the host associated with it.
Transport Protocols
  • TCP: TCP is a fully fledged error checking protocol. It guarantees delivery, but is slow.
  • UDP: UDP is a best effort solution. It sends the data directly, doesn’t use a connection and doesn’t guarantee delivery.
  • Usages:
  1. When data delivery is essential and speed is not, TCP is most suited.
  2. When real-time response and updates are required than the lightweight nature of UDP makes it ideal!
Sockets

Sockets
  • What are they?: Sockets are used in the transmission between networked devices. They provide the interface between your application and the outside world.
  • Data: Through a socket; data can be both sent and received. Therefore, it’s likely any program involving networks will utilise them.
  • Types: There are multiple types of sockets available, each with their own feature set. I’ll be working with UDP and TCP sockets provided by SFML.
  • TCP: As we know, TCP sockets are connection based. You can’t send or receive data between the hosts until the connection is created. Each client needs it’s own socket.
  • UDP: UDP can send and receive data on the same socket irrespective of the number of clients.
  • Stream: TCP is a stream protocol, there’s no message boundary. Multiple bits of data from different objects can be received as a single ‘packet’ of data.
  • Broadcasting: Additionally, UDP can broadcast data on the network segment. This is often know as multi-casting. TCP can not do this.
  • Server/Client: In order to connect the two nodes via TCP, we’ll need one node listening. In a server client infrastructure you can describe the listening node as the server and the connecting one as the client.
  • Client: The client only requires a SF::TCPSOCKET, which can be used to connect to the server by calling the connect function.
  • Server Sockets: The server needs multiple sockets, one to support each client connected and one to listen for new connections.
  • Listener: We can use the class SF::TCPLISTENER, which is a class specifically designed to listen for incoming connections.
  • Accept: The accept function on the listener is also a blocking call.
  • Connections: As discussed previously, UDP sockets do not need direct connections between the two hosts. However, you need to bind them to a specific port to listen on.
  • Data: The server uses a SF::UDPSOCKET, which once bound can now be used for data.
  • Sending: Sending of data is performed the same way irrespective of the protocol.
  • ARGS: UDP requires two additional args, the address and the port of the recipient.
  • Data: Data can be sent as raw bytes or abstracted into a format known as packets.
Guidance-Sending
  • Sending Data: The send functions take a void* pointer. However, it’s generally a bad idea to send arrays of bytes. Types on different platforms can have different storage requirements.
  • Type Sizes: Types such as int or longs may have different sizes or even different endianness, therefore cannot be sent reliably.
  • Broadcasting: You can also use SF::IPADDRESS::BROADCAST to send a message via UDP to the entire sub-network in a single call.
  • Buffer: The first two args are the data being received and the max size that can be received.
  • Received: The third argument reports how much data was received.
  • UDP: UDP has two additional which show the sender’s IP and port the data arrived from.
Blocking
  • Multiple Clients: A single socket can cause issues when you have multiple clients. You want to be able to listen for any incoming packets from clients and deal with them without first processing other sockets or being held up.
  • Blocking Clients: Instead we should listen on all clients and then process any of the sockets when data is received from a client.
  • Selector: Handily, there’s an SF::SOCKETSELECTOR class that does this for us. It can monitor TCP/UDP sockets as well as the SF::TCPLISTENER for new connections.
  • Wait: The wait function on the selector will return true if there’s data to received by one of the monitored sockets.
  • Socket/Listener: If it’s a socket which is ready it’s a client sending data. If it’s a listener it’s a connection request.
  • Check Monitored: The selector only has handles to the sockets, it does not store them.
Packets

Packets
  • Data Exchange: Exchanging data is more tricky then actually sending it. There’s a need to make sure the data is compatible between hosts. Serialised data sent between hosts is often know as packets.
  • Issues: Endianness, primitive type sizes and TCP packet splitting form the low level problem that can occur.
  • SFML: The SFML library provides ways to circumvent these issues, until you’re more comfortable coding your own solutions.
  • SF Primitive Types: Depending on the platforms being used primitive types can vary in size and endianness. They therefore can not be used for reliably for data exchange.
  • Fixed Types: SFML provides a series of fixed types that are guaranteed to be the same size on other platforms.
SF Packets
  • SF: The sf packet class is designed to get around the remaining two issues i.e. endianness and message boundaries.
  • Streams: sf::Packet works just like a normal stream operator would. You use the << and >> operators.
  • Error Checking: The packet class provides error checking. The result of the stream operator will return false if a reading operation fails.
  • Sending/Receiving Packets (1): Quite simply, replace the call to send with a SF::PACKET instead of void* data.
  • Message Boundaries: Packets solve the message boundary issues, meaning the exact same TCP packet is received as was sent. This comes at the cost of additional bytes which forces the packet to be decoded using the SF::PACKET interface.
  • User Data Types: Overload the stream operators and write / read the classes member’s to it.
  • Sending/Receiving Packets (2): Because of the library’s OOP design, we can easily extend the SF::PACKET class to incorporate additional technology such as encryption or compression.
Infrastructure

Approaches
  • Multi-Player Support: Games that utilise networks can be implemented in one of two ways. Either authoritative or non-authoritative.
  • Client-Server: The client server solution is authoratitive and it’s the structure we’ve already been discussing. i.e. a server and then clients.
  • Peer to Peer: P2P does not use a central entity to manage the game. Every client controls it’s game state and is responsible for updating connected peers.
Authoritative
  • Server: The server controls the whole game world, including logic and client synchronisation.
  • Data: All clients constantly receive and send data directly to the server. The clients have a local representation of the game based on the server’s state.
  • Clients: The clients perform actions, which are then forwarded on to the server. The server then updates it’s game state.
  • Dumb Terminals: The clients themselves can be considered dumb terminals. The client sends the data representing things such as keypresses and mouse input. In response the server replies with a packet containing the state of your game.
  • Interpolation: The client needs to provide some form of interpolation between the updates being sent and received, to give the illusion of smooth movement.
  • Latency: The problem with dumb clients, is the latency between the server saying an action is possible and the client receiving it, leading to a delay in response.
  • Client-Side Prediction: The client in fact, can no longer be just a dumb terminal. Instead it has to predict the movement of your character locally, responding immediately to inputs.
  • Authoritative: The server overrides the client’s prediction at any given time, so the more accurate the better.
Non-Authoritative
  • Peer to Peer: A non-authoritative approach has no central entity to control the game’s state. Every peer must control it’s own and communicate changes to other peers.
  • Simulation: The clients then see two views of the game world. Their own character or avatar and a simulation of all the other player’s game entities.
  • Local: All client actions relating to their own game entity is immediate and responsive. For all other players, the network message needs to be received to update their position or actions.
  • Latency (1): The problem with dumb clients, is the latency between the server saying an action is possible and the client receiving it, leading to a delay in response.
  • Simulation: In order to keep all player’s game world up to date, each client needs to send updates as frequently and as quickly as possible.
  • Delays: Delays in updates being received, may mean the player has moved or performed an action “x” amount of time ago, but the other peers aren't aware.
  • Missing Data: Lost or delay packets can mean player’s appear to teleport on the clients. Local prediction similar to client-server interpolation can be used. The longer the latency, the less accurate the interpolation.
  • Extrapolation: Focuses on moving the local entities based on their current states. It assumes that the entity will not change their route. The entity can then safely be moved according to its current velocity.
  • Latency (2): The key to good interpolation and extrapolation is latency. The lower it is the better they will perform. This is why peers with high pings are often kicked.
After finishing the theoretical side of the lesson, we then moved onto the practical task for this week's lesson. Since there wasn't a mini practical task earlier in the lesson, this practical task was a little longer, to compensate for there not being a mini practical task.

The task for this week was to add a Chat Client to go with our Chat Server from that was started last week.

I tried to fix some errors for the Chat Server program before starting on the Chat Client task. Some I managed to fix, due to basic incorrect user input and where to put the parts of the Chat Server code. Originally, they all went inside of the main for some odd reason, while the main itself was missing some code itself. However, there were still two errors left unfixed in the project. Despite this, however, I managed to do the basic setup for the Chat Client, and added all of the code parts into a new project and set the the project's properties for the Chat Client's liking. Hopefully, I should have all ready for next week.

No comments:

Post a Comment