/// <summary> /// Begins receiving the incoming message by handler as /// an asynchronous call. It will not block the caller. /// </summary> /// <param name="handler">An instance of the incoming message handler.</param> public void Receive(NetworkIncomingMessageHandler handler) { if (listener.IsAvailable(identifier)) { incoming.ReceiveOrCallback(handler); } }
public void Send(NetworkOutgoingMessage message, Action callback) { if (listener.IsAvailable(identifier)) { DataBlock block = new NetworkOutgoingBlock(memory.Data, offset, message.Length); SocketBuffer buffer = new SocketBuffer(memory.Data, offset, message.Length); message.ToBytes(block); encryptor?.Encrypt(memory.Data, offset, message.Length); offset += message.Length; parts.Add(offset); socket.Send(buffer, OnSent(callback)); } }
/// <summary> /// Begins receiving data from the remote endpoint. If the buffer already /// contains data it will wait anyway for additional remote data. The handler /// will be notified in asynchronous way. /// </summary> /// <param name="handler">An instance of the incoming message handler.</param> public void Receive(NetworkIncomingMessageHandler handler) { if (listener.IsAvailable(identifier)) { int receiveOffset; int receiveSize; if (offset + length >= memory.Length) { receiveOffset = offset + length - memory.Length; receiveSize = offset - (offset + length) % memory.Length; } else { receiveOffset = offset + length; receiveSize = memory.Length - offset - length; } socket.Receive(new SocketBuffer(memory.Data, receiveOffset, receiveSize), context => OnReceived(context, handler)); } }