示例#1
0
 public LobbyClient(SkySocket c)
 {
     FriendList = new List<User>();
     Notifications = new List<Notification>();
     Callbacks = new Dictionary<string, SocketMessageResult>();
     Games = new List<HostedGame>();
     Socket = c;
 }
示例#2
0
 public LobbyClient()
 {
     FriendList = new List<User>();
     Notifications = new List<Notification>();
     Callbacks = new Dictionary<string, SocketMessageResult>();
     Games = new List<HostedGame>();
     Chatting = new Lobby.Chatting(this);
     Socket = new SkySocket();
     Socket.OnMessageReceived += new SkySocket.MessageReceived(this.OnMessageReceived);
     Socket.OnConnectionClosed += new SkySocket.ConnectionClosed(Socket_OnConnectionClosed);
 }
示例#3
0
        private static void AcceptReceiveDataCallback(IAsyncResult ar)
        {
            // Logger.TL(System.Reflection.MethodInfo.GetCurrentMethod().Name, "ClientLocker");
            lock (ClientLocker)
            {
                //Logger.L(System.Reflection.MethodInfo.GetCurrentMethod().Name, "ClientLocker");
                // Get the socket that handles the client request.

                try
                {
                    TcpListener listener = (TcpListener)ar.AsyncState;
                    SkySocket ss = new SkySocket(listener.EndAcceptTcpClient(ar));
                    Client c = new Client(ss, _nextId);
                    c.OnDisconnect += new EventHandler(c_OnDisconnect);
                    Clients.Add(c);
                    //Logger.log(MethodInfo.GetCurrentMethod().Name, "Client " + _nextId.ToString() + " connected.");
                    _nextId++;
                }
                catch (ObjectDisposedException)
                {
                    Console.WriteLine("AcceptReceiveDataCallback: ObjectDisposedException");
                }
                catch (SocketException)
                {

                }
                AcceptClients();
                //Logger.UL(System.Reflection.MethodInfo.GetCurrentMethod().Name, "ClientLocker");
            }
        }
示例#4
0
 void Socket_OnConnectionClosed(SkySocket socket)
 {
     if (OnDisconnect != null)
     {
         OnDisconnect.BeginInvoke(null, null, null, null);
     }
     Socket.Dispose();
 }
示例#5
0
        /// <summary>
        /// Whenever a SkySocket gets a message, it goes here for processing.
        /// </summary>
        /// <param name="sm">SocketMessage</param>
        private void OnMessageReceived(SkySocket ss,Net.SocketMessage sm)
        {
            User u;
            if (Callbacks.ContainsKey(sm.Header.ToLower()))
            {
                SocketMessageResult a = Callbacks[sm.Header.ToLower()];
                if (a != null)
                {
                    a.Invoke(sm);
                    Callbacks.Remove(sm.Header.ToLower());
                    return;
                }
            }
            switch (sm.Header.ToLower())
            {
                case "end":
                    {
                        Stop();
                        break;
                    }
                case "loginsuccess":
                    Trace.TraceInformation("Got LoginSuccess");
                    Me = (User)sm["me"];
                    if (Me != null)
                    {
                        _onLoginFinished.Invoke(LoginResult.Success, DateTime.Now, "");
                        Chatting.JoinChatRoom(0);
                    }
                    else
                    {
                        _onLoginFinished.Invoke(LoginResult.Failure, DateTime.Now, "Data failure.");
                        Stop();
                        //Close(DisconnectReason.CleanDisconnect);
                    }
                    break;
                case "loginfailed":
                    Trace.TraceInformation("Got LoginFailed");
                    _onLoginFinished.Invoke(LoginResult.Failure, DateTime.Now, (sm["message"] != null) ? (string)sm["message"] : "");
                    break;
                case "friends":
                    lock (friendLocker)
                    {
                        FriendList = new List<User>();
                        foreach (NameValuePair p in sm.Data)
                        {
                            FriendList.Add((User)p.Value);
                        }
                        if (OnDataRecieved != null)
                        {
                            foreach (DataRecieved d in OnDataRecieved.GetInvocationList())
                                d.BeginInvoke(DataRecType.FriendList, null, new AsyncCallback((IAsyncResult r) => { }), null);
                        }
                    }
                    break;
                case "servermessage":
                    {
                        string mess = sm["message"] as string;
                        if (mess != null && OnDataRecieved != null)
                        {
                            foreach(DataRecieved d in OnDataRecieved.GetInvocationList())
                                d.BeginInvoke(DataRecType.ServerMessage, mess, new AsyncCallback((IAsyncResult r) => { }), null);
                        }
                        break;
                    }
                case "friendrequest":
                    lock (noteLocker)
                    {
                        u = (User)sm.Data[0].Value;
                        foreach (Notification n in Notifications)
                        {
                            FriendRequestNotification fr = n as FriendRequestNotification;
                            if (fr != null)
                            {
                                if (fr.User.Uid == u.Uid)
                                    return;
                            }
                        }
                        Notifications.Add(new FriendRequestNotification(u, this, _nextNoteId));
                        _nextNoteId++;
                        if (OnFriendRequest != null)
                            foreach (FriendRequest fr in OnFriendRequest.GetInvocationList())
                                fr.BeginInvoke(u, new AsyncCallback((IAsyncResult r) => { }), null);
                    }
                    break;
                case "status":
                    lock (friendLocker)
                    {
                        u = (User)sm.Data[0].Value;
                        User f = FriendList.FirstOrDefault(us => us.Equals(u));
                        if (f != null)
                        {
                            f.DisplayName = u.DisplayName;
                            f.Status = u.Status;
                            f.CustomStatus = u.CustomStatus;
                        }
                        if (u.Equals(Me))
                        {
                            Me.DisplayName = u.DisplayName;
                            Me.Status = u.Status;
                            Me.CustomStatus = u.CustomStatus;
                        }
                        if (OnUserStatusChanged != null)
                            foreach (UserStatusChanged usc in OnUserStatusChanged.GetInvocationList())
                                usc.BeginInvoke(u.Status, u, new AsyncCallback((IAsyncResult r) => { }), null);
                        if (OnDataRecieved != null)
                            foreach (DataRecieved dr in OnDataRecieved.GetInvocationList())
                                dr.BeginInvoke(DataRecType.FriendList, null, new AsyncCallback((IAsyncResult r) => { }), null);
                    }
                    break;
                case "customstatus":
                    lock (friendLocker)
                    {
                        u = (User)sm["user"];
                        string s = (string)sm["status"];
                        if (u != null && s != null)
                        {
                            if (u.Equals(Me))
                                Me.CustomStatus = s;
                            else
                            {
                                int i = FriendList.IndexOf(u);
                                if (i > -1)
                                    FriendList[i].CustomStatus = s;
                                if (OnDataRecieved != null)
                                    foreach (DataRecieved dr in OnDataRecieved.GetInvocationList())
                                        dr.BeginInvoke(DataRecType.UserCustomStatus, u, new AsyncCallback((IAsyncResult r) => { }), null);
                            }
                        }
                    }
                    break;
                case "banned":
                    int time = (int)sm["end"];

                    _onLoginFinished.Invoke(LoginResult.Banned, Skylabs.ValueConverters.FromPhpTime(time), "");
                    break;
                case "gamelist":
                    {
                        lock (gameLocker)
                        {
                            List<HostedGame> games = sm["list"] as List<HostedGame>;
                            Games = games;
                            if (games.Count > 0)
                                if (OnGameHostEvent != null)
                                    foreach (GameHostEvent ge in OnGameHostEvent.GetInvocationList())
                                        ge.BeginInvoke(Games[0], new AsyncCallback((IAsyncResult r) => { }), null);
                        }
                        break;
                    }
                case "gamehosting":
                    {
                        lock (gameLocker)
                        {
                            HostedGame gm = new HostedGame(sm);
                            Games.Add(gm);
                            if (OnGameHostEvent != null)
                                LazyAsync.Invoke(() => OnGameHostEvent.Invoke(gm));
                        }
                        break;
                    }
                case "gamestarted":
                    {
                        lock (gameLocker)
                        {
                            int p = (int)sm["port"];

                            HostedGame gm = Games.FirstOrDefault(g => g.Port == p);
                            if (gm != null)
                            {
                                gm.GameStatus = HostedGame.eHostedGame.GameInProgress;
                                if (OnGameHostEvent != null)
                                    LazyAsync.Invoke(() => OnGameHostEvent.Invoke(gm));
                            }
                        }
                        break;
                    }
                case "gameend":
                    {
                        lock (gameLocker)
                        {
                            int p = (int)sm["port"];

                            HostedGame gm = Games.FirstOrDefault(g => g.Port == p);
                            if (gm != null)
                            {
                                gm.GameStatus = HostedGame.eHostedGame.StoppedHosting;
                                if (OnGameHostEvent != null)
                                    LazyAsync.Invoke(() => OnGameHostEvent.Invoke(gm));
                                Games.Remove(gm);
                            }
                        }
                        break;
                    }
                case "userjoinedchatroom":
                    {
                        User us = (User)sm["user"];
                        List<User> allusers = (List<User>)sm["allusers"];
                        long? id = (long?)sm["roomid"];
                        if (us == null || allusers == null || id == null)
                            return;
                        long id2 = (long)id;
                        Chatting.UserJoinedChat(id2, us, allusers);
                        break;
                    }
                case "userleftchatroom":
                    {
                        User us = (User)sm["user"];
                        long? id = (long?)sm["roomid"];
                        if (us == null || id == null)
                            return;
                        long id2 = (long)id;
                        Chatting.UserLeftChat(id2, us);
                        break;
                    }
                case "chatmessage":
                    {
                        User us = (User)sm["user"];
                        long? id = (long?)sm["roomid"];
                        string mess = (string)sm["mess"];
                        if (us == null || id == null || mess == null)
                            return;
                        long id2 = (long)id;
                        Chatting.RecieveChatMessage(id2, us, mess);
                        break;
                    }

            }
        }