/// <summary>
    /// Starts receiving.
    /// </summary>
    private void BeginReceive()
    {
      try
      {
        if (_pfnCallBack == null)
          _pfnCallBack = OnDataReceived;

        CSocketPacket socketPkt = new CSocketPacket();
        socketPkt.ThisSocket = _socket;

        _socket.BeginReceive(socketPkt.ReceiveBuffer, 0, socketPkt.ReceiveBuffer.Length, SocketFlags.None, _pfnCallBack,
                             socketPkt);
      }
#if TRACE
      catch (SocketException ex)
      {
        Trace.WriteLine(ex.ToString());
      }
#else
      catch (SocketException)
      {
      }
#endif
    }
Beispiel #2
0
        /// <summary>
        /// Called when data received.
        /// </summary>
        /// <param name="asyncResult">The async result.</param>
        private void OnDataReceived(IAsyncResult asyncResult)
        {
            try
            {
                CSocketPacket theSockId = (CSocketPacket)asyncResult.AsyncState;

                int bytesReceived = theSockId.ThisSocket.EndReceive(asyncResult);

                IntPtr ptrReceive = Marshal.AllocHGlobal(bytesReceived);
                Marshal.Copy(theSockId.ReceiveBuffer, 0, ptrReceive, bytesReceived);
                NETWORKRECV received = (NETWORKRECV)Marshal.PtrToStructure(ptrReceive, typeof(NETWORKRECV));

                /*
                 * Log.Info("IRTrans: Command Start --------------------------------------------");
                 * Log.Info("IRTrans: Client       = {0}", netrecv.clientid);
                 * Log.Info("IRTrans: Status       = {0}", (IrTransStatus)netrecv.statustype);
                 * Log.Info("IRTrans: Remote       = {0}", netrecv.remote);
                 * Log.Info("IRTrans: Command Num. = {0}", netrecv.command_num.ToString());
                 * Log.Info("IRTrans: Command      = {0}", netrecv.command);
                 * Log.Info("IRTrans: Data         = {0}", netrecv.data);
                 * Log.Info("IRTrans: Command End ----------------------------------------------");
                 */

                switch ((IrTransStatus)received.StatusType)
                {
                case IrTransStatus.STATUS_RECEIVE:
                    if (received.Remote.Trim().Equals(_irTransRemoteModel, StringComparison.OrdinalIgnoreCase))
                    {
                        try
                        {
                            string keyCode = received.Command.Trim();

                            if (_remoteButtonHandler != null)
                            {
                                _remoteButtonHandler(Name, keyCode);
                            }
                        }
#if TRACE
                        catch (Exception ex)
                        {
                            Trace.WriteLine(ex.ToString());
                        }
#else
                        catch
                        {
                        }
#endif
                    }
                    break;

                //case IrTransStatus.STATUS_LEARN:

                default:
                    break;
                }

                Marshal.FreeHGlobal(ptrReceive);
                BeginReceive();
            }
            catch (ObjectDisposedException)
            {
            }
#if TRACE
            catch (SocketException ex)
            {
                Trace.WriteLine(ex.ToString());
            }
#else
            catch (SocketException)
            {
            }
#endif
        }