Beispiel #1
0
        public override NetEventType PollEvent(out ulong clientId, out string channelName, out ArraySegment <byte> payload)
        {
            socket.RunInternalLoop();
            NetworkEvent @event = socket.Poll();

            if (@event.Type != NetworkEventType.Nothing)
            {
                clientId = GetMLAPIClientId(@event.Connection.Id, false);
            }
            else
            {
                clientId = 0;
            }

            byte[] dataBuffer = messageBuffer;

            if (@event.Type == NetworkEventType.Data)
            {
                if (@event.Data.Count > messageBuffer.Length)
                {
                    if (temporaryBufferReference != null && temporaryBufferReference.IsAlive && ((byte[])temporaryBufferReference.Target).Length >= @event.Data.Count)
                    {
                        dataBuffer = (byte[])temporaryBufferReference.Target;
                    }
                    else
                    {
                        dataBuffer = new byte[@event.Data.Count];
                        temporaryBufferReference = new WeakReference(dataBuffer);
                    }
                }

                Buffer.BlockCopy(@event.Data.Array, @event.Data.Offset, dataBuffer, 0, @event.Data.Count);
                payload = new ArraySegment <byte>(dataBuffer, 0, @event.Data.Count);
            }
            else
            {
                payload = new ArraySegment <byte>();
            }

            channelName = channelIdToName[@event.ChannelId];

            // Translate NetworkEventType to NetEventType
            switch (@event.Type)
            {
            case NetworkEventType.Data:
                return(NetEventType.Data);

            case NetworkEventType.Connect:
            {
                connections.Add(@event.Connection.Id, @event.Connection);

                // Set the server connectionId
                if (isConnector)
                {
                    serverConnection = @event.Connection;
                }

                return(NetEventType.Connect);
            }

            case NetworkEventType.Timeout:
            case NetworkEventType.Disconnect:
            {
                if (@event.Connection == serverConnection)
                {
                    serverConnection = null;
                }

                connections.Remove(@event.Connection.Id);

                @event.Connection.Recycle();

                return(NetEventType.Disconnect);
            }

            case NetworkEventType.Nothing:
                return(NetEventType.Nothing);
            }

            return(NetEventType.Nothing);
        }
Beispiel #2
0
        private static void NoRufflesManager()
        {
            RuffleSocket server = new RuffleSocket(ServerConfig);

            RuffleSocket client = new RuffleSocket(ClientConfig);

            // IPv4 Connect
            //client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5674));

            // IPv6 Connect
            client.Connect(new IPEndPoint(IPAddress.Parse("0:0:0:0:0:0:0:1"), 5674));

            // The server stores the clients id here
            ulong clientId = 0;
            // The client stores the servers id here
            ulong serverId = 0;

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

            // The time when the last message was sent
            DateTime lastSent = 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)
            {
                // Runs all the internals
                client.RunInternalLoop();
                // Runs all the internals
                server.RunInternalLoop();

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


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

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

                    if (serverEvent.Type == NetworkEventType.Disconnect || serverEvent.Type == NetworkEventType.Timeout)
                    {
                        serverEvent.Connection.Recycle();
                    }
                }

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

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

                    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 (clientEvent.Type == NetworkEventType.Disconnect || clientEvent.Type == NetworkEventType.Timeout)
                    {
                        clientEvent.Connection.Recycle();
                    }
                }

                if ((DateTime.Now - started).TotalSeconds > 10 && (DateTime.Now - lastSent).TotalSeconds >= 1)
                {
                    byte[] helloReliable          = Encoding.ASCII.GetBytes("This message was sent over a reliable channel" + messageCounter);
                    byte[] helloReliableSequenced = Encoding.ASCII.GetBytes("This message was sent over a reliable sequenced channel" + messageCounter);

                    server.Send(new ArraySegment <byte>(helloReliableSequenced, 0, helloReliableSequenced.Length), clientId, 0, false);
                    server.Send(new ArraySegment <byte>(helloReliable, 0, helloReliable.Length), clientId, 1, false);

                    messageCounter++;
                    lastSent = DateTime.Now;
                }
            }
        }