Exemplo n.º 1
0
        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()); //连接断开
            }
        }
Exemplo n.º 2
0
 public void ConnectOpened(IoState state)
 {
     lock (mIoStates)
     {
         mIoStates.Add(state.GetSID(), state);
     }
     mHeader.ConnectOpened(state);
 }
Exemplo n.º 3
0
        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();
        }
Exemplo n.º 4
0
 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);
     }
 }
Exemplo n.º 5
0
 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);
         }
     }
 }
Exemplo n.º 6
0
 public void MessageSent(IoState state, byte[] message)
 {
     mHeader.MessageSent(state, message);
 }
Exemplo n.º 7
0
 public void MessageReceived(IoState state, byte[] message)
 {
     mHeader.MessageReceived(state, message);
 }
Exemplo n.º 8
0
 public void ConnectOpened(IoState state)
 {
     throw new NotImplementedException();
 }