Exemple #1
0
        public void LoadProcessors()
        {
            myServer.AssignProcessor(delegate(Packet P)
            {
                LoginPacket myPacket = (LoginPacket)P;
                Console.WriteLine(myPacket.Username + " attempted to connect!");
                IConnectionWrapper myData;
                if (myLoginHandler.VerifyPlayerLogin(myPacket.Username, myPacket.Sender, myPacket.GetCreationTime(), myPacket.GetSHA(), out myData))
                {
                    var PDataInfo = (PlayerData)myData.MyExternalData;
                    if (PDataInfo != null)
                    {
                        PlayerInfoPacket myInfo = new PlayerInfoPacket();
                        myInfo.NewChar          = false;
                        myInfo.myMap            = myMapHandler.GetPlayerMap(PDataInfo);
                        myInfo.Sender           = myPacket.Sender;
                        myServer.Send(myInfo, NetDeliveryMethod.ReliableOrdered);
                        myMapHandler.Login(myData);
                    }
                    else
                    {
                        ThreadPool.QueueUserWorkItem(delegate(object State)
                        {
                            PlayerInfoPacket myInfo = new PlayerInfoPacket();
                            myInfo.Sender           = myPacket.Sender;
                            PDataInfo      = new PlayerData();
                            myInfo.NewChar = true;
                            myInfo.SetPlayerData(PDataInfo);
                            myInfo.myMap           = myMapHandler.PlaceInFirstAvailable(myInfo.Sender, PDataInfo);
                            PDataInfo.CurrentMapID = PDataInfo.CurrentMapID;
                            myLoginHandler.UpdateInfo(myPacket.GetSHA(), PDataInfo);
                            myServer.Send(myInfo, NetDeliveryMethod.ReliableOrdered);
                        });
                    }
                    Console.WriteLine("User Authenticated Successfully!\n Awaiting Character Creation!");
                }
                else
                {
                    LoginFailedPacket myAck = new LoginFailedPacket();
                    myAck.Sender            = myPacket.Sender;
                    myServer.Send(myAck, NetDeliveryMethod.ReliableOrdered);
                    Console.WriteLine("User Authentication Failed!");
                }
            }, typeof(LoginPacket));

            myServer.AssignProcessor(delegate(Packet P)
            {
                IConnectionWrapper mySender;
                if ((mySender = myLoginHandler.isLoggedIn(P.UserID)) != null)
                {
                    AnnounceToMapPacket myPacket = (AnnounceToMapPacket)P;
                }
            }, typeof(AnnounceToMapPacket));

            myServer.AssignProcessor(delegate(Packet P)
            {
                IConnectionWrapper mySender;
                if ((mySender = myLoginHandler.isLoggedIn(P.UserID)) != null)
                {
                    IConnectionWrapper Recipient;
                    WhisperMessagePlayer myPacket = (WhisperMessagePlayer)P;
                    if ((Recipient = myLoginHandler.isLoggedIn(myPacket.RecipientUserID)) != null)
                    {
                        PlayerData Recip  = (PlayerData)Recipient.MyExternalData;
                        PlayerData Sender = (PlayerData)mySender.MyExternalData;
                        if (Recip.CurrentMapID.CompareTo(Sender.CurrentMapID) == 0)
                        {
                            myPacket.Sender = Recipient.GetConnection();
                            myServer.Send(myPacket, NetDeliveryMethod.ReliableOrdered);
                        }
                    }
                    else
                    {
                        //TODO: send response packet.
                    }
                }
            }, typeof(WhisperMessagePlayer));

            myServer.AssignProcessor(delegate(Packet P)
            {
                IConnectionWrapper mySender;
                if ((mySender = myLoginHandler.isLoggedIn(P.UserID)) != null)
                {
                    SendMessageLocalAreaPacket myPacket = (SendMessageLocalAreaPacket)P;
                }
            }, typeof(SendMessageLocalAreaPacket));

            myServer.AssignProcessor(delegate(Packet P)
            {
                IConnectionWrapper mySender;
                if ((mySender = myLoginHandler.isLoggedIn(P.UserID)) != null)
                {
                    SendMessageMapAreaPacket myPacket = (SendMessageMapAreaPacket)P;
                    //List<PlayerData> myData = myMapHandler.GetPlayersOnMap();
                }
            }, typeof(SendMessageMapAreaPacket));

            myServer.AssignProcessor(delegate(Packet P)
            {
                IConnectionWrapper mySender;
                if ((mySender = myLoginHandler.isLoggedIn(P.UserID)) != null)
                {
                    AnnounceToAllMapsPacket myPacket = (AnnounceToAllMapsPacket)P;
                }
            }, typeof(AnnounceToAllMapsPacket));


            myServer.AssignProcessor(delegate(Packet P)
            {
                IConnectionWrapper Wrapper;
                if ((Wrapper = myLoginHandler.isLoggedIn(P.UserID)) != null)
                {
                    myLoginHandler.Logout(P.UserID);
                    myMapHandler.Logout(Wrapper);
                }
            }, typeof(DisconnectPacket));

            myServer.AssignProcessor(delegate(Packet P)
            {
                IConnectionWrapper myValue = myLoginHandler.isLoggedIn(P.UserID);
                if (myValue != null)
                {
                    Map myMap = myMapHandler.GetPlayerMap((PlayerData)myValue.MyExternalData);
                    foreach (NetConnection A in myMap.GetPlayerIPs())
                    {
                        PlayerMapConnectionPacket myNewPlayer = new PlayerMapConnectionPacket();
                        myNewPlayer.SetNewPlayerData((PlayerData)myValue.MyExternalData);
                        myNewPlayer.Sender = A;
                        myServer.Send(myNewPlayer, NetDeliveryMethod.ReliableOrdered);
                    }
                }
            }, typeof(MapLoadedPacket));
            myServer.AssignProcessor(delegate(Packet P)
            {
                CharacterCreationPacket Packet = ((CharacterCreationPacket)P);
                IConnectionWrapper myValue     = myLoginHandler.isLoggedIn(Packet.UserID);
                if (myValue != null)
                {
                    ((PlayerData)myValue.MyExternalData).SetAnimation(Packet.GetAnimationInfo());
                    ((PlayerData)myValue.MyExternalData).SetName(Packet.GetName());
                    Map myMap = myMapHandler.GetPlayerMap(((PlayerData)myValue.MyExternalData));
                    foreach (NetConnection A in myMap.GetPlayerIPs())
                    {
                        PlayerMapConnectionPacket myNewPlayer = new PlayerMapConnectionPacket();
                        myNewPlayer.SetNewPlayerData(((PlayerData)myValue.MyExternalData));
                        myNewPlayer.Sender = A;
                        myServer.Send(myNewPlayer, NetDeliveryMethod.ReliableOrdered);
                    }
                }
            }, typeof(CharacterCreationPacket));
        }
        public void LoadProcessors()
        {
            Console.WriteLine("Intializing Update Server Processors...");
            myServer.AssignProcessor(delegate(Packet P)
            {
                LoginUpdatePacket myPacket = (LoginUpdatePacket)P;
                if (myWrapper.VerifyUser(myPacket.Username, myPacket.GetCreationTime(), myPacket.GetSHA()))
                {
                    if (myConnectedClientHashes.ContainsKey(myPacket.Sender))
                    {
                        myConnectedClientHashes.Remove(myPacket.Sender);
                        Console.WriteLine("Double update request from: " + myPacket.Username + " from IP: " + myPacket.Sender.RemoteEndPoint.Address);
                    }
                    Console.WriteLine("Client update succeded for: " + myPacket.Username + " from IP: " + myPacket.Sender.RemoteEndPoint.Address);

                    Dictionary <String, FileHashEntry> myClientHashes = new Dictionary <String, FileHashEntry>();
                    foreach (FileHashEntry A in myPacket.myEntries)
                    {
                        if (!myClientHashes.ContainsKey(A.GetDirectoryStripped()))
                        {
                            myClientHashes.Add(A.GetDirectoryStripped(), A);
                        }
                    }

                    //TODO: restrict files so they can't go out of server directory

                    //Send First file out!
                    foreach (FileHashEntry A in myHash)
                    {
                        String myEntry = A.GetDirectoryStripped();
                        if (myClientHashes.ContainsKey(myEntry) && CompareHash(myClientHashes[myEntry].myHash, A.myHash))
                        {
                            myClientHashes.Remove(myEntry);
                        }
                        else
                        {
                            myClientHashes.Add(A.Location, A);
                        }
                    }
                    if (myClientHashes.Count > 1)
                    {
                        myConnectedClientHashes.Add(myPacket.Sender, myClientHashes);
                        ThreadPool.QueueUserWorkItem(delegate(object State)
                        {
                            String myReturn = "";
                            byte[] Update   = null;
                            var myEnum      = myClientHashes.Keys.GetEnumerator();
                            while (myEnum.MoveNext())
                            {
                                if (!myReader.isQueued(myEnum.Current))
                                {
                                    myReturn = myEnum.Current;
                                    Update   = myReader.OpenReadAll(myEnum.Current);
                                    break;
                                }
                            }
                            if (Update == null)
                            {
                                myReturn = myClientHashes.Keys.First();
                                Update   = myReader.OpenReadAll(myReturn);
                            }
                            FileUpdatePacket myUpdate = new FileUpdatePacket();
                            myUpdate.myBytes          = Update;
                            myUpdate.myEntry          = myClientHashes[myReturn];
                            myUpdate.Sender           = myPacket.Sender;
                            myUpdate.TotalFiles       = myClientHashes.Count;
                            myClientHashes.Remove(myReturn);
                            myServer.Send(myUpdate, NetDeliveryMethod.ReliableOrdered);
                        }, null);
                    }
                    else if (myClientHashes.Count == 1)
                    {
                        ThreadPool.QueueUserWorkItem(delegate(object State)
                        {
                            var myReturn              = myClientHashes.Keys.First();
                            byte[] Update             = myReader.OpenReadAll(myReturn);
                            FileUpdatePacket myUpdate = new FileUpdatePacket();
                            myUpdate.myBytes          = Update;
                            myUpdate.myEntry          = myClientHashes[myReturn];
                            myUpdate.Sender           = myPacket.Sender;
                            myUpdate.TotalFiles       = myClientHashes.Count;
                            myClientHashes.Remove(myReturn);
                            myServer.Send(myUpdate, NetDeliveryMethod.ReliableOrdered);
                        }, null);
                    }
                    else
                    {
                        ThreadPool.QueueUserWorkItem(delegate(object State)
                        {
                            FileUpdatePacket myUpdate = new FileUpdatePacket();
                            myUpdate.Sender           = myPacket.Sender;
                            myUpdate.TotalFiles       = 0;
                            myServer.Send(myUpdate, NetDeliveryMethod.ReliableOrdered);
                        }, null);
                    }
                }
                else
                {
                    LoginFailedPacket myFailPacket = new LoginFailedPacket();
                    myFailPacket.Sender            = myPacket.Sender;
                    myServer.Send(myFailPacket, NetDeliveryMethod.ReliableOrdered);
                }
            }, typeof(LoginUpdatePacket));

            myServer.AssignProcessor(delegate(Packet P)
            {
                var G = (DisconnectPacket)P;
                if (G.UserID != null)
                {
                    if (myConnectedClientHashes.ContainsKey(G.Sender))
                    {
                        var Result = myConnectedClientHashes[G.Sender];
                        //TODO: add more security here imo.
                        myConnectedClientHashes.Remove(G.Sender);
                    }
                }
            }, typeof(DisconnectPacket));

            myServer.AssignProcessor(delegate(Packet P)
            {
                GetNextFilePacket G = (GetNextFilePacket)P;
                if (myConnectedClientHashes.ContainsKey(G.Sender))
                {
                    var myClient = myConnectedClientHashes[G.Sender];
                    if (myClient.Count > 0)
                    {
                        ThreadPool.QueueUserWorkItem(delegate(object State)
                        {
                            var myReturn              = myClient.Values.First();
                            byte[] Update             = myReader.OpenReadAll(Path.Combine("./Update", myReturn.GetDirectoryStripped()));
                            FileUpdatePacket myUpdate = new FileUpdatePacket();
                            myUpdate.myBytes          = Update;
                            myUpdate.myEntry          = myReturn;
                            myUpdate.Sender           = G.Sender;
                            myUpdate.TotalFiles       = myClient.Count;
                            myClient.Remove(myReturn.Location);
                            myServer.Send(myUpdate, NetDeliveryMethod.ReliableOrdered);
                        }, null);
                    }
                    else
                    {
                        myConnectedClientHashes.Remove(G.Sender);
                    }
                }
            }, typeof(GetNextFilePacket));
            myServer.OnDisconnect += Disconnected;
        }