private bool session() { Package result; Package stateResult; while (_isALive) { int start = Environment.TickCount; while (Environment.TickCount - start < 1500) { if (_socket.Available != 0) { if (tryReceive(out result)) { Received.Raise(this, new ReceiveEventArgs(result)); } } Thread.Sleep(100); } if (trySendAndReceive(new Package(3, Convert.ToByte('h'), null), out stateResult)) { if (BitConverter.ToInt16(stateResult.Data.ToArray(), 0) != _state) { StateChanged.Raise(this, new StateEventArgs(stateResult.Data)); _state = (ushort)BitConverter.ToInt16(stateResult.Data.ToArray(), 0); } } } return(true); }
private void OnReceiveAsync(IAsyncResult asyncResult) { var state = (ReceiveState)asyncResult.AsyncState; //var size = state.Socket.SafeEndReceive(asyncResult); var size = stream.EndRead(asyncResult, Error.Raise); if (size < 1) { // No data was received Dispose(); } var frame = state.Frame; frame.Add(state.Buffer); if (frame.IsClosed) { Received.Raise(frame.ToContentString()); // Begin receiving data for a new frame ReceiveAsync(); } else { // Receive more data for the current frame var nextState = new ReceiveState { //Socket = state.Socket, Frame = state.Frame }; ReceiveAsync(nextState); } }