private void StartReceiveData(ReceivedState receivedState) { _processPreviousReceive.Wait(); _processPreviousReceive.Reset(); receivedState.NetSocket.BeginReceive(receivedState.Buffer, 0, ReceivedState.BUFFER_SIZE, 0, new AsyncCallback(ReceiveCallback), receivedState); }
public async Task StartAsync(ReceivedState startState) { var currentState = startState as IState; while (!currentState.IsFinishState) { currentState = await currentState.GetNextStateAsync(); } }
private void ReceiveCallback(IAsyncResult receivedSocket) { ReceivedState receivedState = (ReceivedState)receivedSocket.AsyncState; try { int dataLength = receivedState.NetSocket.EndReceive(receivedSocket); if (dataLength > 0) { receivedState.HandleData(); if (receivedState.WaitForData) { StartReceiveData(receivedState); } else { StartReceiveData(new ReceivedState(receivedState.NetSocket)); } } else { if (_sendingToSocket.ContainsKey(receivedState.NetSocket)) { ManualResetEventSlim mres; _sendingToSocket.TryRemove(receivedState.NetSocket, out mres); } receivedState.NetSocket.Shutdown(SocketShutdown.Both); Console.WriteLine("[Server] Connection with {0} closed", receivedState.NetSocket.RemoteEndPoint.ToString()); receivedState.NetSocket.Close(); } } catch (ArgumentNullException ae) { Console.WriteLine("[Server] ArgumentNullException : {0}", ae.ToString()); } catch (SocketException se) { if (_sendingToSocket.ContainsKey(receivedState.NetSocket)) { ManualResetEventSlim mres; _sendingToSocket.TryRemove(receivedState.NetSocket, out mres); } UserSocketException?.Invoke(receivedState.NetSocket); Console.WriteLine("[Server] SocketException : {0}", se.ToString()); } catch (Exception e) { Console.WriteLine("[Server] Unexpected exception : {0}", e.ToString()); } }
public async Task SetEventState(Guid id, ReceivedState state) { var record = await DbSet.FindAsync(id); record.EventState = state; if (state == ReceivedState.ConsumeFailed) { record.FailCount += 1; } await UpdateAsync(record); }
private void ReceiveCallback(IAsyncResult receivedSocket) { ReceivedState receivedState = (ReceivedState)receivedSocket.AsyncState; try { receivedState.NetSocket.EndReceive(receivedSocket); receivedState.HandleData(); _processPreviousReceive.Set(); if (receivedState.WaitForData) { StartReceiveData(receivedState); } else { StartReceiveData(new ReceivedState(receivedState.NetSocket)); } } catch (ArgumentNullException ae) { Debug.LogError(String.Format("ArgumentNullException : {0}", ae.ToString())); } catch (SocketException se) { _processPreviousReceive.Set(); ConnectionLost(); Debug.LogWarning(String.Format("SocketException : {0}", se.ToString())); } catch (Exception e) { Debug.LogError(String.Format("Unexpected exception : {0}", e.ToString())); } }
private void StartReceiveData(ReceivedState receivedState) { receivedState.NetSocket.BeginReceive(receivedState.Buffer, 0, ReceivedState.BUFFER_SIZE, 0, new AsyncCallback(ReceiveCallback), receivedState); }