Beispiel #1
0
 public bool TryReadPacket(out NetworkPacket packet)
 {
     return(m_PacketQueue.TryDequeue(out packet));
 }
Beispiel #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();
        }
Beispiel #3
0
 public bool TryPeekPacket(out NetworkPacket packet)
 {
     return(m_PacketQueue.TryPeek(out packet));
 }
Beispiel #4
0
        public override void WriteData(uint address, uint data)
        {
            if (address == m_ArgumentPin)
            {
                m_ArgumentStack.Push(data);
            }
            else if (address == m_CommandPin)
            {
                Command cmd = ( Command )data;

                switch (cmd)
                {
                case Command.CONNECT:
                    m_Adapter.Connect();

                    break;

                case Command.DISCONNECT:
                    m_Adapter.Disconnect();

                    break;

                case Command.OPEN_PORT:
                    m_Adapter.OpenPort(( int )m_ArgumentStack.Pop());

                    break;

                case Command.CLOSE_PORT:
                    m_Adapter.ClosePort(( int )m_ArgumentStack.Pop());

                    break;

                case Command.SEND:
                    SendCommand();

                    break;

                case Command.HAS_PACKET:
                    uint ret = ( uint )m_Adapter.GetListener(( int )m_ArgumentStack.Pop()).PacketCount;
                    m_ReturnQueue.Enqueue(ret);

                    break;

                case Command.GET_PACKET_SIZE:
                {
                    NetworkPacket p = m_Adapter.GetListener(( int )m_ArgumentStack.Pop()).PeekPacket();

                    m_ReturnQueue.Enqueue(( uint )p.Data.Length);

                    break;
                }

                case Command.GET_PACKET_DATA:
                    ReadCommand();

                    break;

                case Command.CONSUME_PACKET:
                {
                    m_Adapter.GetListener(( int )m_ArgumentStack.Pop()).ReadPacket();

                    break;
                }

                case Command.GET_SENDER_ADDR:
                {
                    NetworkPacket p = m_Adapter.GetListener(( int )m_ArgumentStack.Pop()).PeekPacket();
                    m_ReturnQueue.Enqueue(( uint )p.Sender.NetworkAddress);

                    break;
                }

                case Command.GET_SENDER_PORT:
                {
                    NetworkPacket p = m_Adapter.GetListener(( int )m_ArgumentStack.Pop()).PeekPacket();
                    m_ReturnQueue.Enqueue(( uint )p.Sender.Port);

                    break;
                }

                case Command.GET_ADDR:
                    m_ReturnQueue.Enqueue(( uint )m_Adapter.NetworkAddress);

                    break;

                case Command.IS_CONNECTED:
                    m_ReturnQueue.Enqueue(m_Adapter.IsConnected ? 1u : 0u);

                    break;

                case Command.IS_OPEN_PORT:
                    m_ReturnQueue.Enqueue(m_Adapter.IsPortOpen(( int )m_ArgumentStack.Pop()) ? 1u : 0u);

                    break;

                case Command.GET_GUID:
                    GetGuid();

                    break;
                }
            }
        }