Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@Arke64 Arke64 authored
Showing with 38 additions and 0 deletions.
  1. +37 −0 Socket.c
  2. +1 −0  Socket.h
View
37 Socket.c
@@ -367,6 +367,43 @@ uint32 SAL_Socket_Write(SAL_Socket* socket, const uint8* const toWrite, const ui
}
/**
+ * Send @a writeAmount bytes from @a toWrite over @a socket trying @a tries times to send the data before giving up.
+ *
+ * @param socket Socket to write to
+ * @param toWrite Buffer to write from
+ * @param writeAmount Number of bytes to write
+ * @param tries Number of times to try and send all the data
+ * @returns the number of bytes that were sent.
+ */
+uint32 SAL_Socket_EnsureWrite(SAL_Socket* socket, const uint8* const toWrite, const uint32 writeAmount, uint8 tries) {
+ uint32 sentSoFar;
+ int32 result;
+
+ assert(socket != NULL);
+ assert(toWrite != NULL);
+
+ sentSoFar = 0;
+
+ while (sentSoFar < writeAmount && tries > 0) {
+
+ #ifdef WINDOWS
+ result = send((SOCKET)socket->RawSocket, (const int8*)(toWrite + sentSoFar), writeAmount - sentSoFar, 0);
+ if (result != SOCKET_ERROR)
+ sentSoFar += result;
+ #elif defined POSIX
+ result = send(socket->RawSocket, (const int8*)(toWrite + sentSoFar), writeAmount - sentSoFar, 0);
+ if (result != -1)
+ sentSoFar += result;
+ #endif
+
+ tries--;
+ }
+
+
+ return sentSoFar;
+}
+
+/**
* Register @a callback to be called whenever data is available on @a socket.
*
* @param socket Socket to read from
View
1  Socket.h
@@ -37,6 +37,7 @@ public SAL_Socket* SAL_Socket_Accept(SAL_Socket* listener);
public void SAL_Socket_Close(SAL_Socket* socket);
public uint32 SAL_Socket_Read(SAL_Socket* socket, uint8* const buffer, const uint32 bufferSize);
public uint32 SAL_Socket_Write(SAL_Socket* socket, const uint8* const toWrite, const uint32 writeAmount);
+public uint32 SAL_Socket_EnsureWrite(SAL_Socket* socket, const uint8* const toWrite, const uint32 writeAmount, uint8 tries);
public void SAL_Socket_SetReadCallback(SAL_Socket* socket, SAL_Socket_ReadCallback callback, void* const state);
public void SAL_Socket_UnsetSocketCallback(SAL_Socket* socket);
public uint16 SAL_Socket_HostToNetworkShort(uint16 value);
Please sign in to comment.
Something went wrong with that request. Please try again.