Esempio n. 1
0
        private void LoginButton_Click(object sender, EventArgs e)
        {
            String Password = PasswordText.Text;

            Username = UsernameTextBox.Text;
            LoginUpdatePacket myLogin = new LoginUpdatePacket();

            myLogin.CreateUsernamePasswordHash(Username, Password);
            Directory.CreateDirectory("./Game");
            if (myHash != null && myHash.Count != 0)
            {
                myHash.Clear();
            }
            RecurseIntoDirectory("./Game");
            myLogin.myEntries             = myHash;
            myAction3                     = new Action(OnConnected);
            myServerConnection.OnConnect += LidgrenConnectedCallback;
            myAction4                     = new Action(OnDisconnected);
            myServerConnection.Send(myLogin, Lidgren.Network.NetDeliveryMethod.ReliableOrdered);
            Status.Text = "Connecting!";
        }
        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;
        }