public void EnqueueOutputData(byte[] dataToSend, int count) { lock (_stateLock) { SendIOState snap1 = _sendingState; #if DEBUG && !NETSTANDARD1_6 int currentThread = System.Threading.Thread.CurrentThread.ManagedThreadId; if (snap1 != SendIOState.ReadyNextSend) { } #endif } lock (_queueLock) { _sendingQueue.Enqueue(dataToSend); } }
/// <summary> /// send next data, after prev IO complete /// </summary> public void ProcessWaitingData() { // This method is called by I/O Completed() when an asynchronous send completes. //after IO completed, what to do next.... sendingState = SendIOState.ProcessSending; if (sendArgs.SocketError == SocketError.Success) { this.sendingTransferredBytes += sendArgs.BytesTransferred; int remainingBytes = this.sendingTargetBytes - sendingTransferredBytes; if (remainingBytes > 0) { //no complete!, //start next send ... //**** sendingState = SendIOState.ReadyNextSend; StartSendAsync(); //**** } else if (remainingBytes == 0) { //complete sending //check the queue again ... if (sendingQueue.Count > 0) { //move new chunck to current Sending data this.currentSendingData = sendingQueue.Dequeue(); if (this.currentSendingData == null) { } this.sendingTargetBytes = currentSendingData.Length; this.sendingTransferredBytes = 0; //**** sendingState = SendIOState.ReadyNextSend; StartSendAsync(); //**** } else { //no data ResetBuffer(); //notify no more data //**** sendingState = SendIOState.ReadyNextSend; notify(SendIOEventCode.SendComplete); //**** } } else { //< 0 ???? throw new NotSupportedException(); } } else { //error, socket error ResetBuffer(); sendingState = SendIOState.Error; notify(SendIOEventCode.SocketError); //manage socket errors here } }
public void StartSendAsync() { lock (stateLock) { if (sendingState != SendIOState.ReadyNextSend) { //if in other state then return return; } sendingState = SendIOState.Sending; } //------------------------------------------------------------------------ //send this data first int remaining = this.sendingTargetBytes - this.sendingTransferredBytes; if (remaining == 0) { if (this.sendingQueue.Count > 0) { this.currentSendingData = sendingQueue.Dequeue(); remaining = this.sendingTargetBytes = currentSendingData.Length; this.sendingTransferredBytes = 0; } else { //no data to send ? sendingState = SendIOState.ReadyNextSend; return; } } else if (remaining < 0) { //? throw new NotSupportedException(); } if (remaining <= this.sendBufferSize) { sendArgs.SetBuffer(this.sendStartOffset, remaining); //*** copy from src to dest if (currentSendingData != null) { Buffer.BlockCopy(this.currentSendingData, //src this.sendingTransferredBytes, sendArgs.Buffer, //dest this.sendStartOffset, remaining); } } else { //We cannot try to set the buffer any larger than its size. //So since receiveSendToken.sendBytesRemainingCount > BufferSize, we just //set it to the maximum size, to send the most data possible. sendArgs.SetBuffer(this.sendStartOffset, this.sendBufferSize); //Copy the bytes to the buffer associated with this SAEA object. Buffer.BlockCopy(this.currentSendingData, this.sendingTransferredBytes, sendArgs.Buffer, this.sendStartOffset, this.sendBufferSize); } if (!sendArgs.AcceptSocket.SendAsync(sendArgs)) { //when SendAsync return false //this means the socket can't do async send ProcessWaitingData(); } }
/// <summary> /// send next data, after prev IO complete /// </summary> public void ProcessWaitingData() { // This method is called by I/O Completed() when an asynchronous send completes. //after IO completed, what to do next.... _sendingState = SendIOState.ProcessSending; switch (_sendArgs.SocketError) { default: { //error, socket error Reset(); _sendingState = SendIOState.Error; _notify(SendIOEventCode.SocketError); //manage socket errors here } break; case SocketError.Success: { _sendingTransferredBytes += _sendArgs.BytesTransferred; int remainingBytes = _sendingTargetBytes - _sendingTransferredBytes; if (remainingBytes > 0) { //no complete!, //start next send ... //**** _sendingState = SendIOState.ReadyNextSend; StartSendAsync(); //**** } else if (remainingBytes == 0) { //complete sending //check the queue again ... bool hasSomeData = false; lock (_queueLock) { if (_sendingQueue.Count > 0) { //move new chunck to current Sending data _currentSendingData = _sendingQueue.Dequeue(); hasSomeData = true; } } if (hasSomeData) { _sendingTargetBytes = _currentSendingData.Length; _sendingTransferredBytes = 0; //**** _sendingState = SendIOState.ReadyNextSend; StartSendAsync(); //**** } else { //no data Reset(); //notify no more data //**** _sendingState = SendIOState.ReadyNextSend; _notify(SendIOEventCode.SendComplete); //**** } } else { //< 0 ???? // throw new NotSupportedException(); Reset(); //notify no more data //**** _sendingState = SendIOState.ReadyNextSend; _notify(SendIOEventCode.SendComplete); } } break; } }
public void StartSendAsync() { lock (_stateLock) { if (_sendingState != SendIOState.ReadyNextSend) { //if in other state then return return; } #if DEBUG && !NETSTANDARD1_6 dbugSendingTheadId = System.Threading.Thread.CurrentThread.ManagedThreadId; #endif _sendingState = SendIOState.Sending; } //------------------------------------------------------------------------ //send this data first int remaining = _sendingTargetBytes - _sendingTransferredBytes; if (remaining == 0) { bool hasSomeData = false; lock (_queueLock) { if (_sendingQueue.Count > 0) { _currentSendingData = _sendingQueue.Dequeue(); remaining = _sendingTargetBytes = _currentSendingData.Length; _sendingTransferredBytes = 0; hasSomeData = true; } } if (!hasSomeData) { //no data to send ? _sendingState = SendIOState.ReadyNextSend; return; } } else if (remaining < 0) { //? throw new NotSupportedException(); } if (remaining <= _sendBufferSize) { _sendArgs.SetBuffer(_sendStartOffset, remaining); //*** copy from src to dest if (_currentSendingData != null) { Buffer.BlockCopy(_currentSendingData, //src _sendingTransferredBytes, _sendArgs.Buffer, //dest _sendStartOffset, remaining); } } else { //We cannot try to set the buffer any larger than its size. //So since receiveSendToken.sendBytesRemainingCount > BufferSize, we just //set it to the maximum size, to send the most data possible. _sendArgs.SetBuffer(_sendStartOffset, _sendBufferSize); //Copy the bytes to the buffer associated with this SAEA object. Buffer.BlockCopy(_currentSendingData, _sendingTransferredBytes, _sendArgs.Buffer, _sendStartOffset, _sendBufferSize); } if (!_sendArgs.AcceptSocket.SendAsync(_sendArgs)) { //when SendAsync return false //this means the socket can't do async send ProcessWaitingData(); } }
public void StartSendAsync() { lock (_stateLock) { if (_sendingState != SendIOState.ReadyNextSend) { //if in other state then return return; } _sendingState = SendIOState.Sending; } //------------------------------------------------------------------------ //send this data first int remaining = _sendingTargetBytes - _sendingTransferredBytes; if (remaining == 0) { bool hasSomeData = false; lock (_queueLock) { if (_sendingQueue.Count > 0) { _currentSendingData = _sendingQueue.Dequeue(); remaining = _sendingTargetBytes = _currentSendingData.Length; _sendingTransferredBytes = 0; hasSomeData = true; } } if (!hasSomeData) { //no data to send ? _sendingState = SendIOState.ReadyNextSend; return; } } else if (remaining < 0) { //? throw new NotSupportedException(); } //----------------------------------------------------------- //send to network stream //.... byte[] sendingData = null;// CreateTestHtmlRespMsg("hello!"); using (MemoryStream ms1 = new MemoryStream()) { ms1.Write(_currentSendingData, 0, _currentSendingData.Length); while (_sendingQueue.Count > 0) { byte[] anotherBuffer = _sendingQueue.Dequeue(); ms1.Write(anotherBuffer, 0, anotherBuffer.Length); } sendingData = ms1.ToArray(); } if (!_networkStream.WriteBuffer(sendingData, 0, sendingData.Length)) { remaining = 0; _sendingTargetBytes = _sendingTransferredBytes; } else { //some data pending ... } }