Thursday, 28 January 2016

Low-Level Programming_Post13

Low-Level Programming: Post 13 28/01/2016

January 28th 2016 is the date of the fourteenth lesson on Low-Level Programming.

After clearing up last weeks confusion regarding missing a post on a lesson, everything should be back to normal for the next few weeks.

In this week's lesson, we were taught about Const Correctness & Networking. Below are the main points from the lesson...

  • Const Correctness: What is it and how does it work?
  • Const & Non-Const Functions: How do const and non-const functions work in tandem.
  • Networking: What are data packets and how do we send/receive them?
Const

Constness
  • Const Correctness: Const correctness is fundamentally based on treating data and objects as constant unless we intend to modify it’s value.
  • Semantics: Const allows you to specify a semantic constraint – a particular object should not be modified. Compilers will obey this constraint.
  • Meaning: Const tells both the compiler and programmers that a value should remain invariant. Whenever that’s the case, you should say so i.e. const.
  • Scope: 
  1. Outside of classes; Static at file, function or block scope.
  2. Inside of classes; Both static and non static data members
  • Const Pointers: It is possible to have constant pointers to data, pointer to constant data or even a constant pointer to a constant data address.
  • Syntax: If the word const appears to the left of * it’s constant data, else it’s a constant pointer.
  • Iterators: As covered previously, iterators are just abstracted pointers to data. This means it’s constness is similar to that of pointers.
  • Const Iterator: An iterator that can’t point to a different memory address.
  • Iterator to Const Data: An iterator that can point to any memory address but not amend data.
  • Parameters: When passing user defined types into functions, always use the const declaration where possible.
  • Benefits:
  1. Tells the programmer that the data won’t be modified.
  2. Stops accidental errors such as ‘=‘ when you meant “==“.
  3. Const “may” improve performance with ROM caching.
  • Member Functions: Const member functions identify which functions can be called on const objects.
  • Clarity: They show a clear interface for the class’s interaction with const objects.
  • Const Objects: You can’t work with const objects in non-const functions.
  • Overloading: A member function can be overloaded by it’s constness.
Bitwise Constness
  • Bitwise: A member function is only const if it doesn't modify any of the object’s data members, excluding static variables. i.e. it doesn't modify any of the ‘bits’.
  • Violations: Just look for assignments to data members. Bitwise is C++’s definition of constness. A member function can’t modify any of the nonstatic members.
  • Issue: Functions that aren't const can sometimes pass the bitwise test.
Logical Constness
  • Logical: A const member function might modify some of the bits in it’s object, but only in way clients can’t detect.
  • Example: Caching a value is an interesting process. The cache may be read-only but it needs to be generated first. It would make sense for us to calculate the cache on first run of the function; but it will fail bitwise constant.
  • Mutable: Mutable allows us to evade the limitations of a const variable. Mutable variables can be changed in either const or non-const functions.
Duplication
  • Multiple Functions: The problem with const functions can be code duplication. You can end up duplicating identical code for both const and the non-const function.
  • Approach: We could instead, call the const version of the function, cast away the “constness” of the object and return it from the non-const version.
ConsteXPR
  • Compile Time: Even const variables aren't necessarily created at compile-time even though their value is known. Instead space may be allocated at run-time.
  • Constant Expression: We can actually declare a variable as a constant expression which is evaluated at compile-time. It can therefore be used as constants to determine array sizes etc.
After going over a few of the points in the lesson, it was time for a mini-practical session. Once again, we had to take what we had learnt from the current lesson. The guide from the practical was as followed...

  1. Create a class called GameObjects that stores a vector of GameObject.
  2. Create two functions (one const) that return the GameObject at pos [i] as a reference.
  3. Create a function in GameObject that takes another GameObject& as const.
  4. Return true if the object being tested has more health than the other.
  5. Create a function that takes two GameObjects& as const and returns the one with least health.
I found this particular task fairly tricky, due to my lack of knowledge in this area. I had only managed to complete task 1 and added the code snippets provided from the lesson slides. When the answer was revealed to us about 15 minutes later however, it made some sense to me as to where I went wrong.

Networking

