コード例 #1
0
    private void TCPConnection_ErrorReceived(object sender, ZusiTcpException ex) // Handles MyTCPConnection.ErrorReceived
    {
      // If something goes wrong...
      // ... show the user what the connection object has to say.
      System.Windows.Forms.MessageBox.Show(String.Format("An error occured when receiving data: {0}", ex.Message));

      // ... reset the connection by explicitly calling Disconnect()
      MyTCPConnection.Disconnnect();

      //... and then change the button label to "Connect". 
      BtnConnect.Text = "Connect";
    }
コード例 #2
0
        private void ReceiveLoop()
        {
            var dataHandlers = new Dictionary <string, MethodInfo>();

            try
            {
                while (ConnectionState == ConnectionState.Connected)
                {
                    int packetLength = _clientReader.ReadInt32();

                    int curInstr = GetInstruction(_clientReader.ReadByte(), _clientReader.ReadByte());

                    if (curInstr < 10)
                    {
                        throw new ZusiTcpException("Unexpected Non-DATA instruction received.");
                    }

                    // The first 2 bytes have been the instruction.
                    int bytesRead = 2;

                    while (bytesRead < packetLength)
                    {
                        int curID = _clientReader.ReadByte() + 256 * curInstr;
                        // Another byte read for the ID.
                        bytesRead++;

                        CommandEntry curCommand = _commands[curID];

                        MethodInfo handlerMethod;

                        if (!dataHandlers.TryGetValue(curCommand.Type, out handlerMethod))
                        {
                            handlerMethod = GetType().GetMethod(
                                String.Format("HandleDATA_{0}", curCommand.Type),
                                BindingFlags.Instance | BindingFlags.NonPublic,
                                null,
                                new[] { typeof(BinaryReader), typeof(int) },
                                null);

                            if (handlerMethod == null)
                            {
                                throw new ZusiTcpException(
                                          String.Format(
                                              "Unknown type {0} for DATA ID {1} (\"{2}\") occured.", curCommand.Type, curID, curCommand.Name));
                            }

                            /* Make sure the handler method returns an int. */
                            Debug.Assert(handlerMethod.ReturnType == typeof(int));

                            dataHandlers.Add(curCommand.Type, handlerMethod);
                        }

                        bytesRead += (int)handlerMethod.Invoke(this, new object[] { _clientReader, curID });

                        Debug.Assert(bytesRead <= packetLength);
                    }
                }
            }
            catch (Exception e)
            {
                if (e is ZusiTcpException)
                {
                    _hostContext.Post(ErrorMarshal, e as ZusiTcpException);
                }
                else if (e is EndOfStreamException)
                {
                    /* EndOfStream occurs when the NetworkStream reaches its end while the binaryReader tries to read from it.
                     * This happens when the socket closes the stream.
                     */
                    var newEx = new ZusiTcpException("Connection to the TCP server has been lost.", e);
                    _hostContext.Post(ErrorMarshal, newEx);
                }
                else if (e is ThreadAbortException)
                {
                    /* Thread has been killed: Nothing to do. */
                    return;
                }
                else
                {
                    var newEx =
                        new ZusiTcpException(
                            "An unhandled exception has occured in the TCP receiving loop. This is very probably " +
                            "a bug in the Zusi TCP interface for .NET. Please report this error to the author(s) " +
                            "of this application and/or the author(s) of the Zusi TCP interface for .NET.", e);

                    _hostContext.Post(ErrorMarshal, newEx);
                }

                Disconnnect();
                ConnectionState = ConnectionState.Error;
            }
        }
コード例 #3
0
ファイル: CMainWindow.cs プロジェクト: zusitools/zusimeter
        private void TCPConnection_ErrorReceived(object sender, ZusiTcpException ex)
        {
            System.Windows.Forms.MessageBox.Show(String.Format("An error occured when receiving data: {0}", ex.Message));

            MyTCPConnection.Disconnnect();
            btnConnect.Text = "Connect";
        }