示例#1
0
        public static void OnMessageRecieved(IAsyncResult ar)
        {
            try
            {
                Socket.EndReceive(ar);
            }
            catch (SocketException)
            {
                return;
            }

            SausageHelper.StripData(ref Data);
            Parse(Encoding.ASCII.GetString(Data));

            Data = new byte[1024];
            Listen();
        }
        private void Parse(string msg)
        {
            byte[] m = Encoding.ASCII.GetBytes(msg);
            SausageHelper.StripData(ref m);
            msg = Encoding.ASCII.GetString(m);
            PacketFormat      Message = JsonConvert.DeserializeObject <PacketFormat>(msg);
            SausageConnection Reciever;

            switch (Message.Option)
            {
            case PacketOption.ClientMessage:
                if (!SausageServer.UsersDictionary[Message.Guid].IsMuted)
                {
                    SausageServer.Log(Message);
                }
                break;

            case PacketOption.NameChange:
                SausageServer.Log(Message);
                SausageServer.UsersDictionary[Message.Guid].Name = Message.NewName;
                break;

            // don't need to check for user connected as that is dealt with in OnUserConnect
            case PacketOption.UserDisconnected:
                SausageServer.Log(Message);
                Disconnect();
                SausageServer.UsersDictionary.Remove(Message.Guid);
                break;

            case PacketOption.UserBanned:
                if (UserInfo.IsAdmin)
                {
                    SausageServer.Ban(SausageServer.ConnectedUsers.First(x => x.UserInfo.Guid == Message.Guid));
                }
                break;

            case PacketOption.UserKicked:
                if (UserInfo.IsAdmin)
                {
                    SausageServer.Kick(SausageServer.ConnectedUsers.First(x => x.UserInfo.Guid == Message.Guid));
                }
                break;

            case PacketOption.UserMuted:
                if (UserInfo.IsAdmin)
                {
                    SausageServer.Mute(SausageServer.ConnectedUsers.First(x => x.UserInfo.Guid == Message.Guid));
                }
                break;

            case PacketOption.UserUnmuted:
                if (UserInfo.IsAdmin)
                {
                    SausageServer.Unmute(SausageServer.ConnectedUsers.First(x => x.UserInfo.Guid == Message.Guid));
                }
                break;

            case PacketOption.FriendRequest:
                Reciever = SausageServer.ConnectedUsers.FirstOrDefault(x => x.UserInfo.Guid == Message.Guid);
                if (Reciever == null)
                {
                    SendAsync(new PacketFormat(PacketOption.IsServer)
                    {
                        Content = "User not found"
                    });
                }
                else
                {
                    Reciever.SendAsync(Message);
                    SausageServer.UiCtx.Send(x => SausageServer.Vm.Messages.Add(new ServerMessage($"{UserInfo} requested {Reciever} for a friend request.")));
                }
                break;

            case PacketOption.FriendRequestAccepted:
                Reciever = SausageServer.ConnectedUsers.FirstOrDefault(x => x.UserInfo.Guid == Message.Guid);
                Reciever.SendAsync(Message);
                break;

            case PacketOption.FriendRequestDenied:
                Reciever = SausageServer.ConnectedUsers.FirstOrDefault(x => x.UserInfo.Guid == Message.Guid);
                Reciever.SendAsync(Message);
                break;

            case PacketOption.DmMessage:
                Reciever = SausageServer.ConnectedUsers.FirstOrDefault(x => x.UserInfo.Guid == Message.Guid);
                Reciever.SendAsync(Message);
                break;

            case PacketOption.DmAccepted:
                Reciever = SausageServer.ConnectedUsers.FirstOrDefault(x => x.UserInfo.Guid == Message.Guid);
                Reciever.SendAsync(Message);
                break;

            case PacketOption.DmDenied:
                Reciever = SausageServer.ConnectedUsers.FirstOrDefault(x => x.UserInfo.Guid == Message.Guid);
                Reciever.SendAsync(Message);
                break;

            case PacketOption.DmStartRequest:
                Reciever = SausageServer.ConnectedUsers.FirstOrDefault(x => x.UserInfo.Guid == Message.Guid);
                Reciever.SendAsync(Message);
                break;
            }
        }