public AsyncReceiveState(RpcConnection connection, AsyncReceiveBuffer buffer, Socket socket) { _buffer = buffer; _socket = socket; _connection = connection; }
void AsyncReadResult(AsyncReceiveBuffer buffer) { try { buffer.Socket.BeginReceiveFrom(buffer, OnAsyncResultReceived); } catch (ObjectDisposedException) { // Socket disposed while we were receiving from it... just ignore this } }
public bool TryToComplete(out AsyncReceiveBuffer buffer) { buffer = null; if (Interlocked.CompareExchange(ref _completed, 1L, 0L) == 0L) { buffer = _buffer; return(true); } return(false); }
private void OnAsyncResultReceived(IAsyncResult asyncResult) { AsyncReceiveBuffer buffer = (AsyncReceiveBuffer)asyncResult.AsyncState; buffer.BytesReceived = buffer.Socket.EndReceiveFrom(asyncResult, ref buffer.SenderEndPoint); if (OnAsyncResultReceived(buffer)) { AsyncReadResult(buffer); } else { buffer.Socket.Close(); } }
private void OnAsyncResultReceived(IAsyncResult asyncResult) { try { AsyncReceiveBuffer buffer = (AsyncReceiveBuffer)asyncResult.AsyncState; buffer.BytesReceived = buffer.Socket.EndReceiveFrom(asyncResult, ref buffer.SenderEndPoint); if (OnAsyncResultReceived(buffer)) { AsyncReadResult(buffer); } else { buffer.Socket.Close(); } } catch (ObjectDisposedException) { } // If our socket got disposed, there isn't much we can do }
private bool BeginReceive(AsyncReceiveBuffer asyncReceiveBuffer) { if (Interlocked.CompareExchange(ref _inReceiveCycle, 1L, 0L) != 0L) { return(false); } try { if (asyncReceiveBuffer is null) { asyncReceiveBuffer = (_asyncReceiveBuffer = new AsyncReceiveBuffer()); } var socket = _socket; if (!socket.IsConnected()) { Interlocked.Exchange(ref _inReceiveCycle, 0L); return(false); } var asyncResult = socket.BeginReceive(asyncReceiveBuffer.Buffer, 0, asyncReceiveBuffer.Length, SocketFlags.None, out SocketError errorCode, OnReceiveCompleted, new AsyncReceiveState(this, asyncReceiveBuffer, socket)); if (errorCode.IsSocketError()) { throw new SocketException((int)errorCode); } if (asyncResult?.CompletedSynchronously ?? false) { DoReceiveCompleted(asyncResult, true); } return(true); } catch (Exception) { Interlocked.Exchange(ref _inReceiveCycle, 0L); Interlocked.Exchange(ref _receiving, 0L); Close(); } return(false); }
internal override bool OnAsyncResultReceived(AsyncReceiveBuffer result) { try { HttpDatagram dgram = HttpDatagram.Parse(result.Buffer); if (dgram == null) { return(true); } try { client.ServiceCache.Add(new BrowseService(dgram, false)); } catch (Exception e) { Log.Exception("Invalid browse response", e); } } catch (Exception e) { Log.Exception("Invalid HTTPU datagram", e); } return(true); }
internal virtual bool OnAsyncResultReceived(AsyncReceiveBuffer result) { return(false); }
void AsyncReadResult(AsyncReceiveBuffer buffer) { buffer.Socket.BeginReceiveFrom(buffer, OnAsyncResultReceived); }
private static void DoReceived(RpcConnection connection, AsyncReceiveBuffer asyncReceiveBuffer, bool calledSynchronously = false) { if (!(connection?.Disposed ?? true)) { var bytesReceived = asyncReceiveBuffer.Count; if (bytesReceived > 0) { var buffer = asyncReceiveBuffer.Buffer; connection.ProcessReceived(buffer, bytesReceived); if (connection.Disposed) { return; } if (Common.IsWinPlatform) { int available; var socket = connection.Connection; var bufferLen = asyncReceiveBuffer.Length; while ((available = Math.Min(bufferLen, socket.Available)) > 0) { calledSynchronously = true; bytesReceived = socket.Receive(buffer, 0, available, SocketFlags.None); if (bytesReceived > 0) { connection.ProcessReceived(buffer, bytesReceived); if (connection.Disposed) { return; } } if (asyncReceiveBuffer.SynchronousCompletion() > SynchronousCompletionTreshold) { break; } } } if (calledSynchronously && asyncReceiveBuffer.SynchronousCompletion() > SynchronousCompletionTreshold) { asyncReceiveBuffer.ResetSynchronousCompletion(); ThreadPool.QueueUserWorkItem((waitCallback) => { Interlocked.Exchange(ref connection._inReceiveCycle, 0L); connection.BeginReceive(asyncReceiveBuffer); }); return; } if (!calledSynchronously) { asyncReceiveBuffer.ResetSynchronousCompletion(); } Interlocked.Exchange(ref connection._inReceiveCycle, 0L); connection.BeginReceive(asyncReceiveBuffer); return; } connection.Close(); } }