Advertisement
zerophase

PackageManager

Nov 10th, 2015
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. 3) We have recently discovered that our Android app is overloading our server, now that our user base has increased.  To alleviate this problem, we need to throttle back on the data we are sending to the server so that we send no more than 1024 bytes at a time, and no more often than 30 times per second.  Our packets are guaranteed to be no larger than 800 bytes each, and must be sent in their entirety. You may assume that no packet is of higher priority than any other packet. Each packet will need a header in network byte order that contains the string “PKT_”, followed by a 4-byte unsigned integer that gives the packet size, not including the header (similar to IFF/PNG/WAV formats).  Packets can be concatenated together for a single network send via TransmitData().  Any packets that cannot be sent must be queued up until the next opportunity to send them.
  2.  
  3. Implement PacketManager with the packet throttling described above to handle this situation.  The Send () method is called by the program when it has data to be sent to the server.  The system will call the PacketManager’s Update() method on a semi-regular basis, and pass it a floating point parameter that specifies the amount of time in seconds that has elapsed since the last time we called it.  A NetworkConnection class exists that implements the method to send data to the server.  You can assume the connection to the server and any user authentication has already been handled.
  4.  
  5. In C/C++ your prototypes would be:
  6.  
  7.  
  8. // This class is already implemented, and may be called by your code.
  9. class NetworkConnection
  10. {
  11. public:
  12.  static void TransmitData (unsigned char *  data, int dataSize);
  13. }
  14.  
  15. // You can start with this class and expand it as needed.
  16. class PacketManager
  17. {
  18. public:
  19.   void Update (float secondsSinceLastUpdate);
  20.   void Send (unsigned char * packetData, int packetSize);
  21. }
  22.  
  23.  
  24. In C# the prototypes would be:
  25.  
  26.  
  27. public class NetworkConnection
  28. {
  29.   public static void TransmitData (byte [] data, int dataSize)
  30.   {
  31.     // assume this is already implemented
  32.   }
  33. }
  34.  
  35.  
  36. public class PacketManager
  37. {
  38.   public void Update (float secondsSinceLastUpdate)
  39.   {
  40.   }
  41.   public void Send (byte [] packetData, int packetSize)
  42.   {
  43.   }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement