Example #1
0
        public IAsyncResult BeginProcess(ATFrame frame, bool waitResponse, int timeout, AsyncCallback callback, Object state)
        {
            lock (this.syncRoot)
            {
                lock (this.waitingLock)
                {
                    this.waitingEcho        = this.echoEnabled;
                    this.waitingResponse    = waitResponse;
                    this.waitingTimeout     = Environment.TickCount + timeout;
                    this.waitingCommand     = frame.Command;
                    this.waitingCommandType = frame.CommandType;

                    this.waitingCallback               = callback;
                    this.waitingAsyncResult            = WaitingAsyncResult.GetInstance();
                    this.waitingAsyncResult.AsyncState = state;
                }

                try
                {
                    this.sendingLength = frame.GetBytes(this.sendingBuffer, 0);

                    this.stream.Write(this.sendingBuffer, 0, this.sendingLength);

#if (DEBUG)
                    Debug.Print("Sent frame > ");
                    Debug.Print(new string(ATParser.Bytes2Chars(this.sendingBuffer, 0, this.sendingLength)));
#endif


                    return(this.waitingAsyncResult);
                }
                catch
                {
                    lock (this.waitingLock)
                    {
                        this.waitingEcho     = false;
                        this.waitingResponse = false;
                    }

                    throw new ATModemException(ATModemError.Generic);
                }
            }
        }
Example #2
0
        public ATFrame Process(ATFrame frame, bool waitResponse, int timeout, AsyncCallback callback)
        {
            lock (this.syncRoot)
            {
                if (waitResponse)
                {
                    lock (this.waitingLock)
                    {
                        this.waitingEcho        = this.echoEnabled;
                        this.waitingResponse    = true;
                        this.waitingTimeout     = Environment.TickCount + timeout;
                        this.waitingCommand     = frame.Command;
                        this.waitingCommandType = frame.CommandType;

                        this.waitingCallback = null;

                        this.waitingEchoEvent.Reset();
                        this.waitingResponseEvent.Reset();
                    }
                }

                try
                {
                    this.sendingLength = frame.GetBytes(this.sendingBuffer, 0);

                    this.stream.Write(this.sendingBuffer, 0, this.sendingLength);

#if (DEBUG)
                    Debug.Print("Sent frame > ");
                    Debug.Print(new string(ATParser.Bytes2Chars(this.sendingBuffer, 0, this.sendingLength)));
#endif


                    if (this.waitingEcho && !this.waitingEchoEvent.WaitOne(defaultEchoTimeout, true))
                    {
                        throw new ATModemException(ATModemError.Timeout);
                    }


                    if (!waitResponse)
                    {
                        return(null);
                    }

                    if (this.waitingResponseEvent.WaitOne(timeout, true))
                    {
                        return(this.waitingFrame);
                    }

                    throw new ATModemException(ATModemError.Timeout);
                }
                finally
                {
                    if (waitResponse)
                    {
                        lock (this.waitingLock)
                        {
                            this.waitingEcho     = false;
                            this.waitingResponse = false;
                        }
                    }
                }
            }
        }