Exemple #1
0
        private void Client_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            try
            {
                using (Message message = e.GetMessage())
                    using (DarkRiftReader reader = message.GetReader())
                    {
                        OpCodes opCode = (OpCodes)message.Tag;

                        if (opCode != OpCodes.AuthenticationResponse && pendingConnections.Contains(e.Client.ID))
                        {
                            pendingConnections.Remove(e.Client.ID);
                            LeaveRoom(e.Client.ID);
                            e.Client.Disconnect();
                            return;
                        }

                        switch (opCode)
                        {
                        case OpCodes.UpdateRoomData:
                            string extraData     = reader.ReadString();
                            int    newMaxPlayers = reader.ReadInt32();
                            UpdateRoomData(e, extraData, newMaxPlayers);
                            break;

                        case OpCodes.AuthenticationResponse:
                            if (reader.ReadString() == authKey)
                            {
                                pendingConnections.Remove(e.Client.ID);
                                using (DarkRiftWriter writer = DarkRiftWriter.Create())
                                {
                                    using (Message sendAuthed = Message.Create((ushort)OpCodes.Authenticated, writer))
                                        e.Client.SendMessage(sendAuthed, SendMode.Reliable);
                                }
                            }
                            else
                            {
                                pendingConnections.Remove(e.Client.ID);
                                LeaveRoom(e.Client.ID);
                                e.Client.Disconnect();
                            }
                            break;

                        case OpCodes.RequestID:
                            SendClientID(e);
                            break;

                        case OpCodes.CreateRoom:
                            CreateRoom(e, reader.ReadInt32(), reader.ReadString(), reader.ReadBoolean(), reader.ReadString(), reader.ReadBoolean(), reader.ReadString());
                            break;

                        case OpCodes.JoinServer:
                            ushort hostID        = reader.ReadUInt16();
                            bool   useRelay      = reader.ReadBoolean();
                            string clientLocalIP = reader.ReadString();
                            JoinRoom(e, hostID, useRelay, clientLocalIP);
                            break;

                        case OpCodes.SendData:
                            byte[] readBuffer = readBuffers.Rent(reader.ReadInt32());
                            reader.ReadBytesInto(readBuffer, 0);
                            ProcessData(e, reader, readBuffer, readBuffer.Length);
                            break;

                        case OpCodes.LeaveRoom:
                            LeaveRoom(e.Client);
                            break;

                        case OpCodes.KickPlayer:
                            ushort clientID = reader.ReadUInt16();
                            LeaveRoom(clientID, e.Client.ID);
                            break;

                        case OpCodes.RequestServers:
                            SendServerList(e);
                            break;
                        }
                    }
            }
            catch
            {
                // Do disconnect/kick maybe later if they do be acting up.
            }
        }