File: message.cc

package info (click to toggle)
sinfo 0.0.48-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,332 kB
  • sloc: sh: 11,213; cpp: 6,722; makefile: 271; xml: 151; perl: 149
file content (204 lines) | stat: -rw-r--r-- 4,665 bytes parent folder | download | duplicates (3)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <string.h>
#include "message.h"


Message::Message(unsigned long length, const char * data)
{
  memorySize=length;
  memory = boost::shared_array<char>(new char[memorySize]);

  if (data)
  {
    startIndex=0;
    endIndex=memorySize;
    memcpy(memory.get(),data,memorySize);
  }
  else
  {
    startIndex=memorySize;
    endIndex=memorySize;
  }
  dontSendFlag=false;
}


Message::Message(const Message& message)
{
  memorySize=message.size();
  memory = boost::shared_array<char>(new char[memorySize]);
  startIndex=0;
  endIndex=memorySize;
  memcpy(memory.get(),message.getDataPtr(),memorySize);
  dontSendFlag=message.dontSendFlag;
}


Message::~Message()
{
}


void Message::setMessage(unsigned long length, const char * data)
{
  memorySize=length;
  memory = boost::shared_array<char>(new char[memorySize]);
  startIndex=0;
  endIndex=length;
  memcpy(memory.get(),data,length);
}


Message& Message::operator=(const Message & message)
{
  // check for self-assignment by comparing the address of the implicit object and the parameter
  if (this == &message)
  {
    return *this;
  }
  setMessage(message.size(),message.getDataPtr());
  dontSendFlag=message.dontSendFlag;
  return *this;
}


const char * Message::getDataPtr() const
{
  return &memory[startIndex];
}


unsigned long Message::size() const
{
  return endIndex-startIndex;
}


void Message::clear()
{
  memorySize=STANDARDBUFFERSIZE;
  memory = boost::shared_array<char>(new char[memorySize]);
  startIndex=memorySize;
  endIndex=memorySize;
  dontSendFlag=false;
}


const char & Message::operator [] (unsigned long idx)
{
  unsigned long localidx=startIndex+idx;
  if (localidx>=memorySize)
  {
    throw MessageException("Message::operator[] failed, localidx>=memorySize");
  }
  return memory[localidx];
}

void Message::pushFrontMemory(const void *src, size_t n)
{
  // increase memory by STANDARDBUFFERINCREMENT or n
  if (startIndex<n)
  {
    long increment=STANDARDBUFFERINCREMENT;
    if (startIndex+STANDARDBUFFERINCREMENT<n)
    {
      increment=n;
    }
    long newMemorySize=memorySize+increment;
    boost::shared_array<char>  newMemory(new char[newMemorySize]);
    long newStartIndex=startIndex+increment;
    long newEndIndex=endIndex+increment;
    memcpy(&newMemory[newStartIndex], &memory[startIndex], endIndex-startIndex);

    memorySize=newMemorySize;
    memory=newMemory;
    startIndex=newStartIndex;
    endIndex=newEndIndex;
  }

  startIndex-=n;

  // without litte=>big endian conversion
  // memcpy(&memory[startIndex],src,n);

  // with little =>big endian conversion
  char * srcchar=(char*)src;
  for (unsigned long k=0; k<n; k++)
  {
    memory[startIndex+(n-1)-k]=srcchar[k];
  }
}


void Message::pushBackMemory(const void *src, size_t n)
{
  // increase memory by STANDARDBUFFERINCREMENT or n
  if (endIndex+n>memorySize)
  {
    long increment=STANDARDBUFFERINCREMENT;
    if (endIndex+n>memorySize+STANDARDBUFFERINCREMENT)
    {
      increment=n;
    }
    unsigned long newMemorySize=memorySize+increment;
    boost::shared_array<char>  newMemory(new char[newMemorySize]);
    unsigned long newStartIndex=startIndex;
    unsigned long newEndIndex=endIndex;
    memcpy(&newMemory[newStartIndex], &memory[startIndex], endIndex-startIndex);

    memorySize=newMemorySize;
    memory=newMemory;
    startIndex=newStartIndex;
    endIndex=newEndIndex;
  }

  // without litte=>big endian conversion
  // memcpy(&newMemory[endIndex],src,n);

  // with little =>big endian conversion
  char * srccrar=(char*)src;
  for (unsigned long k=0; k<n; k++)
  {
    memory[endIndex+(n-1)-k]=srccrar[k];
  }

  endIndex+=n;
}


void Message::popFrontMemory(void *dest, size_t n)
{
  if ( n<=endIndex-startIndex )
  {
    // without big=>litte endian conversion
    // memcpy(dest,&memory[startIndex],n);

    // with little =>big endian conversion
    char * destchar=(char*)dest;
    for (unsigned long k=0; k<n; k++)
    {
      destchar[k]=memory[startIndex+(n-1)-k];
    }

    startIndex+=n;


    // shrink memory
    if (startIndex>=STANDARDBUFFERINCREMENT)
    {
      unsigned long newMemorySize=memorySize-STANDARDBUFFERINCREMENT;
      boost::shared_array<char>  newMemory(new char[newMemorySize]);
      unsigned long newStartIndex=startIndex-STANDARDBUFFERINCREMENT;
      unsigned long newEndIndex=endIndex-STANDARDBUFFERINCREMENT;
      memcpy(&newMemory[newStartIndex], &memory[startIndex], endIndex-startIndex);

      memorySize=newMemorySize;
      memory=newMemory;
      startIndex=newStartIndex;
      endIndex=newEndIndex;
    }
  }
  else
  {
    throw MessageException("Message::popFrontMemory failed, no data available");
  }
}