Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <netinet/in.h>
- #include <endian.h>
- #include <iostream>
- #include <string>
- using namespace std;
- const int DATAGRAM_SIZE = 512;
- const int MIN_CLIENT_DATAGRAM_SIZE = 8 + 1 + 4 + 1;//uint64 + int8 + uint32 + player_name
- const int SESSION_ID_POS = 0;
- const int TURN_DIRECTION_POS = 8;
- const int NEXT_EXP_EVENT_NUMBER_POS = 9;
- const int PLAYER_NAME_POS = 13;
- const int MAX_PLAYER_NAME_LEN = 64;
- const int MIN_VALID_PLAYER_NAME_SYMBOL = 33;
- const int MAX_VALID_PLAYER_NAME_SYMBOL = 126;
- class Datagram {
- public:
- char datagram_buffer[512+1];// \0
- int next_free_byte;
- int recv_length;
- public:
- Datagram();
- void clear_datagram();
- void pack_next_int8_bit_value_buffer(int8_t v);
- void pack_next_uint32_bit_value_buffer(uint32_t v);
- void pack_next_uint64_bit_value_buffer(uint64_t v);
- void pack_next_string_buffer(string s);
- void get_int8_bit_value_fbuffer(int8_t& v, int start_in_datagram);
- void get_int32_bit_value_fbuffer(uint32_t& v, int start_in_datagram);
- void get_int64_bit_value_fbuffer(uint64_t& v, int start_in_datagram);
- void get_string_fbuffer(string& s, int start_in_datagram, int end_in_datagram);
- };
- void Datagram::clear_datagram() {
- for(int i = 0; i < DATAGRAM_SIZE; i++) {
- datagram_buffer[i] = 7;
- }
- next_free_byte = 0;
- recv_length = 0;
- }
- Datagram::Datagram() {
- clear_datagram();
- //datagram_buffer[DATAGRAM_SIZE] = '\0';
- }
- void Datagram::pack_next_int8_bit_value_buffer(int8_t v) {
- datagram_buffer[next_free_byte] = ((uint8_t*)&v)[0];
- next_free_byte++;
- }
- void Datagram::pack_next_uint32_bit_value_buffer(uint32_t v) {
- uint32_t net_order = htobe32(v);
- for (int i=0; i<4 ;++i) {
- datagram_buffer[next_free_byte] = ((uint8_t*)&net_order)[i];
- next_free_byte++;
- }
- }
- void Datagram::pack_next_uint64_bit_value_buffer(uint64_t v) {
- uint64_t net_order = htobe64(v);
- for (int i=0; i<8 ;++i) {
- datagram_buffer[next_free_byte] = ((uint8_t*)&net_order)[i];
- next_free_byte++;
- }
- }
- void Datagram::pack_next_string_buffer(string s) {
- //mempcpy(datagram_buffer+next_free_byte, s.c_str(), s.length());
- cout<<s<<endl;
- for(int i = 0; i < s.length(); i++) {
- datagram_buffer[next_free_byte] = (char)s[i];
- next_free_byte++;
- }
- cout<<"next "<<next_free_byte<<endl;
- }
- void Datagram::get_int8_bit_value_fbuffer(int8_t& v, int start_in_datagram) {
- uint8_t a[1];
- mempcpy(a, datagram_buffer + start_in_datagram, sizeof(uint8_t));
- v = *((uint8_t*) a);
- }
- void Datagram::get_int32_bit_value_fbuffer(uint32_t& v, int start_in_datagram) {
- uint32_t a[4];
- mempcpy(a, datagram_buffer + start_in_datagram, sizeof(uint32_t));
- v = *((uint32_t*) a);
- v = be32toh(v);
- }
- void Datagram::get_int64_bit_value_fbuffer(uint64_t& v, int start_in_datagram) {
- uint64_t a[8];
- mempcpy(a, datagram_buffer + start_in_datagram, sizeof(uint64_t));
- v = *((uint64_t*) a);
- v = be64toh(v);
- }
- void Datagram::get_string_fbuffer(string& s, int start_in_datagram, int end_in_datagram) {
- size_t s_len = size_t (end_in_datagram - start_in_datagram + 1);
- char recv[s_len];
- mempcpy(recv, datagram_buffer + start_in_datagram, s_len);
- s = string(recv);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement