source: abuse/trunk/src/imlib/packet.cpp @ 128

Last change on this file since 128 was 124, checked in by Sam Hocevar, 15 years ago
  • Get rid of ugly tabs and trailing spaces everywhere.
File size: 1.8 KB
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *
5 *  This software was released into the Public Domain. As with most public
6 *  domain software, no warranty is made or implied by Crack dot Com or
7 *  Jonathan Clark.
8 */
9
10#include "config.h"
11
[2]12#include <stdlib.h>
13#include <string.h>
14
[56]15#include "packet.hpp"
16#include "jmalloc.hpp"
[2]17
[17]18int packet::advance(int32_t offset)
[2]19{
20  ro+=offset;
21  return ro<=rend;
22}
23
[17]24void packet::write_uint32(uint32_t x)
[2]25{
26  x=lltl(x);
[17]27  write((uint8_t *)&x,4);
[2]28}
29
[17]30void packet::write_uint16(uint16_t x)
[2]31{
32  x=lstl(x);
[17]33  write((uint8_t *)&x,2);
[2]34}
35
[17]36void packet::write_uint8(uint8_t x)
[2]37{
38  write(&x,1);
39}
40
41packet::~packet()
42{ jfree(buf); }
43
[124]44packet::packet(int prefix_size)
45{
[2]46  pre_size=prefix_size;
47
48#ifdef MANAGE_MEM
49  int sp=alloc_space;
50  alloc_space=ALLOC_SPACE_STATIC;
51#endif
52
53  buf_size=1000;
[17]54  buf=(uint8_t *)jmalloc(buf_size,"packet buffer");
[124]55  reset();
[2]56
57#ifdef MANAGE_MEM
58  alloc_space=sp;
59#endif
60}
61
62void packet::get_string(char *st, int len)
63{
64  char *b=(char *)buf+ro;
65  while (len>1 && !eop() && *b)
66  {
[124]67    *st=*b;
[2]68    st++;
69    ro++;
70    b++;
71    len--;
72  }
73  if (*b==0) ro++;
74  *st=0;
75}
76
[124]77void packet::reset()
[2]78{ ro=wo=rend=pre_size; }
79
80void packet::make_bigger(int max)
81{
82  if (buf_size<max)
83  {
84    buf_size=max;
[124]85    buf=(uint8_t *)jrealloc(buf,max,"packet buffer");
[2]86  }
87}
88
[17]89int packet::read(uint8_t *buffer, int size)
[2]90{
91  if (size>rend-ro)
92    size=rend-ro;
93
94  if (size>0)
95  {
96    memcpy(buffer,buf+ro,size);
97    ro+=size;
98    return size;
99  } else return 0;
100}
101
102
[17]103int packet::write(uint8_t *buffer, int size)
[2]104{
105  if (size>buf_size-wo)
106    make_bigger(wo+size);
107  if (size>0)
108  {
109    memcpy(buf+wo,buffer,size);
110    wo+=size;
111    rend=wo;
112    return size;
113  }
114  return 0;
115}
116
117
118
119void packet::insert_into(packet &pk)
120{
121  pk.write(buf+pre_size,rend-pre_size);
122}
123
124
125
Note: See TracBrowser for help on using the repository browser.