Esempio n. 1
0
File: Server.cs Progetto: sys27/SRF
        private void send_DoWork(object o, DoWorkEventArgs args)
        {
            IsBusy = true;
            Socket handler = null;
            SRFMessage message = null;
            xmlfs = null;

            byte[] buf = new byte[BUFFER_SIZE];
            int count = 0;

            try
            {
                xmlfs = (XMLFileSystem)args.Argument;

                FilesCount = xmlfs.FilesCount;
                FullSize = xmlfs.FullSize;

                Action = Resource.BeginSending;

                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Bind(endPoint);
                socket.Listen(0);

                Action = Resource.WaitConnection;
                handler = socket.Accept();

                message = new SRFMessage() { Command = Commands.Hello };
                handler.Send(message.Message);

                handler.Receive(message.Message);
                if (message.Command != Commands.OK)
                    throw new Exception(Resource.ERROR01);

                while (message.Command != Commands.End)
                {
                    handler.Receive(message.Message);

                    switch (message.Command)
                    {
                        case Commands.FileSystem:
                            string fsPath = "fs.xml.out.gz";
                            FileName = "File System";
                            CurrentSize = 0;
                            CurrentFullSize = message.CurrentFullSize = new FileInfo(fsPath).Length;
                            handler.Send(message.Message);

                            fs = new FileStream(fsPath, FileMode.Open, FileAccess.Read);
                            bs = new BufferedStream(fs);

                            timer.Start();

                            while ((count = bs.Read(buf, 0, buf.Length)) > 0)
                            {
                                handler.Send(buf, 0, count, 0);

                                CurrentSize += count;

                                TotalProgress = CurrentProgress = Math.Round(currentSize * 100d / currentFullSize, 2);
                            }

                            timer.Stop();
                            old = 0;
                            Speed = 0;

                            bs.Close();
                            fs.Close();

                            handler.Receive(message.Message);
                            if (message.Command != Commands.OK)
                                throw new Exception(Resource.ERROR02);

                            break;
                        case Commands.File:
                            XElement el = xmlfs.SearchFile(message.ID);
                            if (el == null)
                            {
                                message = new SRFMessage() { Command = Commands.Error };
                                handler.Send(message.Message);
                                throw new Exception(Resource.ERROR03);
                            }
                            message = new SRFMessage() { Command = Commands.OK };
                            handler.Send(message.Message);

                            count = 0;
                            currentSize = 0;
                            FileName = el.Attribute("name").Value;
                            CurrentFullSize = long.Parse(el.Attribute("size").Value);
                            CurrentProgress = 0;

                            CurrentFileCount++;

                            fs = new FileStream(el.Attribute("cut").Value + el.Attribute("path").Value, FileMode.Open, FileAccess.Read);
                            bs = new BufferedStream(fs);

                            timer.Start();

                            while ((count = bs.Read(buf, 0, buf.Length)) > 0)
                            {
                                handler.Send(buf, 0, count, 0);

                                CurrentSize += count;
                                Size += count;

                                CurrentProgress = Math.Round(currentSize * 100d / currentFullSize, 2);
                                TotalProgress = Math.Round(size * 100d / fullSize, 2);
                            }

                            timer.Stop();
                            old = 0;
                            Speed = 0;

                            bs.Close();
                            fs.Close();

                            handler.Receive(message.Message);
                            if (message.Command != Commands.OK)
                                throw new Exception(Resource.ERROR04);

                            break;
                        case Commands.End:
                            return;
                        default:
                            break;
                    }

                    Action = Resource.EndSending;
                }
            }
            catch (IOException ioe)
            {
                Action = Resource.Error;
                OnError(ioe);
            }
            catch (SocketException se)
            {
                Action = Resource.Error;
                OnError(se);
            }
            finally
            {
                message = null;
                xmlfs = null;

                if (timer.Enabled)
                    timer.Stop();

                if (bs != null)
                {
                    bs.Close();
                    bs = null;
                }
                if (fs != null)
                {
                    fs.Close();
                    fs = null;
                }
                if (handler != null)
                {
                    if (handler.Connected)
                        handler.Disconnect(false);
                    handler.Close();
                    handler = null;
                }
                if (socket != null)
                {
                    if (socket.Connected)
                        socket.Disconnect(false);
                    socket.Close();
                    socket = null;
                }

                Clear();
                IsBusy = false;
            }
        }