Oolite
Loading...
Searching...
No Matches
OOTCPStreamDecoder.c
Go to the documentation of this file.
1/*
2
3OOTCPStreamDecoder.c
4
5
6Copyright (C) 2007 Jens Ayton and contributors
7
8Permission is hereby granted, free of charge, to any person obtaining a copy
9of this software and associated documentation files (the "Software"), to deal
10in the Software without restriction, including without limitation the rights
11to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12copies of the Software, and to permit persons to whom the Software is
13furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in all
16copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24SOFTWARE.
25
26*/
27
28#ifdef OO_DEBUG_MONITOR_ENABLED
29
30#include "OOTCPStreamDecoder.h"
32#include <stdarg.h>
33#include <stdint.h>
34#include <stdlib.h>
35
36#ifdef OO_LOG_DEBUG_PROTOCOL_PACKETS
37extern void LogOOTCPStreamDecoderPacket(OOALDictionaryRef packet);
38#else
39#define LogOOTCPStreamDecoderPacket(packet) \
40 do { \
41 } while (0)
42#endif
43
44struct OOTCPStreamDecoder {
45 uint8_t header[4];
46 uint32_t headerSpaceUsed;
47 OOALMutableDataRef nextPacketData;
48 uint32_t nextSize;
49
53
54 void* cbInfo;
55};
56
57static void Error(OOTCPStreamDecoderRef decoder, OOALStringRef format, ...);
58static void PacketReady(OOTCPStreamDecoderRef decoder);
59
61{
62 OOTCPStreamDecoderRef decoder = NULL;
63
64 if (packetCB == NULL)
65 return NULL;
66
67 decoder = malloc(sizeof *decoder);
68 if (decoder == NULL)
69 return NULL;
70
71 decoder->headerSpaceUsed = 0;
72 decoder->nextPacketData = NULL;
73 decoder->nextSize = 0;
74 decoder->Packet = packetCB;
75 decoder->Error = errorCB;
76 decoder->Finalize = finalizeCB;
77 decoder->cbInfo = cbInfo;
78
79 return decoder;
80}
81
83{
84 if (decoder == NULL)
85 return;
86
87 if (decoder->Finalize != NULL) {
88 decoder->Finalize(decoder->cbInfo);
89 }
90
91 if (decoder->nextPacketData != NULL) {
92 OOALRelease(decoder->nextPacketData);
93 decoder->nextPacketData = NULL;
94 }
95
96 free(decoder);
97}
98
100{
101 if (decoder == NULL || data == NULL)
102 return;
103
105}
106
107void OOTCPStreamDecoderReceiveBytes(OOTCPStreamDecoderRef decoder, const void* inBytes, size_t length)
108{
109 const unsigned char* bytes = NULL;
110 size_t remaining;
111 size_t bytesToAdd;
112 OOALAutoreleasePoolRef pool = NULL;
113
114 if (decoder == NULL)
115 return;
116
117 bytes = inBytes;
118 remaining = length;
119
120 if (bytes == NULL && remaining != 0) {
121 Error(decoder, OOALSTR("Invalid data -- NULL bytes but %u byte count."), remaining);
122 return;
123 }
124
125 while (remaining != 0) {
126 if (decoder->nextPacketData != NULL) {
127 // More data expected
128 bytesToAdd = remaining;
129 if (decoder->nextSize < bytesToAdd)
130 bytesToAdd = decoder->nextSize;
131
132 OOALMutableDataAppendBytes(decoder->nextPacketData, bytes, bytesToAdd);
133
134 remaining -= bytesToAdd;
135 decoder->nextSize -= bytesToAdd;
136 bytes += bytesToAdd;
137
138 if (decoder->nextSize == 0) {
139 // Packet is ready.
141 PacketReady(decoder);
143 pool = NULL;
144
145 OOALRelease(decoder->nextPacketData);
146 decoder->nextPacketData = NULL;
147 }
148 } else if (decoder->headerSpaceUsed < 4) {
149 // Read bytes for packet header
150 remaining--;
151 decoder->header[decoder->headerSpaceUsed++] = *bytes++;
152 } else if (decoder->headerSpaceUsed == 4) {
153 // We've read a header, start on a packet.
154 decoder->nextSize = (decoder->header[0] << 24) | (decoder->header[1] << 16) | (decoder->header[2] << 8) | (decoder->header[3] << 0);
155
156 decoder->headerSpaceUsed = 0;
157 if (decoder->nextSize != 0) {
158 decoder->nextPacketData = OOALDataCreateMutable(decoder->nextSize);
159 }
160 } else {
161 Error(decoder, OOALSTR("OOTCPStreamDecoder internal error: reached unreachable state. nextSize = %zu, bufferUsed = %zu, nextPacketData = %@."), (unsigned long)decoder->nextSize, (unsigned long)decoder->headerSpaceUsed, decoder->nextPacketData);
162 }
163 }
164}
165
166static void PacketReady(OOTCPStreamDecoderRef decoder)
167{
168 OOALDictionaryRef packet = NULL;
169 OOALStringRef errorString = NULL;
170 OOALStringRef packetType = NULL;
171
172 packet = OOALPropertyListFromData(decoder->nextPacketData, &errorString);
173
174 // Ensure that it's a property list.
175 if (packet == NULL) {
176 Error(decoder, OOALSTR("Protocol error: packet is not property list (property list error: %@)."), errorString);
177 OOALRelease(errorString);
178 return;
179 }
180
181 // Ensure that it's a dictionary.
182 if (!OOALIsDictionary(packet)) {
183 Error(decoder, OOALSTR("Protocol error: packet is a %@, not a dictionary."), OOTypeDescription(packet));
184 return;
185 }
186
187 LogOOTCPStreamDecoderPacket(packet);
188
189 // Get packet type (and ensure that there is one).
190 packetType = OOALDictionaryGetValue(packet, kOOTCPPacketType);
191 if (packetType == NULL) {
192 Error(decoder, OOALSTR("Protocol error: packet contains no packet type."));
193 return;
194 }
195
196 if (!OOALIsString(packetType)) {
197 Error(decoder, OOALSTR("Protocol error: packet type is a %@, not a string."), OOTypeDescription(packetType));
198 return;
199 }
200
201 decoder->Packet(decoder->cbInfo, packetType, packet);
202}
203
204static void Error(OOTCPStreamDecoderRef decoder, OOALStringRef format, ...)
205{
206 va_list args;
207 OOALStringRef string = NULL;
208
209 if (decoder == NULL || decoder->Error == NULL || format == NULL)
210 return;
211
212 va_start(args, format);
213 string = OOALStringCreateWithFormatAndArguments(format, args);
214 va_end(args);
215
216 if (string != NULL) {
217 decoder->Error(decoder->cbInfo, string);
218 OOALRelease(string);
219 }
220}
221
222#endif /* OO_DEBUG_MONITOR_ENABLED */
#define OOALSTR(x)
#define kOOTCPPacketType
static NSError * Error(OOPListSchemaVerifierErrorCode errorCode, BackLinkChain *keyPath, NSString *format,...)
OOALStringRef OOTypeDescription(OOALObjectRef object)
bool OOALIsDictionary(OOALObjectRef object)
OOALStringRef OOALStringCreateWithFormatAndArguments(OOALStringRef format, va_list args)
const struct NSAutoreleasePool * OOALAutoreleasePoolRef
void OOALMutableDataAppendBytes(OOALMutableDataRef data, const void *bytes, size_t length)
const void * OOALDataGetBytePtr(OOALDataRef data)
const struct NSString * OOALStringRef
size_t OOALDataGetLength(OOALDataRef data)
OOALObjectRef OOALDictionaryGetValue(OOALDictionaryRef dictionary, OOALObjectRef key)
#define OOALDestroyAutoreleasePool(pool)
const struct NSDictionary * OOALDictionaryRef
bool OOALIsString(OOALObjectRef object)
OOALAutoreleasePoolRef OOALCreateAutoreleasePool(void)
struct NSData * OOALMutableDataRef
OOALMutableDataRef OOALDataCreateMutable(size_t capacity)
void OOALRelease(OOALObjectRef object)
OOALObjectRef OOALPropertyListFromData(OOALMutableDataRef data, OOALStringRef *errStr)
const struct NSData * OOALDataRef
struct OOTCPStreamDecoder * OOTCPStreamDecoderRef
void(* OOTCPStreamDecoderFinalizeCallback)(void *cbInfo)
OOTCPStreamDecoderRef OOTCPStreamDecoderCreate(OOTCPStreamDecoderPacketCallback packetCB, OOTCPStreamDecoderErrorCallback errorCB, OOTCPStreamDecoderFinalizeCallback finalizeCB, void *cbInfo)
void(* OOTCPStreamDecoderPacketCallback)(void *cbInfo, OOALStringRef packetType, OOALDictionaryRef packet)
void OOTCPStreamDecoderReceiveBytes(OOTCPStreamDecoderRef decoder, const void *bytes, size_t length)
void OOTCPStreamDecoderDestroy(OOTCPStreamDecoderRef decoder)
void(* OOTCPStreamDecoderErrorCallback)(void *cbInfo, OOALStringRef errorDesc)
void OOTCPStreamDecoderReceiveData(OOTCPStreamDecoderRef decoder, OOALDataRef data)