Example #1
0
        private void Loop()
        {
            Log("Starting Slave Network Node. Thead ID: " + Thread.CurrentThread.ManagedThreadId);

            while (true)
            {
                try
                {
                    if (m_Exit || !m_Client.Connected)
                    {
                        break;
                    }

                    int av = 0;

                    lock ( m_Client )
                    {
                        av = m_Client.Available;
                    }

                    if (av >= 1)
                    {
                        lock ( m_Client )
                        {
                            NetworkNodeRequestType type = ( NetworkNodeRequestType )m_Client.GetStream().ReadByte();
                            byte[] buf;

                            switch (type)
                            {
                            case NetworkNodeRequestType.SEND:
                            {
                                buf = new byte[sizeof(int)];
                                m_Client.GetStream().Read(buf, 0, buf.Length);
                                int len = BitConverter.ToInt32(buf, 0);
                                buf = new byte[len];
                                m_Client.GetStream().Read(buf, 0, buf.Length);
                                NetworkPacket p = NetworkPacket.Deserialize(buf);
                                Send(p);

                                break;
                            }
                            }
                        }
                    }
                    else
                    {
                        Thread.Sleep(NetworkSettings.NetworkNodeThreadSleep);
                    }
                }
                catch (Exception)
                {
                    break;
                }
            }

            Log("Stopping Slave Network Node. Thead ID: " + Thread.CurrentThread.ManagedThreadId);
            m_Exit = false;
            m_Client.Close();
        }
Example #2
0
        private void Loop()
        {
            Log("Starting Network Tunnel. Thead ID: " + Thread.CurrentThread.ManagedThreadId);

            while (true)
            {
                try
                {
                    if (m_Exit || !m_Client.Connected)
                    {
                        break;
                    }

                    if (m_Client.Available >= 1)
                    {
                        NetworkNodeRequestType type = ( NetworkNodeRequestType )m_Client.GetStream().ReadByte();
                        byte[] buf;

                        switch (type)
                        {
                        case NetworkNodeRequestType.DISCONNECT:
                        {
                            buf = new byte[sizeof(int)];
                            m_Client.GetStream().Read(buf, 0, buf.Length);
                            int addr = BitConverter.ToInt32(buf, 0);
                            m_Node.Disconnect(addr);
                            m_RemoteAdapters.TryRemove(addr, out object a);

                            break;
                        }

                        case NetworkNodeRequestType.CONNECT:
                        {
                            int addr = m_Node.RegisterAddress();
                            m_RemoteAdapters.TryAdd(addr, null);

                            buf = BitConverter.GetBytes(addr);
                            m_Client.GetStream().Write(buf, 0, buf.Length);

                            break;
                        }

                        case NetworkNodeRequestType.SEND:
                        {
                            buf = new byte[sizeof(int)];
                            m_Client.GetStream().Read(buf, 0, buf.Length);
                            int len = BitConverter.ToInt32(buf, 0);
                            buf = new byte[len];
                            m_Client.GetStream().Read(buf, 0, buf.Length);
                            m_Node.Send(NetworkPacket.Deserialize(buf));

                            break;
                        }
                        }
                    }
                    else
                    {
                        Thread.Sleep(NetworkSettings.NetworkTunnelThreadSleep);
                    }
                }
                catch (Exception)
                {
                    break;
                }
            }

            Log("Stopping Network Tunnel. Thead ID: " + Thread.CurrentThread.ManagedThreadId);
            m_Exit = false;
            m_Client.Close();
        }