Protocol.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Deserializer.h"
  25. #include "Serializer.h"
  26. // Channel numbers
  27. static const unsigned char CHN_RELIABLE = 0;
  28. static const unsigned char CHN_UNRELIABLE = 1;
  29. // File transfer fragment size
  30. static const unsigned FILE_FRAGMENT_SIZE = 1024;
  31. // Reliable messages (chn.0), server to client
  32. static const unsigned char MSG_CHALLENGE = 0x00;
  33. static const unsigned char MSG_SCENEINFO = 0x01;
  34. static const unsigned char MSG_TRANSFERDATA = 0x02;
  35. static const unsigned char MSG_TRANSFERFAILED = 0x03;
  36. static const unsigned char MSG_JOINREPLY = 0x04;
  37. static const unsigned char MSG_FULLUPDATE = 0x05;
  38. // Reliable messages (chn.0), client to server
  39. static const unsigned char MSG_LOGIN = 0x06;
  40. static const unsigned char MSG_REQUESTFILE = 0x07;
  41. static const unsigned char MSG_JOINSCENE = 0x08;
  42. static const unsigned char MSG_FULLUPDATEACK = 0x09;
  43. // Server/clientupdate sub-messages. Unreliable chn.1 messages consist entirely of these
  44. static const unsigned char MSG_CREATENODE = 0x0a;
  45. static const unsigned char MSG_UPDATENODE = 0x0b;
  46. static const unsigned char MSG_REMOVENODE = 0x0c;
  47. static const unsigned char MSG_CREATECOMPONENT = 0x0d;
  48. static const unsigned char MSG_UPDATECOMPONENT = 0x0e;
  49. static const unsigned char MSG_REMOVECOMPONENT = 0x0f;
  50. static const unsigned char MSG_CONTROLS = 0x10;
  51. static const unsigned char MSG_REMOTEEVENT = 0x11;
  52. static const unsigned char MSG_REMOTENODEEVENT = 0x12;
  53. /// Write a 24-bit networked Node or Component ID
  54. inline void WriteNetID(Serializer& dest, unsigned id)
  55. {
  56. // Write the low 24 bits
  57. dest.Write(&id, 3);
  58. }
  59. // Read a 24-bit networked Node or Component ID
  60. inline unsigned ReadNetID(Deserializer& source)
  61. {
  62. unsigned ret = 0;
  63. // Read the low 24 bits
  64. source.Read(&ret, 3);
  65. return ret;
  66. }
  67. /// Compare network frame numbers, taking wrapping into account
  68. inline bool CheckFrameNumber(unsigned short lhs, unsigned short rhs, bool sameFrameOk = true)
  69. {
  70. // Frame number 0 means "frame never received", so in that case lhs is always considered "newer"
  71. if ((lhs) && (!rhs))
  72. return true;
  73. if ((!sameFrameOk) && (lhs == rhs))
  74. return false;
  75. return ((lhs - rhs) & 0xffff) < 0x8000;
  76. }