protected virtual void ReceivedDataEvent(ClientReceivedDataEvent Revent) { bool bEnd = false; lock (ReceiveDatLock) { foreach (byte theByte in Revent.RcvDat) { if (bEnd) { break; } switch (StxEtxAck) { case STX_SEARCH: if (theByte == 0x02) { ReceivedData = new MemoryStream(); StxEtxAck = ETX_SEARCH; } break; case ETX_SEARCH: if (theByte == 0x03) { //command is finished CommContext.LogMsg(TraceEventType.Verbose, "STXETX RCVD: <STX>" + System.Text.Encoding.Default.GetString(ReceivedData.ToArray()) + "<ETX>"); CommContext.Write(new byte[1] { 0x06 }); //send ACK CommContext.LogMsg(TraceEventType.Verbose, "STXETX SENT: <ACK>"); StxEtxAck = STX_SEARCH; //go back to looking for an STX //process the parsed command ProcessCommand(ReceivedData); //protocol only allows one command and then an ack, so toss the rest of the data bEnd = true; break; } else { try { ReceivedData.WriteByte(theByte); } catch (Exception ex) { CommContext.LogMsg(TraceEventType.Error, ex); //something is wrong with the memory stream, get rid of it and dump all data StxEtxAck = STX_SEARCH; } } break; case ACK_SEARCH: if (theByte == 0x06) { CommContext.LogMsg(TraceEventType.Verbose, "STXETX RCVD: <ACK>"); StxEtxAck = STX_SEARCH; if (SendingContext.AckFound != null) { try { SendingContext.AckFound.Set(); //release the thread that was writing } catch (Exception ex) { CommContext.LogMsg(TraceEventType.Error, ex); } } } break; } } } }
/// <summary> /// 数据接收事件 /// </summary> private void OnReceivedData() { Process(); ClientReceivedDataEvent?.Invoke(this); }
/// <summary> /// 触发数据接收事件 /// </summary> private void OnReceivedData(IMisakaConnection conn) { ClientReceivedDataEvent?.Invoke(conn); }
/// <summary> /// 数据接收时出发 /// </summary> private void OnReceivedData() { ClientReceivedDataEvent?.Invoke(this); }