private static void ReadCallBack(IAsyncResult iar) { IoState state = iar.AsyncState as IoState; int NumOfBytesRead = 0; try { NumOfBytesRead = state.EndRead(iar); if (NumOfBytesRead > 0) { NumOfBytesRead = state.ReadData(NumOfBytesRead); byte[] buffer = new byte[NumOfBytesRead]; Array.Copy(state.GetBuffer(), buffer, NumOfBytesRead); state.mHeader.MessageReceived(state, buffer); state.BeginRead(); } else { state.mHeader.ConnectClosed(state.GetPID(), state.GetSID()); //连接断开 } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); state.mHeader.ConnectClosed(state.GetPID(), state.GetSID()); //连接断开 } }
public void ConnectOpened(IoState state) { lock (mIoStates) { mIoStates.Add(state.GetSID(), state); } mHeader.ConnectOpened(state); }
private static void WriteCallBack(IAsyncResult iar) { IoState state = iar.AsyncState as IoState; state.mStream.EndWrite(iar); int writeCount = state.mWriteBuffer.Length; byte[] buffer = new byte[writeCount]; Array.Copy(state.mWriteBuffer, buffer, writeCount); state.mHeader.MessageSent(state, buffer); state.mCanWrite.Set(); }
private void DoConnectCallBack(IAsyncResult ar) { try { mClient.EndConnect(ar); mIoState = new IoClientState(this, mBufferSize, mClient); mIoState.SetStream(mClient.GetStream()); mHeader.SessionOpened(this); mIoState.BeginRead(); } catch (Exception ex) { mHeader.SessionException(this, ex); } }
public void Start() { if (!mSerialPort.IsOpen) { try { mSerialPort.Open(); mSerialPort.DiscardOutBuffer(); mSerialPort.DiscardInBuffer(); mHeader.SessionOpened(this); //串口打开消息 Stream stream = mSerialPort.BaseStream; stream.ReadTimeout = GetReadTimeout(); //设置读超时 mIoState = new IoSerialState(this, stream, mBufferSize, mSerialPort); mIoState.BeginRead(); } catch (Exception ex) { mHeader.SessionException(this, ex); } } }
public void MessageSent(IoState state, byte[] message) { mHeader.MessageSent(state, message); }
public void MessageReceived(IoState state, byte[] message) { mHeader.MessageReceived(state, message); }
public void ConnectOpened(IoState state) { throw new NotImplementedException(); }