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

Last change on this file since 56 was 56, checked in by Sam Hocevar, 15 years ago
  • Add licensing terms to most C / C++ files (Ref #5).
File size: 1.8 KB
Line 
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
12#include <stdlib.h>
13#include <string.h>
14
15#include "packet.hpp"
16#include "jmalloc.hpp"
17
18int packet::advance(int32_t offset)
19{
20  ro+=offset;
21  return ro<=rend;
22}
23
24void packet::write_uint32(uint32_t x)
25{
26  x=lltl(x);
27  write((uint8_t *)&x,4);
28}
29
30void packet::write_uint16(uint16_t x)
31{
32  x=lstl(x);
33  write((uint8_t *)&x,2);
34}
35
36void packet::write_uint8(uint8_t x)
37{
38  write(&x,1);
39}
40
41packet::~packet()
42{ jfree(buf); }
43
44packet::packet(int prefix_size)
45{
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;
54  buf=(uint8_t *)jmalloc(buf_size,"packet buffer");
55  reset();
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  {
67    *st=*b;
68    st++;
69    ro++;
70    b++;
71    len--;
72  }
73  if (*b==0) ro++;
74  *st=0;
75}
76
77void packet::reset()
78{ ro=wo=rend=pre_size; }
79
80void packet::make_bigger(int max)
81{
82  if (buf_size<max)
83  {
84    buf_size=max;
85    buf=(uint8_t *)jrealloc(buf,max,"packet buffer");
86  }
87}
88
89int packet::read(uint8_t *buffer, int size)
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
103int packet::write(uint8_t *buffer, int size)
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.