Exemplo n.º 1
0
 public RemoteGameClient(RemoteClient remoteClient, Server server)
 {
     reliableEndpoint = new ReliableEndpoint();
     reliableEndpoint.ReceiveCallback  += ReliableReceiveCallback;
     reliableEndpoint.TransmitCallback += ReliableTransmitCallback;
     this.remoteClient = remoteClient;
 }
Exemplo n.º 2
0
 public void ConnectAsync(string uri, string token, Action <bool> callback)
 {
     if (client == null)
     {
         createClient();
         client.OnStateChanged += (ClientState state) =>
         {
             if (state == ClientState.Connected)
             {
                 callback(true);
                 ThreadPool.QueueUserWorkItem(tick);
             }
             else if (state != ClientState.SendingConnectionRequest &&
                      state != ClientState.SendingChallengeResponse)
             {
                 if (OnError != null)
                 {
                     OnError(new SocketErrorEventArgs(new Exception("client timed out while connecting")));
                 }
                 isConnected  = false;
                 client       = null;
                 endpoint     = null;
                 connectedEvt = null;
             }
         };
         client.Connect(System.Convert.FromBase64String(token));
     }
 }
Exemplo n.º 3
0
    private void Server_OnClientConnected(RemoteClient client)
    {
        logLine("Client connected: " + client.RemoteEndpoint.ToString());
        clients++;
        NumClientsText.text = clients.ToString() + "/" + MaxClients.ToString();

        //**** ***** **** ****
        ReliableEndpoint reliableEndpoint = new ReliableEndpoint();

        reliableEndpoint.TransmitCallback = (payload, size) =>
        {
            client.SendPayload(payload, size);
        };
        reliableEndpoint.ReceiveCallback = (message, messageSize) =>
        {
            MessageHandler(client, message, messageSize);
        };
        endpointsByClient.Add(client, reliableEndpoint);
        //**** ** **** *** ****

        //NetworkSpawnPlayer(client);
        SPAWN_PLAYER.Send(client, client.ClientID, new Vector3(0, 0, 0), new Quaternion(0, 0, 0, 1));

        //if have 1 player -> start send position data to clients
        if (players.Count == 1)
        {
            StartCoroutine(updateSender());
        }
    }
Exemplo n.º 4
0
        private void ClientConnectedHandler(RemoteClient client)
        {
            Console.WriteLine($"clientConnectedHandler: {client}");
            ReliableEndpoint _reliableEndpoint = new ReliableEndpoint();

            _reliableEndpoint.ReceiveCallback += ReliableClientMessageReceived;
            _clients.TryAdd(client, _reliableEndpoint);
        }
Exemplo n.º 5
0
        private void OnConnectSuccess()
        {
            _client.QueryStatus(OnStatusChange);

            _reliableClient = new ReliableEndpoint();
            _reliableClient.ReceiveCallback  += OnReliableReceiveCallback;
            _reliableClient.TransmitCallback += OnReliableTransmitCallback;
            _client.AddPayloadListener(OnMessageReceive);
        }
Exemplo n.º 6
0
        public void TestIDWrapping()
        {
            List <byte> sentPackets     = new List <byte>();
            List <byte> receivedPackets = new List <byte>();

            ReliableEndpoint endpoint1 = new ReliableEndpoint();

            endpoint1.ReceiveCallback = (buffer, size) =>
            {
                if (buffer[0] == 0)
                {
                    receivedPackets.Add(buffer[1]);
                }
            };
            endpoint1.TransmitCallback = (buffer, size) =>
            {
                endpoint1.ReceivePacket(buffer, size);
            };

            for (int i = 0; i < 68000; i++)
            {
                sentPackets.Add((byte)i);
                byte[] test = new byte[256];
                test[0] = 0;
                test[1] = (byte)i;
                endpoint1.SendMessage(test, 256, QosType.Reliable);

                endpoint1.UpdateFastForward(0.1);
            }

            while (receivedPackets.Count < sentPackets.Count)
            {
                endpoint1.UpdateFastForward(1.0);
            }

            if (receivedPackets.Count == sentPackets.Count)
            {
                bool compare = true;
                for (int i = 0; i < receivedPackets.Count; i++)
                {
                    if (receivedPackets[i] != sentPackets[i])
                    {
                        compare = false;
                        break;
                    }
                }

                if (!compare)
                {
                    throw new System.Exception("Received packet contents differ!");
                }
            }
            else
            {
                throw new System.Exception((sentPackets.Count - receivedPackets.Count) + " packets not received");
            }
        }
Exemplo n.º 7
0
 public void Close()
 {
     if (client != null)
     {
         isConnected = false;
         client.Disconnect();
         client       = null;
         endpoint     = null;
         connectedEvt = null;
     }
 }
Exemplo n.º 8
0
 public void CloseAsync(Action callback)
 {
     if (client != null)
     {
         isConnected = false;
         client.Disconnect();
         client       = null;
         endpoint     = null;
         connectedEvt = null;
         callback();
     }
 }
Exemplo n.º 9
0
        public ReliableIOServerAdapter(IPAddress publicIP, ushort port) : base(publicIP, port)
        {
            Endpoint = new ReliableEndpoint
            {
                ReceiveCallback  = ReliableReceive,
                TransmitCallback = ReliableTransmit
            };

            Reliability = QosType.UnreliableOrdered;

            Server.OnClientMessageReceived -= ReceiveMessage;
            Server.OnClientMessageReceived += NetcodeReceiveMessage;
        }
Exemplo n.º 10
0
        public ReliableIOClientAdapter(IPAddress publicIP, ushort port) : base(publicIP, port)
        {
            Endpoint = new ReliableEndpoint
            {
                ReceiveCallback  = ReceiveMessage,
                TransmitCallback = ReliableTransmit
            };

            Reliability = QosType.UnreliableOrdered;

            Client.OnMessageReceived -= ReceiveMessage;
            Client.OnMessageReceived += Endpoint.ReceivePacket;
        }
Exemplo n.º 11
0
    private void connectToServer()
    {
        var factory = new TokenFactory(0x1122334455667788L, _privateKey);

        clientId = Random.Range(1, 100);
        string ip = "192.168.1.70";

        ip = "54.243.1.231";
        byte[] connectToken = factory.GenerateConnectToken(
            new IPEndPoint[]
        {
            new IPEndPoint(IPAddress.Parse(ip), 12345),
            new IPEndPoint(IPAddress.Parse("0.0.0.0"), 12345)
        },
            30,
            5,
            1UL,
            (ulong)clientId,
            new byte[256]);

        client.Connect(connectToken);
        client.OnStateChanged    += UpdateStatus;
        client.OnMessageReceived += (payload, payloadSize) =>
        {
            endpoint.ReceivePacket(payload, payloadSize);
        };

        endpoint = new ReliableEndpoint
        {
            ReceiveCallback = (buffer, size) =>
            {
                var packet = PacketGenerator.DecodePacket(buffer);
                if (packet.TickId > tick)
                {
                    processor.Process(packet);
                    tick = packet.TickId;
                }
                else
                {
                    print("Skip: " + packet);
                }
            },
            TransmitCallback = (payload, payloadSize) =>
            {
                client.Send(payload, payloadSize);
            }
        };
    }
Exemplo n.º 12
0
 public void Connect(string uri, string token)
 {
     if (client == null)
     {
         createClient();
         client.Connect(System.Convert.FromBase64String(token));
     }
     connectedEvt.WaitOne(10000);
     if (client.State != ClientState.Connected)
     {
         if (OnError != null)
         {
             OnError(new SocketErrorEventArgs(new Exception("client timed out while connecting")));
         }
         isConnected  = false;
         client       = null;
         endpoint     = null;
         connectedEvt = null;
         return;
     }
     ThreadPool.QueueUserWorkItem(tick);
 }
Exemplo n.º 13
0
    void ClientStateChanged(ClientState state)
    {
        Debug.Log("clientStateChanged: " + state.ToString());
        if (state == ClientState.Connected)
        {
            //Set up ReliableNetcode endpoint object and callbacks
            reliableEndpoint = new ReliableEndpoint();
            reliableEndpoint.ReceiveCallback  += ReliableReceiveCallback;
            reliableEndpoint.TransmitCallback += ReliableTransmitCallback;

            SendFindGame();
        }
        else if (state == ClientState.Disconnected)
        {
            reliableEndpoint = null;
            hasDisconnected  = true;
        }
        else
        {
            reliableEndpoint = null;
        }
    }
