CPN
Computational Process Networks
PacketDecoder.cc
Go to the documentation of this file.
1 //=============================================================================
2 // Computational Process Networks class library
3 // Copyright (C) 1997-2006 Gregory E. Allen and The University of Texas
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // The GNU Public License is available in the file LICENSE, or you
16 // can write to the Free Software Foundation, Inc., 59 Temple Place -
17 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
18 // World Wide Web at http://www.fsf.org.
19 //=============================================================================
23 #include "common_priv.h"
24 #include "PacketDecoder.h"
26 #include <string.h>
27 
28 namespace CPN {
29 
31  : numbytes(0)
32  {
33  }
34 
36 
37  void *PacketDecoder::GetDecoderBytes(unsigned &amount) {
38  char *ptr = reinterpret_cast<char*>(&header.header);
39  amount = sizeof(header.header) - numbytes;
40  ASSERT(amount != 0, "Zero decoder bytes");
41  return ptr + numbytes;
42  }
43 
44  void PacketDecoder::ReleaseDecoderBytes(unsigned amount) {
45  ASSERT(numbytes + amount <= sizeof(header.header));
46  numbytes += amount;
47  if (numbytes == sizeof(header.header)) {
48  if (header.Valid()) {
50  numbytes = 0;
51  } else {
52  // If it is not valid, what do we do??
53  // Well, we can search for the header word
54  // wont always work...
55  char *ptr = reinterpret_cast<char*>(&header.header);
56  numbytes -= 1;
57  memmove(ptr, ptr + 1, numbytes);
58  }
59  }
60  }
61 
63  numbytes = 0;
64  }
65 }
66 
PacketHeader header
Definition: PacketHeader.h:148
void ReleaseDecoderBytes(unsigned amount)
virtual ~PacketDecoder()
bool Valid() const
Definition: PacketHeader.h:134
void FirePacket(const Packet &packet)
Definition: PacketHeader.cc:31
#define ASSERT(exp,...)
void * GetDecoderBytes(unsigned &amount)