Oolite 1.91.0.7604-240417-a536cbe
Loading...
Searching...
No Matches
Typedefs | Functions
OOTCPStreamDecoder.h File Reference
#include "OOTCPStreamDecoderAbstractionLayer.h"
+ Include dependency graph for OOTCPStreamDecoder.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct OOTCPStreamDecoderOOTCPStreamDecoderRef
 
typedef void(* OOTCPStreamDecoderPacketCallback) (void *cbInfo, OOALStringRef packetType, OOALDictionaryRef packet)
 
typedef void(* OOTCPStreamDecoderErrorCallback) (void *cbInfo, OOALStringRef errorDesc)
 
typedef void(* OOTCPStreamDecoderFinalizeCallback) (void *cbInfo)
 

Functions

OOTCPStreamDecoderRef OOTCPStreamDecoderCreate (OOTCPStreamDecoderPacketCallback packetCB, OOTCPStreamDecoderErrorCallback errorCB, OOTCPStreamDecoderFinalizeCallback finalizeCB, void *cbInfo)
 
void OOTCPStreamDecoderDestroy (OOTCPStreamDecoderRef decoder)
 
void OOTCPStreamDecoderReceiveData (OOTCPStreamDecoderRef decoder, OOALDataRef data)
 
void OOTCPStreamDecoderReceiveBytes (OOTCPStreamDecoderRef decoder, const void *bytes, size_t length)
 

Typedef Documentation

◆ OOTCPStreamDecoderErrorCallback

typedef void(* OOTCPStreamDecoderErrorCallback) (void *cbInfo, OOALStringRef errorDesc)

Definition at line 41 of file OOTCPStreamDecoder.h.

◆ OOTCPStreamDecoderFinalizeCallback

typedef void(* OOTCPStreamDecoderFinalizeCallback) (void *cbInfo)

Definition at line 42 of file OOTCPStreamDecoder.h.

◆ OOTCPStreamDecoderPacketCallback

typedef void(* OOTCPStreamDecoderPacketCallback) (void *cbInfo, OOALStringRef packetType, OOALDictionaryRef packet)

Definition at line 40 of file OOTCPStreamDecoder.h.

◆ OOTCPStreamDecoderRef

Definition at line 38 of file OOTCPStreamDecoder.h.

Function Documentation

◆ OOTCPStreamDecoderCreate()

OOTCPStreamDecoderRef OOTCPStreamDecoderCreate ( OOTCPStreamDecoderPacketCallback packetCB,
OOTCPStreamDecoderErrorCallback errorCB,
OOTCPStreamDecoderFinalizeCallback finalizeCB,
void * cbInfo )

Definition at line 64 of file OOTCPStreamDecoder.c.

65{
66 OOTCPStreamDecoderRef decoder = NULL;
67
68 if (packetCB == NULL) return NULL;
69
70 decoder = malloc(sizeof *decoder);
71 if (decoder == NULL) return NULL;
72
73 decoder->headerSpaceUsed = 0;
74 decoder->nextPacketData = NULL;
75 decoder->nextSize = 0;
76 decoder->Packet = packetCB;
77 decoder->Error = errorCB;
78 decoder->Finalize = finalizeCB;
79 decoder->cbInfo = cbInfo;
80
81 return decoder;
82}
OOTCPStreamDecoderFinalizeCallback Finalize
OOALMutableDataRef nextPacketData
OOTCPStreamDecoderErrorCallback Error
OOTCPStreamDecoderPacketCallback Packet

References OOTCPStreamDecoder::cbInfo, OOTCPStreamDecoder::Error, OOTCPStreamDecoder::Finalize, OOTCPStreamDecoder::headerSpaceUsed, OOTCPStreamDecoder::nextPacketData, OOTCPStreamDecoder::nextSize, and OOTCPStreamDecoder::Packet.

◆ OOTCPStreamDecoderDestroy()

void OOTCPStreamDecoderDestroy ( OOTCPStreamDecoderRef decoder)

Definition at line 85 of file OOTCPStreamDecoder.c.

86{
87 if (decoder == NULL) return;
88
89 if (decoder->Finalize != NULL)
90 {
91 decoder->Finalize(decoder->cbInfo);
92 }
93
94 if (decoder->nextPacketData != NULL)
95 {
97 decoder->nextPacketData = NULL;
98 }
99
100 free(decoder);
101}
void OOALRelease(OOALObjectRef object)

