Exemplo n.º 1
0
        public override SocketTasks StartClient()
        {
            SocketConfig config = GetConfig(false, NetworkingManager.Singleton.NetworkConfig);

            socket = new RuffleSocket(config);

            isConnector = true;

            if (!socket.Start())
            {
                return(SocketTask.Fault.AsTasks());
            }

            serverConnection = socket.Connect(new IPEndPoint(IPAddress.Parse(ConnectAddress), Port));

            if (serverConnection == null)
            {
                return(SocketTask.Fault.AsTasks());
            }
            else
            {
                connectTask = SocketTask.Working;

                return(connectTask.AsTasks());
            }
        }
Exemplo n.º 2
0
        public bool Connect(IPEndPoint endPoint)
        {
            socket = new RuffleSocket(new SocketConfig
            {
                ChallengeDifficulty = 20,
                ChannelTypes        = new[]
                {
                    ChannelType.ReliableSequencedFragmented
                },
                DualListenPort = 0
            });

            if (!socket.Start())
            {
                return(false);
            }

            var con = socket.Connect(endPoint);

            if (con != null)
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 3
0
        public static void Server()
        {
            RuffleSocket socket = new RuffleSocket(new Configuration.SocketConfig()
            {
                AllowBroadcasts          = true,
                AllowUnconnectedMessages = true,
                DualListenPort           = 5555
            });

            socket.Start();

            while (true)
            {
                // Wait for message. This is to prevent a tight loop
                socket.SyncronizationEvent.WaitOne(1000);

                NetworkEvent @event;
                while ((@event = socket.Poll()).Type != NetworkEventType.Nothing)
                {
                    if (@event.Type == NetworkEventType.BroadcastData)
                    {
                        // We got a broadcast. Reply to them with the same token they used
                        socket.SendUnconnected(@event.Data, (IPEndPoint)@event.EndPoint);
                    }

                    // Recycle the event
                    @event.Recycle();
                }
            }
        }
Exemplo n.º 4
0
        public override bool StartServer()
        {
            SocketConfig config = GetConfig(true);

            socket = new RuffleSocket(config);

            serverConnection = null;
            isConnector      = false;

            return(socket.Start());
        }
Exemplo n.º 5
0
        public Task Run()
        {
            _server = new RuffleSocket(_serverConfig);
            _server.Start();

            return(Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    Update();
                }
            }, TaskCreationOptions.LongRunning));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Binds the UDP socket to the specified local endpoint.
        /// </summary>
        /// <param name="endpoint">The local endpoint to bind to.</param>
        public void Bind(IPEndPoint endpoint)
        {
            if (socket != null)
            {
                return;
            }
            // Setup the socket info here
            socket = new RuffleSocket(new SocketConfig()
            {
                AllowBroadcasts          = true, //necessary ?
                AllowUnconnectedMessages = true, //necessary ?
                DualListenPort           = endpoint.Port,
                IPv4ListenAddress        = endpoint.Address
            });

            socket.Start();
        }
Exemplo n.º 7
0
        public override SocketTasks StartServer()
        {
            SocketConfig config = GetConfig(true, NetworkingManager.Singleton.NetworkConfig);

            socket = new RuffleSocket(config);

            serverConnection = null;
            isConnector      = false;

            if (socket.Start())
            {
                return(SocketTask.Done.AsTasks());
            }
            else
            {
                return(SocketTask.Fault.AsTasks());
            }
        }
Exemplo n.º 8
0
        public bool Listen(int port)
        {
            socket = new RuffleSocket(new SocketConfig
            {
                ChallengeDifficulty = 20,
                IPv4ListenAddress   = IPAddress.Parse("0.0.0.0"),
                ChannelTypes        = new[]
                {
                    ChannelType.ReliableSequencedFragmented
                },
                DualListenPort = port,
            });

            var r = socket.Start();

            m_Address = new RuffleTransportAddress {
                EndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port)
            };
            return(r);
        }