Exemplo n.º 14
0
        public void TestUnreliableOrderedSequence()
        {
            Random rand = new Random();

            for (int run = 0; run < RANDOM_RUNS; run++)
            {
                Console.WriteLine("RUN: " + run);

                List <byte> sentPackets     = new List <byte>();
                List <byte> receivedPackets = new List <byte>();

                List <byte[]> testQueue = new List <byte[]>();

                ReliableEndpoint endpoint1 = new ReliableEndpoint();
                endpoint1.ReceiveCallback = (buffer, size) =>
                {
                    if (buffer[0] == 0)
                    {
                        receivedPackets.Add(buffer[1]);
                    }
                };
                endpoint1.TransmitCallback = (buffer, size) =>
                {
                    int index = testQueue.Count;
                    if (rand.Next(100) >= 50)
                    {
                        index = rand.Next(testQueue.Count);
                    }

                    byte[] item = new byte[size];
                    Buffer.BlockCopy(buffer, 0, item, 0, size);

                    testQueue.Insert(index, item);
                };

                // semi-randomly enqueue packets
                for (int i = 0; i < 10; i++)
                {
                    sentPackets.Add((byte)i);
                    byte[] test = new byte[256];
                    test[0] = 0;
                    test[1] = (byte)i;
                    endpoint1.SendMessage(test, 256, QosType.UnreliableOrdered);
                }

                // now dequeue all packets
                while (testQueue.Count > 0)
                {
                    var item = testQueue[0];
                    testQueue.RemoveAt(0);

                    endpoint1.ReceivePacket(item, item.Length);
                }

                // and verify that packets aren't out of order or duplicated
                List <int> processed = new List <int>();
                int        sequence  = 0;
                for (int i = 0; i < receivedPackets.Count; i++)
                {
                    if (receivedPackets[i] < sequence)
                    {
                        throw new System.Exception("Found out-of-order packet!");
                    }

                    if (processed.Contains(receivedPackets[i]))
                    {
                        throw new System.Exception("Found duplicate packet!");
                    }

                    processed.Add(receivedPackets[i]);
                    sequence = receivedPackets[i];
                }

                Console.WriteLine("Dropped packets: " + (sentPackets.Count - receivedPackets.Count));
            }
        }
Exemplo n.º 15
0
        public void TestReliability()
        {
            Random rand = new Random();

            for (int run = 0; run < RANDOM_RUNS; run++)
            {
                Console.WriteLine("RUN: " + run);

                List <byte> sentPackets     = new List <byte>();
                List <byte> receivedPackets = new List <byte>();

                int droppedPackets = 0;

                ReliableEndpoint endpoint1 = new ReliableEndpoint();
                endpoint1.ReceiveCallback = (buffer, size) =>
                {
                    if (buffer[0] == 0)
                    {
                        receivedPackets.Add(buffer[1]);
                    }
                };
                endpoint1.TransmitCallback = (buffer, size) =>
                {
                    if (rand.Next(100) > 50)
                    {
                        endpoint1.ReceivePacket(buffer, size);
                    }
                    else
                    {
                        droppedPackets++;
                    }
                };

                for (int i = 0; i < 100; i++)
                {
                    sentPackets.Add((byte)i);
                    byte[] test = new byte[256];
                    test[0] = 0;
                    test[1] = (byte)i;
                    endpoint1.SendMessage(test, 256, QosType.Reliable);

                    endpoint1.UpdateFastForward(0.1);
                }

                int iterations = 0;
                while (receivedPackets.Count < sentPackets.Count)
                {
                    endpoint1.UpdateFastForward(1.0);
                    iterations++;
                }

                Console.WriteLine("Dropped packets: " + droppedPackets);

                if (receivedPackets.Count == sentPackets.Count)
                {
                    bool compare = true;
                    for (int i = 0; i < receivedPackets.Count; i++)
                    {
                        if (receivedPackets[i] != sentPackets[i])
                        {
                            compare = false;
                            break;
                        }
                    }

                    if (!compare)
                    {
                        throw new System.Exception("Received packet contents differ!");
                    }
                }
                else
                {
                    throw new System.Exception((sentPackets.Count - receivedPackets.Count) + " packets not received");
                }
            }
        }
Exemplo n.º 16
0
        private void createClient()
        {
            client          = new Client();
            endpoint        = new ReliableEndpoint();
            connectedEvt    = new ManualResetEvent(false);
            client.Tickrate = tickrate;

            client.OnStateChanged += (ClientState state) =>
            {
                switch (state)
                {
                case ClientState.ConnectTokenExpired:
                    if (OnError != null)
                    {
                        OnError(new SocketErrorEventArgs(new Exception("connect token expired")));
                    }
                    break;

                case ClientState.InvalidConnectToken:
                    if (OnError != null)
                    {
                        OnError(new SocketErrorEventArgs(new Exception("invalid connect token")));
                    }
                    break;

                case ClientState.ConnectionTimedOut:
                    isConnected = false;
                    if (OnError != null)
                    {
                        OnError(new SocketErrorEventArgs(new Exception("connection timed out")));
                    }
                    break;

                case ClientState.ChallengeResponseTimedOut:
                    if (OnError != null)
                    {
                        OnError(new SocketErrorEventArgs(new Exception("connection response timed out")));
                    }
                    break;

                case ClientState.ConnectionRequestTimedOut:
                    if (OnError != null)
                    {
                        OnError(new SocketErrorEventArgs(new Exception("connection request timed out")));
                    }
                    break;

                case ClientState.ConnectionDenied:
                    if (OnError != null)
                    {
                        OnError(new SocketErrorEventArgs(new Exception("connection denied")));
                    }
                    break;

                case ClientState.Disconnected:
                    isConnected = false;
                    if (OnClose != null)
                    {
                        OnClose(new SocketCloseEventArgs(1, "disconnected"));
                    }
                    break;

                case ClientState.SendingConnectionRequest:
                    break;

                case ClientState.SendingChallengeResponse:
                    break;

                case ClientState.Connected:
                    isConnected = true;
                    connectedEvt.Set();
                    if (OnOpen != null)
                    {
                        OnOpen();
                    }
                    break;

                default:
                    Logger.Warn(String.Format("Unknown client state in change: {0}", state));
                    break;
                }
            };

            client.OnMessageReceived += (payload, size) =>
            {
                endpoint.ReceivePacket(payload, size);
            };

            endpoint.TransmitCallback = (payload, size) =>
            {
                client.Send(payload, size);
            };
            endpoint.ReceiveCallback = (payload, size) =>
            {
                var payloadCopy = new byte[size];
                Array.Copy(payload, 0, payloadCopy, 0, size);
                if (OnMessage != null)
                {
                    OnMessage(new SocketMessageEventArgs(payloadCopy));
                }
            };
        }
Exemplo n.º 17
0
        public void TestBasicSending()
        {
            var rand = new Random();

            List <byte> sentPackets     = new List <byte>();
            List <byte> receivedPackets = new List <byte>();

            ReliableEndpoint endpoint1 = new ReliableEndpoint();

            endpoint1.ReceiveCallback = (buffer, size) =>
            {
                for (int i = 0; i < size; ++i)
                {
                    receivedPackets.Add(buffer[i]);
                }
            };
            endpoint1.TransmitCallback = (buffer, size) =>
            {
                endpoint1.ReceivePacket(buffer, size);
            };

            for (int i = 0; i < 1; i++)
            {
                int    count = rand.Next(1, 256);
                byte[] test  = new byte[count];
                rand.NextBytes(test);
                for (int j = 0; j < count; ++j)
                {
                    sentPackets.Add(test[j]);
                }

                endpoint1.SendMessage(test, 0, count, QosType.Reliable);
            }

            int iterations = 0;

            for (int i = 0; i < 100000; i++)
            {
                endpoint1.UpdateFastForward(1.0);
                iterations++;

                if (receivedPackets.Count == sentPackets.Count)
                {
                    break;
                }
            }

            if (receivedPackets.Count == sentPackets.Count)
            {
                bool compare = true;
                for (int i = 0; i < receivedPackets.Count; i++)
                {
                    if (receivedPackets[i] != sentPackets[i])
                    {
                        compare = false;
                        break;
                    }
                }

                if (!compare)
                {
                    throw new System.Exception("Received packet contents differ!");
                }
            }
            else
            {
                throw new System.Exception((sentPackets.Count - receivedPackets.Count) + " packets not received");
            }
        }