ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Packet.h
Go to the documentation of this file.
1 /*
2  * Original work Copyright 2009 - 2010 Kevin Ackley (kackley@gwi.net)
3  * Modified work Copyright 2018 - 2020 Andy Maloney <asmaloney@gmail.com>
4  *
5  * Permission is hereby granted, free of charge, to any person or organization
6  * obtaining a copy of the software and accompanying documentation covered by
7  * this license (the "Software") to use, reproduce, display, distribute,
8  * execute, and transmit the Software, and to prepare derivative works of the
9  * Software, and to permit third-parties to whom the Software is furnished to
10  * do so, all subject to the following:
11  *
12  * The copyright notices in the Software and this entire statement, including
13  * the above license grant, this restriction and the following disclaimer,
14  * must be included in all copies of the Software, in whole or in part, and
15  * all derivative works of the Software, unless such copies or derivative
16  * works are solely in the form of machine-executable object code generated by
17  * a source language processor.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  * DEALINGS IN THE SOFTWARE.
26  */
27 
28 #pragma once
29 
30 #include <cstdint>
31 #include <vector>
32 
33 #include "Common.h"
34 
35 namespace e57
36 {
37  class CheckedFile;
38  class PacketLock;
39 
41  enum
42  {
46  };
47 
49  constexpr int DATA_PACKET_MAX = ( 64 * 1024 );
50 
52  {
53  public:
54  PacketReadCache( CheckedFile *cFile, unsigned packetCount );
55 
56  std::unique_ptr<PacketLock> lock( uint64_t packetLogicalOffset,
57  char *&pkt ); //??? pkt could be const
58 
59 #ifdef E57_DEBUG
60  void dump( int indent = 0, std::ostream &os = std::cout );
61 #endif
62 
63  protected:
65  friend class PacketLock;
66  void unlock( unsigned cacheIndex );
67 
68  void readPacket( unsigned oldestEntry, uint64_t packetLogicalOffset );
69 
70  struct CacheEntry
71  {
72  uint64_t logicalOffset_ = 0;
74  unsigned lastUsed_ = 0;
75  };
76 
77  unsigned lockCount_ = 0;
78  unsigned useCount_ = 0;
79  CheckedFile *cFile_ = nullptr;
80 
81  std::vector<CacheEntry> entries_;
82  };
83 
84  class PacketLock
85  {
86  public:
87  ~PacketLock();
88 
89  private:
91  PacketLock( const PacketLock &plock );
92  PacketLock &operator=( const PacketLock &plock );
93 
94  protected:
95  friend class PacketReadCache;
97  PacketLock( PacketReadCache *cache, unsigned cacheIndex );
98 
99  PacketReadCache *cache_ = nullptr;
100  unsigned int cacheIndex_ = 0;
101  };
102 
104  {
105  public:
107 
108  void reset();
109 
110  void verify( unsigned bufferLength = 0 ) const; //???use
111 
112 #ifdef E57_DEBUG
113  void dump( int indent = 0, std::ostream &os = std::cout ) const;
114 #endif
115  const uint8_t packetType = DATA_PACKET;
116 
117  uint8_t packetFlags = 0;
119  uint16_t bytestreamCount = 0;
120  };
121 
123  {
124  public:
125  DataPacket();
126 
127  void verify( unsigned bufferLength = 0 ) const;
128  char *getBytestream( unsigned bytestreamNumber, unsigned &byteCount );
129  unsigned getBytestreamBufferLength( unsigned bytestreamNumber );
130 
131 #ifdef E57_DEBUG
132  void dump( int indent = 0, std::ostream &os = std::cout ) const;
133 #endif
134 
135  static constexpr int PayloadSize = DATA_PACKET_MAX - sizeof( DataPacketHeader );
136 
138 
139  uint8_t payload[PayloadSize];
140  };
141 }
void verify(unsigned bufferLength=0) const
Definition: Packet.cpp:344
void dump(int indent=0, std::ostream &os=std::cout) const
Definition: Packet.cpp:390
uint16_t bytestreamCount
Definition: Packet.h:119
const uint8_t packetType
Definition: Packet.h:115
uint16_t packetLogicalLengthMinus1
Definition: Packet.h:118
uint8_t packetFlags
Definition: Packet.h:117
DataPacketHeader header
Definition: Packet.h:137
void verify(unsigned bufferLength=0) const
Definition: Packet.cpp:409
void dump(int indent=0, std::ostream &os=std::cout) const
Definition: Packet.cpp:508
static constexpr int PayloadSize
Definition: Packet.h:135
char * getBytestream(unsigned bytestreamNumber, unsigned &byteCount)
Definition: Packet.cpp:453
uint8_t payload[PayloadSize]
Definition: Packet.h:139
unsigned getBytestreamBufferLength(unsigned bytestreamNumber)
Definition: Packet.cpp:499
unsigned int cacheIndex_
Definition: Packet.h:100
PacketReadCache * cache_
Definition: Packet.h:99
std::vector< CacheEntry > entries_
Definition: Packet.h:81
unsigned lockCount_
Definition: Packet.h:77
PacketReadCache(CheckedFile *cFile, unsigned packetCount)
Definition: Packet.cpp:77
CheckedFile * cFile_
Definition: Packet.h:79
void unlock(unsigned cacheIndex)
Definition: Packet.cpp:164
void readPacket(unsigned oldestEntry, uint64_t packetLogicalOffset)
Definition: Packet.cpp:179
unsigned useCount_
Definition: Packet.h:78
void dump(int indent=0, std::ostream &os=std::cout)
Definition: Packet.cpp:257
std::unique_ptr< PacketLock > lock(uint64_t packetLogicalOffset, char *&pkt)
Definition: Packet.cpp:85
@ DATA_PACKET
Definition: Packet.h:44
@ EMPTY_PACKET
Definition: Packet.h:45
@ INDEX_PACKET
Definition: Packet.h:43
constexpr int DATA_PACKET_MAX
maximum size of CompressedVector binary data packet
Definition: Packet.h:49
unsigned lastUsed_
No need to init since it's a data buffer.
Definition: Packet.h:74
char buffer_[DATA_PACKET_MAX]
Definition: Packet.h:73