Source

blake / blake_netbuffer_wrapper.cpp

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
Copyright (C) 2001-2006 Daybo Logic.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. Neither the name of the project nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
/*
  NetBuffer's C++ class is now accessable through this interface.
  The reason we put this together was so that clients written in
  languages other than C++ can more easily access the NetBuffer
  component (which is technically quite complicated).
*/

#define BLAKE_SOURCE

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif //HAVE_CONFIG_H

#include <time.h>
#include <stdio.h>

#ifdef HAVE_LIMITS_H
# include <limits.h>
#endif

#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif

#ifdef HAVE_STRING_H
# include <string.h>
#endif

#if defined(__WIN32__) && defined(HAVE_WINDOWS_H)
# include <windows.h>
#endif /*__WIN32__ && HAVE_WINDOWS_H*/

#ifdef BLAKE_HDRSTOP
# pragma hdrstop
#endif /*BLAKE_HDRSTOP*/

#ifdef HAVE_DLSTDINC_H
# include "dlstdinc.h"
#endif

#include "blake_threads.h"
#include "blake.h"
#include "blake_main.h"
#include "blake_netbuffer.hpp" // C++ NetBuffer class
#include "blake_netbuffer_wrapper.h"
/*-------------------------------------------------------------------------*/
static const char *const rcsid =
  "$Id: blake_netbuffer.cpp,v 1.0 2006/03/19 20:05:00 ddrp Exp $";

static const char* errorMessages_en[] = {
  "No error", /*bnbeNoError*/
  "Cannot accomodate data", /*bnbeCannotAccommodate*/
  "Data of extreme size", /*bnbeExtremeSize*/
  "Bad direction of data travel", /*bnbeBadDirection*/
  "The socket is already registered", /*bnbeSocketRegistered*/
  "NetBuffer was not initialised", /*bnbeUninitialised*/
  "The socket is not registered", /*bnbeSocketNotRegistered*/
  "Not enough memory"  /*bnbeNotEnoughMem*/
};

static const char* errorMessages_fr[] = {
  "aucune erreur", /*bnbeNoError*/
  "ne peut adapter  des donnes", /*bnbeCannotAccommodate*/
  "donnes de taille extrme", /*bnbeExtremeSize*/
  "la que mauvaise direction des donnes voyagent", /*bnbeBadDirection*/
  "la douille est dj enregistre", /*bnbeSocketRegistered*/
  "NetBuffer n'a pas t initialise", /*bnbeUninitialised*/
  "la douille n'est pas enregistre", /*bnbeSocketNotRegistered*/
  "pas assez de mmoire" /*bnbeNotEnoughMem*/
};

static const char* errorMessages_es[] = {
  "ningn error", /*bnbeNoError*/
  "puede no acomodar dato", /*bnbeCannotAccommodate*/
  "dato de extremo tamao", /*bnbeExtremeSize*/
  "malo direccin de dato recorrido", /*bnbeBadDirection*/
  "zcalo es ya colocado", /*bnbeSocketRegistered*/
  "NetBuffer era no inicializado", /*bnbeUninitialised*/
  "zcalo es no colocado", /*bnbeSocketNotRegistered*/
  "no bastante de la memoria del", /*bnbeNotEnoughMem*/
};

static const char* errorMessages_de[] = {
  "kein Strung", /*bnbeNoError*/
  "knnen nicht unterbringen Daten", /*bnbeCannotAccommodate*/
  "Daten von extrem Gre", /*bnbeExtremeSize*/
  "schlecht Richtung von Daten Spielraum", /*bnbeBadDirection*/
  "Einfaung ist bereits registriert", /*bnbeSocketRegistered*/
  "NetBuffer war nicht initialisiert", /*bnbeUninitialised*/
  "Einfaung ist nicht registriert", /*bnbeSocketNotRegistered*/
  "nicht gengend Gedchtnis" /*bnbeNotEnoughMem*/
};

static const char* errorMessages_it[] = {
  "nessun errore", /*bnbeNoError*/
  "pu non accomodare dato", /*bnbeCannotAccommodate*/
  "dato di estremo formato", /*bnbeExtremeSize*/
  "difettoso senso di dato corsa", /*bnbeBadDirection*/
  "zoccolo  gi registrato", /*bnbeSocketRegistered*/
  "NetBuffer era non inizializzato", /*bnbeUninitialised*/
  "zoccolo  non registrato", /*bnbeSocketNotRegistered*/
  "non abbastanza di memoria del" /*bnbeNotEnoughMem*/
};

static const char* errorMessages_pt[] = {
  "nenhum erro", /*bnbeNoError*/
  "pode no para acomodar dados", /*bnbeCannotAccommodate*/
  "dados de extremo tamanho", /*bnbeExtremeSize*/
  "mau sentido de dados curso", /*bnbeBadDirection*/
  "soquete  j registado", /*bnbeSocketRegistered*/
  "NetBuffer era no inicializado", /*bnbeUninitialised*/
  "soquete  no registado", /*bnbeSocketNotRegistered*/
  "no bastante da memria do" /*bnbeNotEnoughMem*/
};

