Exemple #1
0
        private void HandleTcpRequest(PatchClient patchClient)
        {
            while (MessageFormatter.Connected(patchClient.client))
            {
                try {
                    List <string> filesToDownload;
                    if (patchClient.client.GetStream().DataAvailable)
                    {
                        if (patchClient.fileList == null)
                        {
                            patchClient.fileList = JsonConvert.DeserializeObject <Dictionary <string, string> >(MessageFormatter.ReadStreamOnce(patchClient.client.GetStream()));
                            Console.WriteLine("Filelist received!");

                            Console.WriteLine("Comparing files to master list");
                            filesToDownload = FileChecker.CompareFileDictionaries(masterFiles, patchClient.fileList);

                            Console.WriteLine("Missing files on client:");
                            foreach (var item in filesToDownload)
                            {
                                Console.WriteLine(item);
                            }

                            FileTransferModel fileTransferModel = GenerateFileTransferModel(filesToDownload, masterFilesPath);

                            Console.WriteLine("Sending missing files list to client");
                            byte [] modelData = MessageFormatter.MessageBytes(fileTransferModel);
                            patchClient.client.GetStream().Write(modelData, 0, modelData.Length);
                            Console.WriteLine("Files list sent");
                        }
                        else
                        {
                            string   fileToSend = MessageFormatter.ReadStreamOnce(patchClient.client.GetStream());
                            FileInfo fi         = new FileInfo(masterFilesPath + '/' + fileToSend);
                            Console.WriteLine("{0} size: {1}", fi.Name, fi.Length);
                            byte [] preBuffer = BitConverter.GetBytes((int)fi.Length);
                            patchClient.client.Client.SendFile(fi.FullName, preBuffer, null, TransmitFileOptions.UseDefaultWorkerThread);
                            Console.WriteLine("{0} sent", fi.Name);
                        }
                    }
                }
                catch (Exception e) {
                    Console.WriteLine("Error: " + e.Message);
                }
            }

            lock (clients)
                clients.Remove(patchClient);

            Console.WriteLine("{0} disconnected!", patchClient.client.Client.RemoteEndPoint.ToString());
            Console.WriteLine("Currently {0} other connected clients!", clients.Count);
        }
Exemple #2
0
        private void HandleConnections()
        {
            TcpListener listener = new TcpListener(IPAddress.Any, GlobalVariables.PATCHMANAGER_PORT);

            listener.Start();
            while (true)
            {
                TcpClient client = listener.AcceptTcpClient();

                Console.WriteLine("Connected!");

                lock (clients) {
                    PatchClient patchClient = new PatchClient(client);
                    clients.Add(patchClient);
                    Thread t = new Thread(() => HandleTcpRequest(patchClient));
                    t.IsBackground = true;
                    t.Start();
                }
            }
        }