public static void SendLengthPrefixed <T>(this Socket socket, byte[] data, Action <Exception, Socket>?exceptionThrown, TaskCompletionSource <T>?taskCompletionSource) { byte[] lengthPrefixed = ArrayHelpers.CombineArrays(BitConverter.GetBytes(data.Length), data); try { socket.BeginSend(lengthPrefixed, 0, lengthPrefixed.Length, SocketFlags.None, (ar) => { try { socket.EndSend(ar); } catch (SocketException e) { exceptionThrown?.Invoke(e, socket); taskCompletionSource?.TrySetException(e); } }, socket); } catch (SocketException e) { exceptionThrown?.Invoke(e, socket); taskCompletionSource?.TrySetException(e); } }
public void CombineArrays() { byte[] one = new byte[1] { 1 }; byte[] two = new byte[1] { 2 }; byte[] three = new byte[1] { 3 }; byte[] combined = ArrayHelpers.CombineArrays(one, two, three); Assert.IsTrue(combined[0] == 1); Assert.IsTrue(combined[1] == 2); Assert.IsTrue(combined[2] == 3); Assert.IsTrue(combined.Length == 3); }