public void Send(byte[] data, IConnection connection, IBandwidthController bandwidthController, SuccessCallback onSuccess, FailureCallback onFailure) { var buffer = new Buffer(data); SendInternal(IOState.Create(buffer, buffer.Size, connection, bandwidthController, onSuccess, onFailure)); }
private void ReceiveInternal(IOState state) { if (!state.BandwidthController.CanTransmit(state.PendingBytes)) { _receiveQueue.Add(state); return; } if (state.WaitingForBuffer) { state.Buffer = _bufferAllocator.Allocate(state.Bytes); if (state.WaitingForBuffer) { _receiveQueue.Add(state); return; } } state.BandwidthController.SetTransmittion(state.PendingBytes); state.Connection.Receive(state.GetBufferForPending(), (readCount, success) => { try { if (success && readCount > 0) { if (readCount < state.PendingBytes) { state.PendingBytes -= readCount; _receiveQueue.Add(state); } else { var data = state.GetData(); state.SuccessCallback(data); } } else { state.FailureCallback(); } } finally { state.Release(); _bufferAllocator.Free(state.Buffer); } }); }
public void Receive(int bytes, IConnection connection, IBandwidthController bandwidthController, SuccessCallback onSuccess, FailureCallback onFailure) { ReceiveInternal(IOState.Create(null, bytes, connection, bandwidthController, onSuccess, onFailure)); }
private void SendInternal(IOState state) { if (!state.BandwidthController.CanTransmit(state.PendingBytes)) { _sendQueue.Add(state); return; } if (state.WaitingForBuffer) { state.Buffer = _bufferAllocator.Allocate(state.Bytes); if (state.WaitingForBuffer) { _sendQueue.Add(state); return; } } state.BandwidthController.SetTransmittion(state.PendingBytes); state.Connection.Send(state.GetBufferForPending(), (sentCount, success) => { try { if(success && sentCount > 0) { if (sentCount < state.PendingBytes) { state.PendingBytes -= sentCount; _sendQueue.Add(state); } else { var data = state.GetData(); state.SuccessCallback(data); } } else { state.FailureCallback(); } } finally { state.Release(); _bufferAllocator.Free(state.Buffer); } }); }