Advertisement
Guest User

datagram.cpp

a guest
Aug 22nd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <netinet/in.h>
  5. #include <endian.h>
  6. #include <iostream>
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11. const int DATAGRAM_SIZE = 512;
  12. const int MIN_CLIENT_DATAGRAM_SIZE = 8 + 1 + 4 + 1;//uint64 + int8 + uint32 + player_name
  13.  
  14. const int SESSION_ID_POS = 0;
  15. const int TURN_DIRECTION_POS = 8;
  16. const int NEXT_EXP_EVENT_NUMBER_POS = 9;
  17. const int PLAYER_NAME_POS = 13;
  18.  
  19. const int MAX_PLAYER_NAME_LEN = 64;
  20. const int MIN_VALID_PLAYER_NAME_SYMBOL = 33;
  21. const int MAX_VALID_PLAYER_NAME_SYMBOL = 126;
  22.  
  23. class Datagram {
  24.  
  25.  
  26.  
  27.  
  28. public:
  29.     char datagram_buffer[512+1];// \0
  30.     int next_free_byte;
  31.     int recv_length;
  32.  
  33. public:
  34.     Datagram();
  35.     void clear_datagram();
  36.  
  37.     void pack_next_int8_bit_value_buffer(int8_t v);
  38.  
  39.     void pack_next_uint32_bit_value_buffer(uint32_t v);
  40.  
  41.     void pack_next_uint64_bit_value_buffer(uint64_t v);
  42.  
  43.     void pack_next_string_buffer(string s);
  44.  
  45.     void get_int8_bit_value_fbuffer(int8_t& v, int start_in_datagram);
  46.  
  47.     void get_int32_bit_value_fbuffer(uint32_t& v, int start_in_datagram);
  48.  
  49.     void get_int64_bit_value_fbuffer(uint64_t& v, int start_in_datagram);
  50.  
  51.     void get_string_fbuffer(string& s, int start_in_datagram, int end_in_datagram);
  52.  
  53. };
  54.  
  55. void Datagram::clear_datagram() {
  56.     for(int i = 0; i < DATAGRAM_SIZE; i++) {
  57.         datagram_buffer[i] = 7;
  58.     }
  59.     next_free_byte = 0;
  60.     recv_length = 0;
  61. }
  62.  
  63. Datagram::Datagram() {
  64.     clear_datagram();
  65.     //datagram_buffer[DATAGRAM_SIZE] = '\0';
  66. }
  67.  
  68.  
  69.  
  70. void Datagram::pack_next_int8_bit_value_buffer(int8_t v) {
  71.     datagram_buffer[next_free_byte] = ((uint8_t*)&v)[0];
  72.     next_free_byte++;
  73. }
  74.  
  75. void Datagram::pack_next_uint32_bit_value_buffer(uint32_t v) {
  76.     uint32_t net_order = htobe32(v);
  77.     for (int i=0; i<4 ;++i) {
  78.         datagram_buffer[next_free_byte] = ((uint8_t*)&net_order)[i];
  79.         next_free_byte++;
  80.     }
  81. }
  82.  
  83. void Datagram::pack_next_uint64_bit_value_buffer(uint64_t v) {
  84.     uint64_t net_order = htobe64(v);
  85.     for (int i=0; i<8 ;++i) {
  86.         datagram_buffer[next_free_byte] = ((uint8_t*)&net_order)[i];
  87.         next_free_byte++;
  88.     }
  89. }
  90.  
  91.  
  92. void Datagram::pack_next_string_buffer(string s) {
  93.     //mempcpy(datagram_buffer+next_free_byte, s.c_str(), s.length());
  94.     cout<<s<<endl;
  95.     for(int i = 0; i < s.length(); i++) {
  96.         datagram_buffer[next_free_byte] = (char)s[i];
  97.         next_free_byte++;
  98.     }
  99.     cout<<"next "<<next_free_byte<<endl;
  100. }
  101.  
  102.  
  103.  
  104. void Datagram::get_int8_bit_value_fbuffer(int8_t& v, int start_in_datagram) {
  105.     uint8_t a[1];
  106.     mempcpy(a, datagram_buffer + start_in_datagram, sizeof(uint8_t));
  107.     v = *((uint8_t*) a);
  108. }
  109.  
  110. void Datagram::get_int32_bit_value_fbuffer(uint32_t& v, int start_in_datagram) {
  111.     uint32_t a[4];
  112.     mempcpy(a, datagram_buffer + start_in_datagram, sizeof(uint32_t));
  113.     v = *((uint32_t*) a);
  114.     v = be32toh(v);
  115. }
  116.  
  117. void Datagram::get_int64_bit_value_fbuffer(uint64_t& v, int start_in_datagram) {
  118.     uint64_t a[8];
  119.     mempcpy(a, datagram_buffer + start_in_datagram, sizeof(uint64_t));
  120.     v = *((uint64_t*) a);
  121.     v = be64toh(v);
  122. }
  123.  
  124. void Datagram::get_string_fbuffer(string& s, int start_in_datagram, int end_in_datagram) {
  125.     size_t s_len = size_t (end_in_datagram - start_in_datagram + 1);
  126.     char recv[s_len];
  127.     mempcpy(recv, datagram_buffer + start_in_datagram, s_len);
  128.     s = string(recv);
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement