neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
tld_packet.hpp
Go to the documentation of this file.
1// tld_packet.hpp
2/*
3 * Copyright (c) 2007 Leigh Johnston.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and data forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in data form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * * Neither the name of Leigh Johnston nor the names of any
19 * other contributors to this software may be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36#pragma once
37
38#include <neolib/neolib.hpp>
39#include <vector>
41
42namespace neolib
43{
44 template <typename IdType, std::size_t MaxPacketSize = 1024, typename CharType = char, typename PacketTraits = DefaultPacketTraits>
45 class basic_tld_packet : basic_binary_data_packet<CharType, PacketTraits>
46 {
47 typedef tld_packet<IdType, CharType, PacketTraits> self_type;
49 // types
50 public:
58 private:
59 typedef std::vector<CharType> contents_type;
60 typedef typename contents_type::size_type contents_offset_type;
61 // construction
63 {
64 allocate(0);
65 }
66 // interface
67 public:
68 virtual const_pointer data() const
69 {
70 return &iContents[0];
71 }
72 virtual pointer data()
73 {
74 return &iContents[0];
75 }
76 virtual size_type length() const
77 {
78 return iContents.size();
79 }
80 virtual bool has_max_length() const
81 {
82 return MaxPacketSize != 0;
83 }
84 virtual size_type max_length() const
85 {
86 return MaxPacketSize;
87 }
88 virtual void clear()
89 {
90 iContents.clear();
91 allocate(0);
92 iWritePosition = iContents.size();
93 iReadPosition = PacketTraits::HeaderSize;
94 }
95 virtual bool take_some(const_pointer& aFirst, const_pointer aLast)
96 {
97 }
98 virtual clone_pointer clone() const
99 {
100 return clone_pointer(new self_type(*this));
101 }
102 virtual void copy_from(const basic_packet<CharType>& aSource)
103 {
104 iContents = aSource.iContents;
105 allocate(0);
106 iWritePosition = iContents.size();
107 iReadPosition = PacketTraits::HeaderSize;
108 }
109 void encode(IdType Id)
110 {
111 contents_offset_type oldPosition = iWritePosition;
112 iWritePosition = PacketTraits::IdOffset;
113 encode(static_cast<uint32_t>(Id));
114 iWritePosition = oldPosition;
115 }
116 IdType id() const
117 {
118 contents_offset_type oldPosition = iReadPosition;
119 iReadPosition = PacketTraits::IdOffset;
120 IdType result = static_cast<IdType>(decode<uint32_t>());
121 iReadPosition = oldPosition;
122 return result;
123 }
124 // implementation
125 private:
126 virtual void write(const void* aData, std::size_t aLength)
127 {
128 }
129 virtual void read(void* aData, std::size_t aLength) const
130 {
131 }
132 void allocate(std::size_t aLength)
133 {
134 iContents.resize(std::max(iContents.size() + aLength, PacketTraits::HeaderSize));
135 }
136 // attributes
137 private:
138 contents_type iContents;
139 contents_offset_type iWritePosition;
140 mutable contents_offset_type iReadPosition;
141 };
142
143 typedef basic_data_packet<char> data_packet;
144}
base_type::const_pointer const_pointer
base_type::const_iterator const_iterator
base_type::character_type character_type
base_type::character_type character_type
base_type::string_type string_type
virtual clone_pointer clone() const
virtual bool has_max_length() const
virtual void copy_from(const basic_packet< CharType > &aSource)
base_type::pointer pointer
virtual const_pointer data() const
base_type::const_iterator const_iterator
virtual size_type length() const
virtual size_type max_length() const
base_type::const_pointer const_pointer
base_type::iterator iterator
virtual bool take_some(const_pointer &aFirst, const_pointer aLast)
base_type::size_type size_type
virtual pointer data()
std::unique_ptr< i_basic_packet > clone_pointer
Definition i_packet.hpp:55
basic_data_packet< char > data_packet