Ejemplo n.º 1
0
        /// <summary>
        /// Tries to create a network connection to the given IP address
        /// </summary>
        /// <param name="ip">ip to connect to</param>
        /// <param name="port">port to connect to</param>
        public void Connect(string ip, int port)
        {
            // Connect to the ip and port
            m_client = new TcpClient();
            m_client.Connect(IPAddress.Parse(ip), port);

            // Read the welcome message, this will tell if we're connected to a little or big endian application:
            NetworkStream stream = m_client.GetStream();

            BinaryReader reader = new BinaryReader(stream);

            byte[] connectmsg = reader.ReadBytes(4);

            uint id = BitConverter.ToUInt32(connectmsg, 0);

            if (id == 0x44454247)               // 'DEBG'
            {
                m_reader    = new BinaryReader(stream);
                m_bigEndian = false;
            }
            else
            if (id == 0x47424544)               // 'GBED' --> Reverse of 'DEBG', so must be different endian-ness
            {
                m_reader    = new BinaryReaderEndianSwap(stream);
                m_bigEndian = true;
            }

            m_connected = true;

            m_connectedTo = ip;

            if (m_bigEndian)
            {
                m_writer = new BinaryWriterEndianSwap(stream);
            }
            else
            {
                m_writer = new BinaryWriter(stream);
            }

            // Start the network message thread:
            m_exitNetworkThread  = false;
            m_networkMessages    = new Queue <DebugMessage>();
            m_networkThread      = new Thread(new ThreadStart(NetworkMessageThread));
            m_networkThread.Name = "Network Thread";
            m_networkThread.Start();
        }
Ejemplo n.º 2
0
        private void NetworkMessageThread()
        {
            // Update the state:
            while (!m_exitNetworkThread)
            {
                // Read messages until the connection runs out of data
                while (m_client.Available > 8)
                {
                    // Read the network message:
                    try
                    {
                        uint type = m_reader.ReadUInt32();
                        int  size = m_reader.ReadInt32();

                        List <byte> bytesRead = new List <byte>();
                        while (bytesRead.Count < size)
                        {
                            if (m_client.Available > 0)
                            {
                                byte[] readbytes = m_reader.ReadBytes(Math.Min(m_client.Available, size - bytesRead.Count));
                                bytesRead.AddRange(readbytes);
                            }
                            else
                            {
                                Thread.Sleep(0);                                        // go asleep for a bit, waiting for the data to come in.
                            }
                        }

                        byte[] bytes = bytesRead.ToArray();

                        DebugMessage msg = DebugMessageFactory.CreateDebugMessage(type);                                // If we can't create this message yet, discard it
                        if (msg != null)
                        {
                            using (Stream s = new MemoryStream(bytes))
                            {
                                BinaryReader reader;
                                if (m_bigEndian)
                                {
                                    reader = new BinaryReaderEndianSwap(s);
                                }
                                else
                                {
                                    reader = new BinaryReader(s);
                                }

                                msg.Deserialize(reader);
                            }

                            // Add the network message to the queue of messages:
                            lock ( m_thisLock )
                            {
                                m_networkMessages.Enqueue(msg);
                            }
                        }
                    }
                    catch
                    {
                        // On any error, we disconnect:
                        Disconnect();
                    }
                }

                // Go to sleep for a bit:
                Thread.Sleep(10);
            }
        }