Exemplo n.º 1
0
        public IAsyncResult BeginReceive(TimeSpan timeout, object state, AsyncCallback callback)
        {
            long totalMilliseconds = (long)timeout.TotalMilliseconds;

            if ((totalMilliseconds < 0L) || (totalMilliseconds > 4294967295L))
            {
                throw new ArgumentException("InvalidParameter", "timeout");
            }
            ReceiveMessageCallback caller = new ReceiveMessageCallback(ReceiveItemWorker);

            if (callback == null)
            {
                callback = CreateCallBack();
            }
            if (state == null)
            {
                state = new object();
            }
            state = (int)ReceiveState.Wait;

            // Initiate the asychronous call.  Include an AsyncCallback
            // delegate representing the callback method, and the data
            // needed to call EndInvoke.
            IAsyncResult result = caller.BeginInvoke(timeout, state, callback, caller);

            this.ResetEvent.Set();
            //OnReceiveCompleted(new ReceiveResultEventArgs(this, result));
            return(result);
        }
Exemplo n.º 2
0
        // Callback method must have the same signature as the
        // AsyncCallback delegate.
        public IQueueItem EndReceive(IAsyncResult asyncResult)
        {
            // Retrieve the delegate.
            ReceiveMessageCallback caller = (ReceiveMessageCallback)asyncResult.AsyncState;

            // Call EndInvoke to retrieve the results.
            IQueueItem item = (IQueueItem)caller.EndInvoke(asyncResult);

            AsyncCompleted(item);
            this.ResetEvent.WaitOne();
            return(item);
        }
Exemplo n.º 3
0
 private void Initialize(Control ctrl, ReceiveMessageCallback callback, string portName, int baudRate, int readTimeout, int writeTimeout)
 {
     serialPort = new SerialPort()
     {
         PortName     = portName,
         BaudRate     = baudRate,
         ReadTimeout  = readTimeout,
         WriteTimeout = writeTimeout
     };
     serialPort.DataReceived += new SerialDataReceivedEventHandler(ReceiveMessageHandler);
     control = ctrl;
     receiveMessageCallback = callback;
     ConsoleOutput("Initialized.");
 }
Exemplo n.º 4
0
        public IQueueItem AsyncReceive(object state)
        {
            if (state == null)
            {
                state = new object();
            }
            TimeSpan timeout = TimeSpan.FromMilliseconds(QueueApi.LongTimeout);
            ReceiveMessageCallback caller = new ReceiveMessageCallback(this.ReceiveItemWorker);

            // Initiate the asychronous call.
            IAsyncResult result = caller.BeginInvoke(timeout, state, CreateCallBack(), caller);

            result.AsyncWaitHandle.WaitOne();

            // Call EndInvoke to wait for the asynchronous call to complete,
            // and to retrieve the results.
            IQueueItem item = caller.EndInvoke(result);

            AsyncCompleted(item);
            return(item);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Connects to a SerialPort with the specified settings. Also initializes the class if not done already.
 /// </summary>
 /// <param name="ctrl">Reference to the control where the callback will be invoked.</param>
 /// <param name="callback">Callback to invoke after receiving messages.</param>
 /// <param name="portName">Name of COM-Port to connect to.</param>
 /// <param name="baudRate">Baudrate to use while communicating.</param>
 /// <param name="readTimeout">Timeout for reading messages.</param>
 /// <param name="writeTimeout">Timeout for sending messages.</param>
 /// <returns>Returns true, if connecting was successfull.</returns>
 public bool Connect(Control ctrl, ReceiveMessageCallback callback, string portName, int baudRate, int readTimeout, int writeTimeout)
 {
     try {
         if (serialPort == null)
         {
             Initialize(ctrl, callback, portName, baudRate, readTimeout, writeTimeout);
         }
         else if (serialPort.IsOpen)
         {
             ConsoleOutput("Already connected.");
         }
         else
         {
             serialPort.PortName = portName;
         }
         serialPort.Open();
         ConsoleOutput(String.Format("Connected to port {0}", portName));
         return(true);
     } catch {
         ConsoleOutput(String.Format("Unable to connect to port {0}", portName));
         return(false);
     }
 }
Exemplo n.º 6
0
        //Start Dinu!  TODO improve
        void ReceiveMessage(string username, string message, DateTime date)
        {
            if (Application.OpenForms[0].InvokeRequired)
            {
                ReceiveMessageCallback d = new ReceiveMessageCallback(ReceiveMessage);
                this.Invoke(d, new object[] { username, message, date });
            }
            else
            {
                if (username != null && username != "" && message != null && message != "")
                {
                    foreach (Form OpenForm in Application.OpenForms)
                    {

                        if (OpenForm is Conversation)
                        {
                            Conversation temp = (Conversation)OpenForm;
                            if (username == temp.id)
                            {
            //                                OpenForm.TopMost = true;
                                OpenForm.Focus();
                                temp.Receive_Msg_Auto(message, date);
                                //                                MessageBox.Show("Found");
                                return;
                            }
                        }
                    }

                    //TODO send maybe my name and my/other logo rethink
                    Conversation convFriend = new Conversation(_chatClient, username, null, username, "Tmp Status"); //TODO modify
                    convFriend.Show();
                    convFriend.Receive_Msg_Auto(message, date);
                    //                    MessageBox.Show("Created new");
                }
            }

            // Console.WriteLine("Received Message from: {0} \nMessage: {1}", username, message);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Connects to a SerialPort with the specified settings. Also initializes the class if not done already.
 /// </summary>
 /// <param name="ctrl">Reference to the control where the callback will be invoked.</param>
 /// <param name="callback">Callback to invoke after receiving messages.</param>
 /// <param name="portName">Name of COM-Port to connect to.</param>
 /// <returns>Returns true, if connecting was successfull.</returns>
 public bool Connect(Control ctrl, ReceiveMessageCallback callback, string portName)
 {
     return(Connect(ctrl, callback, portName, 9600, 2000, 500));
 }