embb_mtapi_network_buffer.c 2.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <embb_mtapi_network_buffer.h>
#include <embb/base/c/memory_allocation.h>
#include <string.h>

void embb_mtapi_network_buffer_initialize(
  embb_mtapi_network_buffer_t * that,
  int capacity) {
  that->position = 0;
  that->size = 0;
  that->data = embb_alloc(capacity);
  if (NULL != that->data) {
    that->capacity = capacity;
  } else {
    that->capacity = 0;
  }
}

void embb_mtapi_network_buffer_finalize(
  embb_mtapi_network_buffer_t * that) {
  that->position = 0;
  that->size = 0;
  that->capacity = 0;
  if (NULL != that->data) {
    embb_free(that->data);
    that->data = NULL;
  }
}

int embb_mtapi_network_buffer_push_back_int8(
  embb_mtapi_network_buffer_t * that,
  int8_t value) {
  if (that->size + 1 > that->capacity) {
    return 0;
  }
  memcpy(that->data + that->size, &value, 1);
  that->size += 1;
  return 1;
}

int embb_mtapi_network_buffer_push_back_int16(
  embb_mtapi_network_buffer_t * that,
  int16_t value) {
  if (that->size + 2 > that->capacity) {
    return 0;
  }
  memcpy(that->data + that->size, &value, 2);
  that->size += 2;
48
  return 2;
49 50 51 52 53 54 55 56 57 58
}

int embb_mtapi_network_buffer_push_back_int32(
  embb_mtapi_network_buffer_t * that,
  int32_t value) {
  if (that->size + 4 > that->capacity) {
    return 0;
  }
  memcpy(that->data + that->size, &value, 4);
  that->size += 4;
59
  return 4;
60 61 62 63 64 65 66 67 68 69 70
}

int embb_mtapi_network_buffer_push_back_rawdata(
  embb_mtapi_network_buffer_t * that,
  int32_t size,
  void * rawdata) {
  if (that->size + size > that->capacity) {
    return 0;
  }
  memcpy(that->data + that->size, rawdata, size);
  that->size += size;
71
  return size;
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
}

int embb_mtapi_network_buffer_pop_front_int8(
  embb_mtapi_network_buffer_t * that,
  int8_t * value) {
  if (that->position + 1 > that->size) {
    return 0;
  }
  memcpy(value, that->data + that->position, 1);
  that->position += 1;
  return 1;
}

int embb_mtapi_network_buffer_pop_front_int16(
  embb_mtapi_network_buffer_t * that,
  int16_t * value) {
  if (that->position + 2 > that->size) {
    return 0;
  }
  memcpy(value, that->data + that->position, 2);
  that->position += 2;
  return 2;
}

int embb_mtapi_network_buffer_pop_front_int32(
  embb_mtapi_network_buffer_t * that,
  int32_t * value) {
  if (that->position + 4 > that->size) {
    return 0;
  }
  memcpy(value, that->data + that->position, 4);
  that->position += 4;
  return 4;
}

int embb_mtapi_network_buffer_pop_front_rawdata(
  embb_mtapi_network_buffer_t * that,
  int32_t size,
  void * rawdata) {
  if (that->position + size > that->size) {
    return 0;
  }
  memcpy(rawdata, that->data + that->position, size);
  that->position += size;
  return size;
}