Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added EnsureWrite to Socket.

EnsureWrite will try a user specified number of times to send all the
data returning the amount of data sent.
  • Loading branch information...
commit 1819b968943d1f18c61fb7d15a6b2d1d9987de0c 1 parent 49a79e2
John authored

Showing 2 changed files with 38 additions and 0 deletions. Show diff stats Hide diff stats

  1. +37 0 Socket.c
  2. +1 0  Socket.h
37 Socket.c
@@ -367,6 +367,43 @@ uint32 SAL_Socket_Write(SAL_Socket* socket, const uint8* const toWrite, const ui
367 367 }
368 368
369 369 /**
  370 + * Send @a writeAmount bytes from @a toWrite over @a socket trying @a tries times to send the data before giving up.
  371 + *
  372 + * @param socket Socket to write to
  373 + * @param toWrite Buffer to write from
  374 + * @param writeAmount Number of bytes to write
  375 + * @param tries Number of times to try and send all the data
  376 + * @returns the number of bytes that were sent.
  377 + */
  378 +uint32 SAL_Socket_EnsureWrite(SAL_Socket* socket, const uint8* const toWrite, const uint32 writeAmount, uint8 tries) {
  379 + uint32 sentSoFar;
  380 + int32 result;
  381 +
  382 + assert(socket != NULL);
  383 + assert(toWrite != NULL);
  384 +
  385 + sentSoFar = 0;
  386 +
  387 + while (sentSoFar < writeAmount && tries > 0) {
  388 +
  389 + #ifdef WINDOWS
  390 + result = send((SOCKET)socket->RawSocket, (const int8*)(toWrite + sentSoFar), writeAmount - sentSoFar, 0);
  391 + if (result != SOCKET_ERROR)
  392 + sentSoFar += result;
  393 + #elif defined POSIX
  394 + result = send(socket->RawSocket, (const int8*)(toWrite + sentSoFar), writeAmount - sentSoFar, 0);
  395 + if (result != -1)
  396 + sentSoFar += result;
  397 + #endif
  398 +
  399 + tries--;
  400 + }
  401 +
  402 +
  403 + return sentSoFar;
  404 +}
  405 +
  406 +/**
370 407 * Register @a callback to be called whenever data is available on @a socket.
371 408 *
372 409 * @param socket Socket to read from
1  Socket.h
@@ -37,6 +37,7 @@ public SAL_Socket* SAL_Socket_Accept(SAL_Socket* listener);
37 37 public void SAL_Socket_Close(SAL_Socket* socket);
38 38 public uint32 SAL_Socket_Read(SAL_Socket* socket, uint8* const buffer, const uint32 bufferSize);
39 39 public uint32 SAL_Socket_Write(SAL_Socket* socket, const uint8* const toWrite, const uint32 writeAmount);
  40 +public uint32 SAL_Socket_EnsureWrite(SAL_Socket* socket, const uint8* const toWrite, const uint32 writeAmount, uint8 tries);
40 41 public void SAL_Socket_SetReadCallback(SAL_Socket* socket, SAL_Socket_ReadCallback callback, void* const state);
41 42 public void SAL_Socket_UnsetSocketCallback(SAL_Socket* socket);
42 43 public uint16 SAL_Socket_HostToNetworkShort(uint16 value);

0 comments on commit 1819b96

Please sign in to comment.
Something went wrong with that request. Please try again.