References OOTCPStreamDecoder::cbInfo, OOTCPStreamDecoder::Finalize, OOTCPStreamDecoder::nextPacketData, and OOALRelease().

+ Here is the call graph for this function:

◆ OOTCPStreamDecoderReceiveBytes()

void OOTCPStreamDecoderReceiveBytes ( OOTCPStreamDecoderRef decoder,
const void * bytes,
size_t length )

Definition at line 112 of file OOTCPStreamDecoder.c.

113{
114 const unsigned char *bytes = NULL;
115 size_t remaining;
116 size_t bytesToAdd;
117 OOALAutoreleasePoolRef pool = NULL;
118
119 if (decoder == NULL) return;
120
121 bytes = inBytes;
122 remaining = length;
123
124 if (bytes == NULL && remaining != 0)
125 {
126 Error(decoder, OOALSTR("Invalid data -- NULL bytes but %u byte count."), remaining);
127 return;
128 }
129
130 while (remaining != 0)
131 {
132 if (decoder->nextPacketData != NULL)
133 {
134 // More data expected
135 bytesToAdd = remaining;
136 if (decoder->nextSize < bytesToAdd) bytesToAdd = decoder->nextSize;
137
138 OOALMutableDataAppendBytes(decoder->nextPacketData, bytes, bytesToAdd);
139
140 remaining -= bytesToAdd;
141 decoder->nextSize -= bytesToAdd;
142 bytes += bytesToAdd;
143
144 if (decoder->nextSize == 0)
145 {
146 // Packet is ready.
148 PacketReady(decoder);
150 pool = NULL;
151
152 OOALRelease(decoder->nextPacketData);
153 decoder->nextPacketData = NULL;
154 }
155 }
156 else if (decoder->headerSpaceUsed < 4)
157 {
158 // Read bytes for packet header
159 remaining--;
160 decoder->header[decoder->headerSpaceUsed++] = *bytes++;
161 }
162 else if (decoder->headerSpaceUsed == 4)
163 {
164 // We've read a header, start on a packet.
165 decoder->nextSize = (decoder->header[0] << 24) |
166 (decoder->header[1] << 16) |
167 (decoder->header[2] << 8) |
168 (decoder->header[3] << 0);
169
170 decoder->headerSpaceUsed = 0;
171 if (decoder->nextSize != 0)
172 {
173 decoder->nextPacketData = OOALDataCreateMutable(decoder->nextSize);
174 }
175 }
176 else
177 {
178 Error(decoder, OOALSTR("OOTCPStreamDecoder internal error: reached unreachable state. nextSize = %lu, bufferUsed = %lu, nextPacketData = %@."), (unsigned long)decoder->nextSize, (unsigned long)decoder->headerSpaceUsed, decoder->nextPacketData);
179 }
180 }
181}
#define OOALSTR(x)
const struct NSAutoreleasePool * OOALAutoreleasePoolRef
void OOALMutableDataAppendBytes(OOALMutableDataRef data, const void *bytes, size_t length)
#define OOALDestroyAutoreleasePool(pool)
OOALAutoreleasePoolRef OOALCreateAutoreleasePool(void)
OOALMutableDataRef OOALDataCreateMutable(size_t capacity)
static void PacketReady(OOTCPStreamDecoderRef decoder)
static void Error(OOTCPStreamDecoderRef decoder, OOALStringRef format,...)

References Error(), OOTCPStreamDecoder::header, OOTCPStreamDecoder::headerSpaceUsed, OOTCPStreamDecoder::nextPacketData, OOTCPStreamDecoder::nextSize, OOALCreateAutoreleasePool(), OOALDataCreateMutable(), OOALDestroyAutoreleasePool, OOALMutableDataAppendBytes(), OOALRelease(), OOALSTR, and PacketReady().

Referenced by OOTCPStreamDecoderReceiveData().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ OOTCPStreamDecoderReceiveData()

void OOTCPStreamDecoderReceiveData ( OOTCPStreamDecoderRef decoder,
OOALDataRef data )

Definition at line 104 of file OOTCPStreamDecoder.c.

105{
106 if (decoder == NULL || data == NULL) return;
107
109}
const void * OOALDataGetBytePtr(OOALDataRef data)
size_t OOALDataGetLength(OOALDataRef data)
void OOTCPStreamDecoderReceiveBytes(OOTCPStreamDecoderRef decoder, const void *inBytes, size_t length)

References OOALDataGetBytePtr(), OOALDataGetLength(), and OOTCPStreamDecoderReceiveBytes().

+ Here is the call graph for this function: