public void SocketReceivedPacket(SocketConnection conn, Packet pak)
        {
            PacketsReceived++;
            Console.Title = string.Format("{0} Active Connection - {1} Packets Received - {2} Packets Sent", SocketConnections.Count, PacketsReceived, PacketsSent);

            SocketConnection_PacketReceived_Event(conn, pak);
        }
Example #2
0
 static void Socket_SocketConnection_PacketReceived_Event(SocketConnection socketConnection, Packet pak)
 {
     if (pak.IsValid())
     {
         uint Header = pak.Readuint();
         switch (Header)
         {
             case Packets.PAK_CLI_LGNRQST: PacketHandlers.HandleLoginRequest(socketConnection, pak); break;
             case Packets.PAK_CLI_FRNDLSTRQST: PacketHandlers.HandleFriendListRequest(socketConnection, pak); break;
             case Packets.PAK_CLI_MEMINFORQST: PacketHandlers.HandleMemberInfoRequest(socketConnection, pak); break;
             case Packets.PAK_CLI_SNDMSG: PacketHandlers.HandleMessageSend(socketConnection, pak); break;
             case Packets.PAK_CLI_CHNGSTSRQST: PacketHandlers.HandleStatusChangeRequest(socketConnection, pak); break;
             case Packets.PAK_CLI_OFFLNMSGRQST: PacketHandlers.HandleOfflineMessagesRequest(socketConnection, pak); break;
             case Packets.PAK_CLI_FRNDADDRQST: PacketHandlers.HandleFriendAddRequest(socketConnection, pak); break;
             case Packets.PAK_CLI_FRNDDNYRQST: PacketHandlers.HandleFriendDenyRequest(socketConnection, pak); break;
             case Packets.PAK_CLI_CHNGUPDTRQST: PacketHandlers.HandleChangeUpdateRequest(socketConnection, pak); break;
             case Packets.PAK_CLI_FRNDRMVRQST: PacketHandlers.HandleFriendRemoveRequest(socketConnection, pak); break;
             case Packets.PAK_CLI_CALL_RQST: PacketHandlers.HandleCallRequest(socketConnection, pak); break;
             case Packets.PAK_CLI_CALL_ACC: PacketHandlers.HandleCallAccept(socketConnection, pak); break;
             case Packets.PAK_CLI_CALL_DNY: PacketHandlers.HandleCallDeny(socketConnection, pak); break;
             default:
                 Console.ForegroundColor = ConsoleColor.Red;
                 Console.WriteLine("Invalid packet from {0}, 0x{1:X}", socketConnection.IP, Header);
                 break;
         }
     }
     else
     {
         //Console.ForegroundColor = ConsoleColor.Red;
         //Console.WriteLine("Invalid packet from {0}", socketConnection.IP);
         //socketConnection.clientSocket.Close();
     }
 }
Example #3
0
        public static void HandleCallRequest(SocketConnection conn, Packet pak)
        {
            if (conn.ConnectionData.ContainsKey("Member"))
            {
                Member memfrom = (Member)conn.ConnectionData["Member"];
                int MemberCall = pak.Readint();
                int Port = pak.Readint();
                string LocalAddress = pak.Readstring();

                if (memfrom.Friends.Contains(Convert.ToString(MemberCall)))
                {
                    if (ServerSocket.Instance.MemberConnections.ContainsKey(MemberCall))
                    {
                        SocketConnection toconn = ServerSocket.Instance.MemberConnections[MemberCall];
                        if(conn.clientSocket.RemoteEndPoint.ToString().Split(':')[0] == toconn.clientSocket.RemoteEndPoint.ToString().Split(':')[0])
                            toconn.SendPacket(Packet.New(Packets.PAK_SRV_CALL_DLVR, memfrom.ID, LocalAddress, Port));
                        else
                            toconn.SendPacket(Packet.New(Packets.PAK_SRV_CALL_DLVR, memfrom.ID, conn.clientSocket.RemoteEndPoint.ToString().Split(':')[0], Port));
                    }
                    else
                    {
                        //TODO: Notify the member that the friend is offline
                    }
                }
                else
                {
                    //TODO: Notify the member that the friend is not in their list
                }
            }
        }
        public void SocketConnectionDisconnected(SocketConnection socketConnection)
        {
            try
            {
                socketConnection.SqlConnection.Close();
            }
            catch { }

            SocketConnections.Remove(socketConnection);
            socketConnection.Dispose();

            SocketConnectionDisconnected_Event(this, socketConnection);

            Console.Title = string.Format("{0} Active Connection - {1} Packets Received - {2} Packets Sent", SocketConnections.Count, PacketsReceived, PacketsSent);
        }
Example #5
0
        static void Socket_SocketConnectionDisconnected_Event(object sender, SocketConnection socketConnection)
        {
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("Connection Closed: {0}", socketConnection.IP);

            if (socketConnection.ConnectionData.ContainsKey("Member"))
            {
                Member mem = socketConnection.ConnectionData["Member"] as Member;
                mem.StatusCode = 0x0;

                foreach (string FriendIDStr in mem.Friends)
                    try
                    {
                        int FriendID = int.Parse(FriendIDStr);
                        if (ServerSocket.Instance.MemberConnections.ContainsKey(FriendID))
                            ServerSocket.Instance.MemberConnections[FriendID].SendPacket(Packet.New(Packets.PAK_SRV_NEWSTSDLVR, mem.ID, mem.StatusCode));
                    }
                    catch { }

                ServerSocket.Instance.MemberConnections.Remove(mem.ID);
            }
        }
Example #6
0
        public static void HandleCallDeny(SocketConnection conn, Packet pak)
        {
            if (conn.ConnectionData.ContainsKey("Member"))
            {
                Member memfrom = (Member)conn.ConnectionData["Member"];
                int MemberCall = pak.Readint();

                if (memfrom.Friends.Contains(Convert.ToString(MemberCall)))
                {
                    if (ServerSocket.Instance.MemberConnections.ContainsKey(MemberCall))
                    {
                        ServerSocket.Instance.MemberConnections[MemberCall].SendPacket(Packet.New(Packets.PAK_SRV_CALL_DNY_DLVR, memfrom.ID));
                    }
                    else
                    {
                        //TODO: Notify the member that the friend is offline
                    }
                }
                else
                {
                    //TODO: Notify the member that the friend is not in their list
                }
            }
        }
Example #7
0
        public static void HandleStatusChangeRequest(SocketConnection conn, Packet pak)
        {
            if (conn.ConnectionData.ContainsKey("Member"))
            {
                Member mem = (Member)conn.ConnectionData["Member"];
                mem.StatusCode = pak.Readbyte();
                mem.Save();

                foreach (string FriendIDStr in mem.Friends)
                    try
                    {
                        int FriendID = int.Parse(FriendIDStr);
                        if (ServerSocket.Instance.MemberConnections.ContainsKey(FriendID))
                            ServerSocket.Instance.MemberConnections[FriendID].SendPacket(Packet.New(Packets.PAK_SRV_NEWSTSDLVR, mem.ID, mem.StatusCode));
                    }
                    catch { }
            }
        }
Example #8
0
        public static void HandleOfflineMessagesRequest(SocketConnection conn, Packet pak)
        {
            if (conn.ConnectionData.ContainsKey("Member"))
            {
                Member mem = (Member)conn.ConnectionData["Member"];
                OfflineMessage[] OfflineMessages = OfflineMessageManager.Instance.GetMessages(mem.ID);

                foreach (OfflineMessage msg in OfflineMessages)
                    conn.SendPacket(Packet.New(Packets.PAK_SRV_MSGDLVR, msg.From, msg.Message));
            }
        }
Example #9
0
        public static void HandleMessageSend(SocketConnection conn, Packet pak)
        {
            if (conn.ConnectionData.ContainsKey("Member"))
            {
                Member memfrom = (Member)conn.ConnectionData["Member"];
                int MemberSendTo = pak.Readint();
                string Message = pak.Readstring();

                if (memfrom.Friends.Contains(Convert.ToString(MemberSendTo)))
                {
                    if (ServerSocket.Instance.MemberConnections.ContainsKey(MemberSendTo))
                        ServerSocket.Instance.MemberConnections[MemberSendTo].SendPacket(Packet.New(Packets.PAK_SRV_MSGDLVR, memfrom.ID, Message));
                    else
                    {
                        OfflineMessageManager.Instance.NewOfflineMessage(memfrom.ID, MemberSendTo, Message);
                    }
                }
                else
                {
                    //TODO: Notify the member that the friend is not in their list
                }
            }
        }
Example #10
0
        public static void HandleLoginRequest(SocketConnection conn, Packet pak)
        {
            string Account = pak.Readstring();
            string PasswordHash = pak.Readstring();

            string Nickname = Member.TryLoginWithPassword(Account, PasswordHash, conn.SqlConnection);

            if (Nickname == null)
                conn.SendPacket(Packet.New(Packets.PAK_SRV_LGNRESP, false, (byte)0x01));
            else
            {
                Member mem = new Member(Account, conn.SqlConnection);

                if (ServerSocket.Instance.MemberConnections.ContainsKey(mem.ID))
                {
                    ServerSocket.Instance.MemberConnections[mem.ID].SendPacket(Packet.New(0x0));
                    conn.SendPacket(Packet.New(Packets.PAK_SRV_LGNRESP, false, (byte)0x02));
                }
                else
                {
                    mem.StatusCode = 0x01;
                    conn.ConnectionData.Add("MemberConnected", true);
                    conn.ConnectionData.Add("Member", mem);

                    foreach (string FriendIDStr in mem.Friends)
                        try
                        {
                            int FriendID = int.Parse(FriendIDStr);
                            if (ServerSocket.Instance.MemberConnections.ContainsKey(FriendID))
                                ServerSocket.Instance.MemberConnections[FriendID].SendPacket(Packet.New(Packets.PAK_SRV_NEWSTSDLVR, mem.ID, mem.StatusCode));
                        }
                        catch { }

                    ServerSocket.Instance.MemberConnections.Add(mem.ID, conn);

                    conn.SendPacket(Packet.New(Packets.PAK_SRV_LGNRESP, true, Nickname, mem.MemberData, mem.FirstName + " " + mem.LastName, mem.ID, mem.IsPremiumIM, mem.NameFontColor, mem.StatusFontColor));
                }
            }

            Console.WriteLine("HandleLoginRequest({0}, {1});", Account, PasswordHash);
        }
Example #11
0
        public static void HandleFriendRemoveRequest(SocketConnection conn, Packet pak)
        {
            if (conn.ConnectionData.ContainsKey("Member"))
            {
                Member member1 = (Member)conn.ConnectionData["Member"];
                int MemberID = pak.Readint();

                Member member2;
                if (ServerSocket.Instance.MemberConnections.ContainsKey(MemberID))
                    member2 = ServerSocket.Instance.MemberConnections[MemberID].ConnectionData["Member"] as Member;
                else
                    member2 = new Member(MemberID, conn.SqlConnection);

                if (!member2.IsValid || !member1.Friends.Contains(member2.ID.ToString()))
                    return;

                member1.Friends.Remove(member2.ID.ToString());
                member2.Friends.Remove(member1.ID.ToString());

                member1.Save();
                member2.Save();

                if (ServerSocket.Instance.MemberConnections.ContainsKey(MemberID))
                    ServerSocket.Instance.MemberConnections[MemberID].SendPacket(Packet.New(Packets.PAK_SRV_FRNDRMVDLVR, member1.ID));
                conn.SendPacket(Packet.New(Packets.PAK_SRV_FRNDRMVDLVR, member2.ID));
            }
        }
Example #12
0
        public static void HandleFriendListRequest(SocketConnection conn, Packet pak)
        {
            if (conn.ConnectionData.ContainsKey("Member"))
            {
                Packet pak2 = new Packet(null);
                Member mem = (Member)conn.ConnectionData["Member"];

                pak2.Write(Packets.PAK_SRV_FRNDLSTRESP);
                int FriendCount = 0;
                foreach (string FriendID in mem.Friends)
                    try { Convert.ToInt32(FriendID); FriendCount++; }
                    catch { }
                foreach (string FriendID in mem.PendingFriends)
                    try { Convert.ToInt32(FriendID); FriendCount++; }
                    catch { }

                pak2.Write(FriendCount);

                foreach (string FriendID in mem.Friends)
                    try { pak2.Write(Convert.ToInt32(FriendID)); }
                    catch { }
                foreach (string FriendID in mem.PendingFriends)
                    try { pak2.Write(Convert.ToInt32(FriendID)); }
                    catch { }

                conn.SendPacket(pak2);

                Console.WriteLine("HandleFriendListRequest({0});", mem.Friends.Count);
            }
        }
Example #13
0
        public static void HandleFriendDenyRequest(SocketConnection conn, Packet pak)
        {
            if (conn.ConnectionData.ContainsKey("Member"))
            {
                Member member1 = (Member)conn.ConnectionData["Member"];

                int MemberID = pak.Readint();

                Member member2;
                if (ServerSocket.Instance.MemberConnections.ContainsKey(MemberID))
                    member2 = ServerSocket.Instance.MemberConnections[MemberID].ConnectionData["Member"] as Member;
                else
                    member2 = new Member(MemberID, conn.SqlConnection);

                if (!member2.IsValid || member1.Friends.Contains(member2.ID.ToString()))
                    return;

                if (member1.PendingFriends.Contains(member2.ID.ToString()))
                {
                    member1.PendingFriends.Remove(member2.ID.ToString());
                    member1.Save();
                }
            }
        }
Example #14
0
        public static void HandleFriendBlockRequest(SocketConnection conn, Packet pak)
        {
            if (conn.ConnectionData.ContainsKey("Member"))
            {
                Member mem = (Member)conn.ConnectionData["Member"];

            }
        }
Example #15
0
        public static void HandleFriendAddRequest(SocketConnection conn, Packet pak)
        {
            if (conn.ConnectionData.ContainsKey("Member"))
            {
                Member member1 = (Member)conn.ConnectionData["Member"];
                string FriendSearch = pak.Readstring();

                int MemberID = Member.FindMember(FriendSearch, conn.SqlConnection);
                if (MemberID == -1 || MemberID == member1.ID)
                    return;

                Member member2;
                if (ServerSocket.Instance.MemberConnections.ContainsKey(MemberID))
                    member2 = ServerSocket.Instance.MemberConnections[MemberID].ConnectionData["Member"] as Member;
                else
                    member2 = new Member(MemberID, conn.SqlConnection);

                if (!member2.IsValid || member1.Friends.Contains(member2.ID.ToString()))
                    return;

                if (member1.PendingFriends.Contains(member2.ID.ToString()))
                {
                    member1.PendingFriends.Remove(member2.ID.ToString());

                    member1.Friends.Add(member2.ID.ToString());
                    member2.Friends.Add(member1.ID.ToString());

                    member1.Save();
                    member2.Save();

                    Packet pak2 = new Packet(null);
                    pak2.Write(Packets.PAK_SRV_MEMINFORESP);

                    if (member2.IsValid)
                    {
                        pak2.Write(true);
                        pak2.Write(member2.ID);
                        pak2.Write(member2.Nickname);
                        pak2.Write(member2.MemberData);
                        pak2.Write(member2.Authority);
                        pak2.Write(member2.StatusCode);
                        pak2.Write(false);
                        pak2.Write(member2.FirstName);
                        pak2.Write(member2.LastName);
                        pak2.Write(member2.IsPremiumIM);
                        pak2.Write(member2.NameFontColor);
                        pak2.Write(member2.StatusFontColor);
                    }
                    else
                        pak2.Write(false);

                    conn.SendPacket(pak2);

                    if (ServerSocket.Instance.MemberConnections.ContainsKey(MemberID))
                    {
                        SocketConnection conn2 = ServerSocket.Instance.MemberConnections[MemberID];

                        Packet pak3 = new Packet(null);
                        pak3.Write(Packets.PAK_SRV_MEMINFORESP);

                        if (member1.IsValid)
                        {
                            pak3.Write(true);
                            pak3.Write(member1.ID);
                            pak3.Write(member1.Nickname);
                            pak3.Write(member1.MemberData);
                            pak3.Write(member1.Authority);
                            pak3.Write(member1.StatusCode);
                            pak3.Write(false);
                            pak3.Write(member1.FirstName);
                            pak3.Write(member1.LastName);
                            pak3.Write(member1.IsPremiumIM);
                            pak3.Write(member1.NameFontColor);
                            pak3.Write(member1.StatusFontColor);
                        }
                        else
                            pak3.Write(false);

                        conn2.SendPacket(pak3);
                    }
                }
                else if (member2.PendingFriends.Contains(member1.ID.ToString())) { }
                else
                {
                    member2.PendingFriends.Add(member1.ID.ToString());
                    member2.Save();

                    if (ServerSocket.Instance.MemberConnections.ContainsKey(MemberID))
                    {
                        SocketConnection conn2 = ServerSocket.Instance.MemberConnections[MemberID];

                        Packet pak2 = new Packet(null);
                        pak2.Write(Packets.PAK_SRV_MEMINFORESP);

                        if (member1.IsValid)
                        {
                            pak2.Write(true);
                            pak2.Write(member1.ID);
                            pak2.Write(member1.Nickname);
                            pak2.Write(member1.MemberData);
                            pak2.Write(member1.Authority);
                            pak2.Write(member1.StatusCode);
                            pak2.Write(true);
                            pak2.Write(member1.FirstName);
                            pak2.Write(member1.LastName);
                            pak2.Write(member1.IsPremiumIM);
                            pak2.Write(member1.NameFontColor);
                            pak2.Write(member1.StatusFontColor);
                        }
                        else
                            pak2.Write(false);

                        conn2.SendPacket(pak2);
                    }
                }
            }
        }
Example #16
0
        public static void HandleMemberInfoRequest(SocketConnection conn, Packet pak)
        {
            int MemberID = 0;

            if (conn.ConnectionData.ContainsKey("Member"))
            {
                try
                {
                    Packet pak2 = new Packet(null);
                    pak2.Write(Packets.PAK_SRV_MEMINFORESP);

                    MemberID = pak.Readint();
                    Member mem = conn.ConnectionData["Member"] as Member;
                    Member member;
                    if (ServerSocket.Instance.MemberConnections.ContainsKey(MemberID))
                        member = ServerSocket.Instance.MemberConnections[MemberID].ConnectionData["Member"] as Member;
                    else
                        member = new Member(MemberID, conn.SqlConnection);
                    if (member.IsValid)
                    {
                        pak2.Write(true);
                        pak2.Write(MemberID);
                        pak2.Write(member.Nickname);
                        pak2.Write(member.MemberData);
                        pak2.Write(member.Authority);
                        pak2.Write(member.StatusCode);
                        pak2.Write(mem.PendingFriends.Contains(MemberID.ToString()));
                        pak2.Write(member.FirstName);
                        pak2.Write(member.LastName);
                        pak2.Write(member.IsPremiumIM);
                        pak2.Write(member.NameFontColor);
                        pak2.Write(member.StatusFontColor);
                    }
                    else
                        pak2.Write(false);

                    conn.SendPacket(pak2);
                }
                catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            }

            Console.WriteLine("HandleMemberInfoRequest({0}); SqlState: {1}", MemberID, conn.SqlConnection.State);
        }
        private void AcceptConnection(IAsyncResult ar)
        {
            try
            {
                Socket ClientSocket = RawSocket.EndAccept(ar);
                RawSocket.BeginAccept(new AsyncCallback(AcceptConnection), RawSocket);

                SocketConnection socketConnection = new SocketConnection(this, ClientSocket);
                SocketConnections.Add(socketConnection);
                SocketConnectionConnected_Event(this, socketConnection);

                Console.Title = string.Format("{0} Active Connection - {1} Packets Received - {2} Packets Sent", SocketConnections.Count, PacketsReceived, PacketsSent);
            }
            catch { }
        }
Example #18
0
 static void Socket_SocketConnectionConnected_Event(object sender, SocketConnection socketConnection)
 {
     Console.ForegroundColor = ConsoleColor.Blue;
     Console.WriteLine("New Connection: {0}", socketConnection.IP);
 }