CPN
Computational Process Networks
PacketEncoder.h
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 #ifndef CPN_PACKETENCODER_H
24 #define CPN_PAcKETENCODER_H
25 #pragma once
26 
27 #include "common_priv.h"
28 #include "PacketHeader.h"
30 #include <vector>
31 // For the iovec definition
32 #include <sys/uio.h>
33 
34 namespace CPN {
39  public:
40  PacketEncoder();
41  virtual ~PacketEncoder();
42 
43  template<typename Queue_t>
44  void SendEnqueue(const Packet &packet, Queue_t &queue) {
45  ASSERT(packet.Type() == PACKET_ENQUEUE);
46  std::vector<iovec> iovs;
47  // Must use const_cast here because iovec.iov_base isn't const... :(
48  iovec header = { const_cast<PacketHeader*>(&packet.header), sizeof(packet.header) };
49  iovs.push_back(header);
50  for (unsigned i = 0; i < queue.NumChannels(); ++i) {
51  iovec iov;
52  iov.iov_base = const_cast<void*>(queue.GetRawDequeuePtr(packet.Count(), i));
53  ASSERT(iov.iov_base);
54  iov.iov_len = packet.Count();
55  iovs.push_back(iov);
56  }
57  WriteBytes(&iovs[0], iovs.size());
58  queue.Dequeue(packet.Count());
59  }
60 
61  void SendPacket(const Packet &packet);
62  void SendPacket(const Packet &packet, void *data);
63  protected:
64  virtual void WriteBytes(const iovec *iov, unsigned iovcnt) = 0;
65  private:
66  };
67 
68 }
69 #endif
void SendEnqueue(const Packet &packet, Queue_t &queue)
Definition: PacketEncoder.h:44
PacketHeader header
Definition: PacketHeader.h:148
uint32_t Count() const
Definition: PacketHeader.h:131
#define CPN_LOCAL
Definition: common.h:37
PacketType_t Type() const
Definition: PacketHeader.h:125
Declarations of a generic binary packet format.
#define ASSERT(exp,...)