Esempio n. 1
0
        public bool Kick(User user, string Reason)
        {
            try
            {
                SendToUser(user, new Message() { Type = MessageType.Kick, Text = Reason });
                Users.Remove(user);

                if (KickedUser != null)
                    KickedUser(new KickEventArgs(user.Guid, user.IP, user.Port, Reason));

                return true;
            }
            catch (Exception ex)
            {
                if (ServerError != null)
                    ServerError(new ServerErrorEventArgs(ex, DateTime.Now, this));

                return false;
            }
        }
Esempio n. 2
0
        public bool SendToUser(User user, Message message)
        {
            AsyncObject ao = new AsyncObject(1);
            ao.Buffer = message;
            ao.WorkingSocket = user.Socket;
            try
            {
                user.Socket.BeginSend(ao.Buffer, 0, ao.Buffer.Length, SocketFlags.None, _fnSendHandler, ao);
            }
            catch (Exception ex)
            {
                if (ServerError != null)
                    ServerError(new ServerErrorEventArgs(ex, DateTime.Now, this));

                return false;
            }

            return true;
        }
Esempio n. 3
0
        private void connect(IAsyncResult ar)
        {
            Socket client;
            try
            {
                client = _ServerSocket.EndAccept(ar);
            }
            catch (Exception ex)
            {
                if (ServerError != null)
                    ServerError(new ServerErrorEventArgs(ex, DateTime.Now, this));

                return;
            }

            AsyncObject ao = new AsyncObject(4096);
            ao.WorkingSocket = client;

            User user = new User();
            user.Socket = client;
            user.IP = ((IPEndPoint)client.RemoteEndPoint).Address.ToString();
            user.Port = ((IPEndPoint)client.RemoteEndPoint).Port;
            user.Server = this;
            user.Guid = null;
            Users.Add(user);

            Message info = new Message() { Type = MessageType.Info, Text = Data.ServerName };
            SendToUser(user, info);

            info = new Message() { Type = MessageType.Info, Text = Data.MaxUserCount.GetValueOrDefault().ToString() };
            SendToUser(user, info);

            //info = new Message() { Type = MessageType.Info, Text = Utility.ToBase64(Data.ServerImage, ImageFormat.Png) };
            //SendToUser(user, info);

            try
            {
                client.BeginReceive(ao.Buffer, 0, ao.Buffer.Length, SocketFlags.None, _fnReceiveHandler, ao);
            }
            catch (Exception ex)
            {
                if (ServerError != null)
                    ServerError(new ServerErrorEventArgs(ex, DateTime.Now, this));

                return;
            }
        }
Esempio n. 4
0
        public bool SendToOther(User other, Message message)
        {
            foreach (var it in Users)
            {
                if (it == other) continue;

                AsyncObject ao = new AsyncObject(1);
                ao.Buffer = message;
                ao.WorkingSocket = it.Socket;
                try
                {
                    it.Socket.BeginSend(ao.Buffer, 0, ao.Buffer.Length, SocketFlags.None, _fnSendHandler, ao);
                }
                catch (Exception ex)
                {
                    if (ServerError != null)
                        ServerError(new ServerErrorEventArgs(ex, DateTime.Now, this));

                    return false;
                }
            }

            return true;
        }