static bool inited;
static struct {

  #ifdef BLAKE_THREADS
  dtlock_t lock; /* Must be locked before anything is touched! */
  #endif //BLAKE_THREADS

  blake_link_ptrs socketManagers;
} data;

static void i_DestroyAllSocketManagers();
static void i_DeleteSocketManager(const unsigned int Index);
static Blake::TNetBuffer *i_GetManagerForSocket(
  const int Socket,
  unsigned int *PIndex
);
/*-------------------------------------------------------------------------*/
void blake_int_netbuffer_NCI()
{
  blake_Unused(rcsid);
}
/*-------------------------------------------------------------------------*/
OVERCHAT_C_IN_CPP bool netbuffer_Init()
{
  if ( !inited ) {
    daybothreads_CreateLock(&data.lock);
    data.socketManagers.size = sizeof(data.socketManagers);
    blake_InitLinkPtrs(&data.socketManagers, sizeof(Blake::TNetBuffer*));
    inited = !inited;
  }
  return inited;
}
/*-------------------------------------------------------------------------*/
OVERCHAT_C_IN_CPP void netbuffer_Cleanup()
{
  if ( inited ) {
    daybothreads_EnterLock(&data.lock);
    i_DestroyAllSocketManagers();
    daybothreads_LeaveLock(&data.lock);
    daybothreads_DestroyLock(&data.lock);
    inited = !inited;
  }
  return;
}
/*-------------------------------------------------------------------------*/
OVERCHAT_C_IN_CPP bool netbuffer_IsInited()
{
  return inited;
}
/*-------------------------------------------------------------------------*/
static void i_DestroyAllSocketManagers()
{
  while ( data.socketManagers.Count(&data.socketManagers) ) {
    Blake::TNetBuffer* classPtr = NULL;
    data.socketManagers.Get(&data.socketManagers, 0, &classPtr);
    if ( classPtr )
      delete classPtr; /* Destroy class */
    data.socketManagers.DelByPos(&data.socketManagers, 0);
  }
  return;
}
/*-------------------------------------------------------------------------*/
static Blake::TNetBuffer *i_GetManagerForSocket(
  const int Socket,
  unsigned int *PIndex)
{
  unsigned int i, c;
  Blake::TNetBuffer *theNetBuffer = NULL;

  c = data.socketManagers.Count(&data.socketManagers);
  for ( i = 0U; i < c; i++ ) {
    Blake::TNetBuffer *thisNetBuffer = NULL;

    data.socketManagers.Get(&data.socketManagers, i, &thisNetBuffer);
    if ( thisNetBuffer ) {
      if ( thisNetBuffer->GetSocket() == Socket ) {
        theNetBuffer = thisNetBuffer; /* Yup, this is it */
        if ( PIndex ) /* Caller wants it's index? */
          *PIndex = i;
      }
    }
  }
  return theNetBuffer;
}
/*-------------------------------------------------------------------------*/
static void i_DeleteSocketManager(const unsigned int Index)
{
  Blake::TNetBuffer *nbp;

  if ( Index < data.socketManagers.Count(&data.socketManagers) ) {
    data.socketManagers.Get(&data.socketManagers, Index, &nbp);
    if ( nbp )
      delete nbp;
    data.socketManagers.DelByPos(&data.socketManagers, Index);
  }
  return;
}
/*-------------------------------------------------------------------------*/
BLAKEAPI(unsigned int) netbuffer_GetErrorMessage(const enum blakeNetBufferError NetBufferError, char* Buff, size_t MaxSize, enum blakeLang Language)
{
  unsigned int errorIndex = (unsigned int)NetBufferError;
  const char* msgptr;

  if ( !inited ) return 0U;

  switch ( Language ) {
    case langEnglish : {
      if ( errorIndex < sizeof(errorMessages_en)/sizeof(errorMessages_en[0]) )
        msgptr = errorMessages_en[errorIndex];
      else
        msgptr = blake_int_NoSuchError(langEnglish);
      break;
    }
    case langFrench : {
      if ( errorIndex < sizeof(errorMessages_fr)/sizeof(errorMessages_fr[0]) )
        msgptr = errorMessages_fr[errorIndex];
      else
        msgptr = blake_int_NoSuchError(langFrench);
      break;
    }
    case langSpanish : {
      if ( errorIndex < sizeof(errorMessages_es)/sizeof(errorMessages_es[0]) )
        msgptr = errorMessages_es[errorIndex];
      else
        msgptr = blake_int_NoSuchError(langSpanish);
      break;
    }
    case langGerman : {
      if ( errorIndex < sizeof(errorMessages_de)/sizeof(errorMessages_de[0]) )
        msgptr = errorMessages_de[errorIndex];
      else
        msgptr = blake_int_NoSuchError(langGerman);
      break;
    }
    case langItalian : {
      if ( errorIndex < sizeof(errorMessages_it)/sizeof(errorMessages_it[0]) )
        msgptr = errorMessages_it[errorIndex];
      else
        msgptr = blake_int_NoSuchError(langItalian);
      break;
    }
    case langPortuguese : {
      if ( errorIndex < sizeof(errorMessages_pt)/sizeof(errorMessages_pt[0]) )
        msgptr = errorMessages_pt[errorIndex];
      else
        msgptr = blake_int_NoSuchError(langPortuguese);
      break;
    }
    default : {
      msgptr = NULL;
    }
  }

  if ( Buff && MaxSize ) {
    Buff[0] = '\0';
    if ( msgptr )
      strncat(Buff, msgptr, MaxSize);
  }

  if ( msgptr )
    return (unsigned int)strlen(msgptr);

  return 0;
}
/*-------------------------------------------------------------------------*/
BLAKEAPI(const char*) netbuffer_GetErrorMessageMnemonic(
  const enum blakeNetBufferError NetBufferError
)
{
  unsigned int errIndex;
  static const char *mnemonics[] = {
    "bnbeNoError",
    "bnbeCannotAccommodate",
    "bnbeExtremeSize",
    "bnbeBadDirection",
    "bnbeSocketRegistered",
    "bnbeUninitialised",
    "bnbeSocketNotRegistered",
    "bnbeNotEnoughMem"
  };

  errIndex = (unsigned int)NetBufferError;
  if ( errIndex >= sizeof(mnemonics)/sizeof(mnemonics[0]) )
    return NULL;
  return mnemonics[NetBufferError];
}
/*-------------------------------------------------------------------------*/
BLAKEAPI(void) netbuffer_Close(int Socket)
{
  if ( inited ) {
    Blake::TNetBuffer *classPtr;
    unsigned int indexOfManager;

    daybothreads_EnterLock(&data.lock);
    classPtr = i_GetManagerForSocket(Socket, &indexOfManager);
    if ( classPtr )
      i_DeleteSocketManager(indexOfManager);
    daybothreads_LeaveLock(&data.lock);
  }
  return;
}
/*-------------------------------------------------------------------------*/
BLAKEAPI(enum blakeNetBufferError) netbuffer_Push(int Socket, enum blakeNetBufferDirection Direction, const void* Data, const size_t DataSize)
{
  enum blakeNetBufferError err;
  Blake::TNetBuffer *nbp;

  if ( !inited ) return bnbeUninitialised;
  daybothreads_EnterLock(&data.lock);
  nbp = i_GetManagerForSocket(Socket, NULL); /* Get the manager, we don't care about it's index */
  if ( nbp )
    err = nbp->Push(Direction, Data, DataSize);
  else
    err = bnbeSocketNotRegistered;

  daybothreads_LeaveLock(&data.lock);

  return err;
}
/*-------------------------------------------------------------------------*/
BLAKEAPI(size_t) netbuffer_Pop(int Socket, enum blakeNetBufferDirection Direction, void* Data, const size_t MaxDataSize, enum blakeNetBufferError* PNetError)
{
  size_t sizeRet = 0U;
  enum blakeNetBufferError bnbeErr = bnbeNoError;
  Blake::TNetBuffer* nbp;

  if ( !inited ) return bnbeUninitialised;
  daybothreads_EnterLock(&data.lock);
  nbp = i_GetManagerForSocket(Socket, NULL);
  if ( nbp )
    sizeRet = nbp->Pop(Direction, Data, MaxDataSize, &bnbeErr);
  else
    bnbeErr = bnbeSocketNotRegistered;

  daybothreads_LeaveLock(&data.lock);

  if ( PNetError )
    *PNetError = bnbeErr;

  return sizeRet;
}
/*-------------------------------------------------------------------------*/
BLAKEAPI(enum blakeNetBufferError) netbuffer_Open(int Socket)
{
  enum blakeNetBufferError bnbeErr = bnbeNoError;
  Blake::TNetBuffer* newNetBuffer;
  unsigned int i, c;

  if ( !inited ) return bnbeUninitialised;

  /* Check the socket's not in use */
  daybothreads_EnterLock(&data.lock);
  c = data.socketManagers.Count(&data.socketManagers);
  for ( i = 0U; i < c; i++ ) {
    Blake::TNetBuffer* currentNetBuffer;
    data.socketManagers.Get(&data.socketManagers, i, &currentNetBuffer);
    if ( currentNetBuffer->GetSocket() == Socket )
      bnbeErr = bnbeSocketRegistered; /* Socket already registered */
  }
  if ( bnbeErr == bnbeNoError ) {
    newNetBuffer = new Blake::TNetBuffer(Socket);
    if ( newNetBuffer ) {
      if ( data.socketManagers.AddToTail(&data.socketManagers, &newNetBuffer) != blakeSuccess ) {
        delete newNetBuffer;
        bnbeErr = bnbeNotEnoughMem;
      }
    }
    else
      bnbeErr = bnbeNotEnoughMem;
  }

  daybothreads_LeaveLock(&data.lock);
  return bnbeErr;
}
/*-------------------------------------------------------------------------*/