Networking
  • Communications: Networking covers the concept of using multiple hosts/nodes to share data with each other. They can keep each other updated on any game logic changes.
  • Ease: Actually getting computers to talk to each other is a fairly easy task, but making sense of the bits sent/received isn’t.
Interwebs
  • Transport: The internet works by encoding packets of data and transporting them across the internet. A session can be thought of the mechanism for allowing the two to communicate.
  • Host Layers: Host layers are concerned with software, where the media layers are focused on the physical hardware responsible for transmitting data i.e. bits.
Protocols
  • Protocols: The internet sends data via protocols. Two of the most common in games is UDP and TCP/IP. They both allow data to be sent over the internet. But offer radically different approaches to how they accomplish this.
  • TCP (1): Transport Control Protocol; Creates a connection via two hosts. TCP is a guaranteed full duplex protocol. The connection is reliable and ordered.
  • UDP (1): User Datagram Protocol; Data is simply sent as packets. The receiver listens for these packets. Does not guarantee delivery of data.
  • TCP (2): Forms the backbone of the internet. It’s a reliable connection based protocols. I t forms a connection between two nodes.
  • Reliability: TCP provides reliable and guaranteed delivery of data.
  • Ordered: The data sent by TCP will be received in the same order.
  • Performance (1): All this extra error checking involves the receiver acknowledging receipt of complete and non-corrupt packets. i.e. ACK. Corrupt packets causes a queue to resolve the out of order issues.
  • IP: IP sits beneath the TCP stack. There is no concept of connections, packets are just passed from one computer to the next.
  • Duration: IP passes the data between nodes, but the route is unknown.
  • Recipent: There’s no guarantee that the packet is ever received by the intended host.
  • Routing: IP will do it’s best to get data from a to b via any possible route necessary. This means data can arrive out of order if the 2nd packet takes a different route to the 1st. It’s like a postal system.
  • UDP (2): Just like TCP it’s built on top of IP. However, it doesn’t provide layers of complexity and features. It’s a thin wrapper over the IP protocol.
  • Delivery: There’s no guarantee of delivery, nor attempt to confirm it.
  • Split: If the data get’s split into multiple packets, you won’t know.
  • Performance (2): Because it’s lightweight it’s quick but the lack of guaranteed delivery, or the ordering of packets make’s it an unreliable connection protocol.
TCP Vs UDP


  • TCP Innards: TCP is a stream protocol, you just write bytes to a stream formed by the connection. TCP breaks your stream of data into packets before sending them via the IP protocol.
  • Queues: This actually means there needs to be a queue for sending and buffer for receiving.
  • Delay: This process causes a delay, we can disable it, but TCP is still slow.
  • Guidance: Never use TCP for time critical applications. Real-time requirements dictate the usage of UDP. The truth is probably UDP and TCP connections should be used in tandem. Send real-time data via UDP, everything else by TCP.
IP Addresses
  • Hosts: Every networked PC has TCP/IP installed which provides an IP. The IP address uniquely identifies the PC’s ‘address’.
  • Classes: Some ranges of IP addresses are for private use only and the others for public i.e. internet.
  • Routing: A series of routers are responsible for forwarding packets to these IP addresses. Consider your IP address as your mailbox, with IP responsible for delivering items.
  • TCP Innards: The IPv4 address is a 4-byte number. IP addresses are normally in decimal format. It’s formed of two parts. The network ID and host ID.
  • LAN: Local area networks sit behind a router, that serves data from and to the internet.
DNS
  • Domain Name System: Clearly, even though every host has an IP address, we don’t browse the web using those IP’s. Instead we resolve an alias that points to that IP.
  • Resolve: The DNS servers around the world are responsible for converting a human readable name to an IP address.
  • Abstraction: It’s clear that this layer of abstraction is of benefit. Imagine trying to remember the IP addresses of all your favourite sites?
Once the theoretical side of the lesson ended, we moved onto the main practical of the lesson. Our task was to create our own Chat Server using Visual Studio 2013. Although my set-up was when following the instructions was fine, it came up with compiler errors nevertheless. There were also sections in the code where we would need our own input to make it work. Sadly, before I started to try and fix the errors, I ran out of time in the lesson.

No comments:

Post a Comment