Exemplo n.º 9
0
        public override bool StartClient()
        {
            SocketConfig config = GetConfig(false);

            socket = new RuffleSocket(config);

            isConnector = true;

            if (!socket.Start())
            {
                return(false);
            }

            serverConnection = socket.Connect(new IPEndPoint(IPAddress.Parse(ConnectAddress), Port));

            if (serverConnection == null)
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 10
0
        public void Run()
        {
            var platform = new DesktopPlatform();

            WindowCreateInfo wci = new WindowCreateInfo
            {
                X = 100,
                Y = 100,
                WindowWidth = 1280,
                WindowHeight = 720,
                WindowTitle = "Tortuga Demo"
            };

            GraphicsDeviceOptions options = new GraphicsDeviceOptions(
                debug: false,
                swapchainDepthFormat: PixelFormat.R16_UNorm,
                syncToVerticalBlank: true,
                resourceBindingModel: ResourceBindingModel.Improved,
                preferDepthRangeZeroToOne: true,
                preferStandardClipSpaceYDirection: true);
            #if DEBUG
            options.Debug = true;
            #endif

            _window = platform.CreateWindow(wci, options, GraphicsBackend.Vulkan) as DesktopWindow;
            _window.GraphicsDeviceCreated += LoadResources;
            _window.Tick += Update;
            _window.Resized += _window_Resized;

            _viewport = new ViewportManager(1280, 720);

            _cameraSpaceInputTracker = new TransformedInputTracker(_window.InputTracker);
            _cameraSpaceGameInputTracker = new ActiveInputTracker(_cameraSpaceInputTracker);

            _client.Start();
            _client.Connect(new IPEndPoint(IPAddress.Loopback, 5674));

            _window.Run();
        }
Exemplo n.º 11
0
        public static void Main(string[] args)
        {
            RuffleSocket server = new RuffleSocket(ServerConfig);

            RuffleSocket client = new RuffleSocket(ClientConfig);

            client.Start();
            server.Start();

            if (IPv6)
            {
                // IPv6 Connect
                client.Connect(new IPEndPoint(IPAddress.Parse("0:0:0:0:0:0:0:1"), 5674));
            }
            else
            {
                // IPv4 Connect
                client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5674));
            }

            // The server stores the clients id here
            Connection clientConnection = null;
            // The client stores the servers id here
            Connection serverConnection = null;

            // The time when the connection started
            DateTime started = DateTime.Now;

            // The time when the last message was sent
            DateTime lastSent = DateTime.MinValue;

            // The time the last status was printed
            DateTime lastStatusPrint = DateTime.MinValue;

            // The amount of message that has been received
            int messagesReceived = 0;

            // The amount of messages that has been sent
            int messageCounter = 0;

            while (true)
            {
                // Polls server for events
                NetworkEvent serverEvent = server.Poll();
                // Polls client for events
                NetworkEvent clientEvent = client.Poll();

                if (serverEvent.Type != NetworkEventType.Nothing)
                {
                    Console.WriteLine("ServerEvent: " + serverEvent.Type);

                    if (serverEvent.Type == NetworkEventType.Connect)
                    {
                        clientConnection = serverEvent.Connection;
                    }

                    if (serverEvent.Type == NetworkEventType.AckNotification)
                    {
                        Console.WriteLine("The remote acked message id: " + serverEvent.NotificationKey);
                    }
                }

                serverEvent.Recycle();

                if (clientEvent.Type != NetworkEventType.Nothing)
                {
                    Console.WriteLine("ClientEvent: " + clientEvent.Type);

                    if (clientEvent.Type == NetworkEventType.Connect)
                    {
                        serverConnection = clientEvent.Connection;
                    }

                    if (clientEvent.Type == NetworkEventType.Data)
                    {
                        messagesReceived++;
                        Console.WriteLine("Got message: \"" + Encoding.ASCII.GetString(clientEvent.Data.Array, clientEvent.Data.Offset, clientEvent.Data.Count) + "\"");
                    }
                }

                clientEvent.Recycle();

                if (serverConnection != null && clientConnection != null && serverConnection.State == ConnectionState.Connected && clientConnection.State == ConnectionState.Connected && (DateTime.Now - lastSent).TotalSeconds >= (1f / 1))
                {
                    byte[] helloReliable = Encoding.ASCII.GetBytes("This message was sent over a reliable channel" + messageCounter);
                    clientConnection.Send(new ArraySegment <byte>(helloReliable, 0, helloReliable.Length), 1, false, (ulong)messageCounter);
                    Console.WriteLine("Sending packet: " + messageCounter);

                    messageCounter++;
                    lastSent = DateTime.Now;
                }

                if (serverConnection != null && clientConnection != null && serverConnection.State == ConnectionState.Connected && clientConnection.State == ConnectionState.Connected && (DateTime.Now - lastStatusPrint).TotalSeconds >= 5)
                {
                    Console.WriteLine("Ping: " + serverConnection.SmoothRoundtrip + "ms, " + clientConnection.SmoothRoundtrip + "ms");
                    lastStatusPrint = DateTime.Now;
                }
            }
        }
Exemplo n.º 12
0
        public static void Client()
        {
            RuffleSocket socket = new RuffleSocket(new Configuration.SocketConfig()
            {
                AllowBroadcasts          = true,
                AllowUnconnectedMessages = true,
                DualListenPort           = 0
            });

            socket.Start();

            // Wait for message. This is to prevent a tight loop
            socket.SyncronizationEvent.WaitOne(1000);

            // Create RNG
            System.Random rnd = new System.Random();
            // Alloc token
            byte[] token = new byte[32];
            // Fill buffer with random data
            rnd.NextBytes(token);

            // Save last send time
            DateTime lastBroadcastSendTime = DateTime.MinValue;

            while (true)
            {
                // If we havent sent broadcast for 5 seconds
                if ((DateTime.Now - lastBroadcastSendTime).TotalSeconds > 5)
                {
                    lastBroadcastSendTime = DateTime.Now;

                    // Send broadcast
                    socket.SendBroadcast(new ArraySegment <byte>(token), 5555);
                }

                // Wait for message. This is to prevent a tight loop
                socket.SyncronizationEvent.WaitOne(1000);

                NetworkEvent @event;
                while ((@event = socket.Poll()).Type != NetworkEventType.Nothing)
                {
                    if (@event.Type == NetworkEventType.UnconnectedData)
                    {
                        // We got a reply. Ensure the token is correct
                        if (@event.Data.Count == token.Length)
                        {
                            bool missMatch = false;

                            // The token had the same length. Check all elements
                            for (int i = 0; i < @event.Data.Count; i++)
                            {
                                if (@event.Data.Array[@event.Data.Offset + i] != token[i])
                                {
                                    // This element did not match. Exit
                                    missMatch = true;
                                    break;
                                }
                            }

                            if (missMatch)
                            {
                                // Continue the receive loop the loop
                                continue;
                            }
                            else
                            {
                                // All matched.
                                Console.WriteLine("Found server at endpoint: " + ((IPEndPoint)@event.EndPoint));
                            }
                        }
                    }

                    // Recycle the event
                    @event.Recycle();
                }
            }
        }