Ejemplo n.º 1
0
        /// <summary>
        /// Receives the PROMPT message and responds with an ACK message, then prepares the transfer socket to receive data
        /// </summary>
        private void BeginReceiveFile()
        {
            byte[] filetransferheader = null;
            int index = 0;

            // Read in 256 bytes, PROMPT type and blank cookie

            Logging.WriteString("In BeginReceiveFile()");

            filetransferheader = new byte[256];
            try
            {
                while (index < filetransferheader.Length)
                {
                    index += socket.Read(filetransferheader, index, filetransferheader.Length - index);
                }
            }
            catch (Exception ex)
            {
                string message = "Error negotiating file transfer:"
                                 + Environ.NewLine + ex.Message;
                CancelFileTransfer(message);
            }

            Logging.WriteString("Got file transfer header");

            using (ByteStream bstream = new ByteStream(filetransferheader))
            {
                bstream.AdvanceToPosition(8);
                bstream.ReadFileTransferInformation(this);
            }

            // Respond with the same header, but with the ACK type and the ICBM cookie set
            index = 6;
            Marshal.InsertUshort(filetransferheader, 0x0202, ref index);
            Marshal.CopyArray(Cookie.ToByteArray(), filetransferheader, 0, ref index);

            Logging.WriteString("Rewrote file transfer header");

            index = 0;
            try
            {
                while (index < filetransferheader.Length)
                {
                    index += socket.Write(filetransferheader, index, filetransferheader.Length - index);
                }
            }
            catch (Exception ex)
            {
                string message = "Error negotiating file transfer:"
                                 + Environ.NewLine + ex.Message;
                CancelFileTransfer(message);
            }

            // Open the file for writing
            try
            {
                _datachunk = new byte[8192];
                _datastream = (new StreamWriter(LocalFileName, false)).BaseStream;
                _streamposition = 0;
            }
            catch (Exception)
            {
                throw new Exception("Can't open target file for writing");
            }

            Logging.WriteString("File opened for writing");

            // Signal the parent session that the file transfer has started
            parent.OnFileTransferProgress(Cookie, 0, FileHeader.Size);

            // Start receiving data
            socket.BeginRead(_datachunk, 0, _datachunk.Length, new AsyncCallback(SendFileTransferReceive), null);
            //socket.BeginReceive(_datachunk, 0, _datachunk.Length, SocketFlags.None, new AsyncCallback(SendFileTransferReceive), null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Receives the PROMPT message and responds with an ACK message, then prepares the transfer socket to receive data
        /// </summary>
        private void BeginReceiveFile()
        {
            byte[] filetransferheader = null;
            int    index = 0;

            // Read in 256 bytes, PROMPT type and blank cookie

            Logging.WriteString("In BeginReceiveFile()");

            filetransferheader = new byte[256];
            try
            {
                while (index < filetransferheader.Length)
                {
                    index += socket.Read(filetransferheader, index, filetransferheader.Length - index);
                }
            }
            catch (Exception ex)
            {
                string message = "Error negotiating file transfer:"
                                 + Environ.NewLine + ex.Message;
                CancelFileTransfer(message);
            }

            Logging.WriteString("Got file transfer header");

            using (ByteStream bstream = new ByteStream(filetransferheader))
            {
                bstream.AdvanceToPosition(8);
                bstream.ReadFileTransferInformation(this);
            }

            // Respond with the same header, but with the ACK type and the ICBM cookie set
            index = 6;
            Marshal.InsertUshort(filetransferheader, 0x0202, ref index);
            Marshal.CopyArray(Cookie.ToByteArray(), filetransferheader, 0, ref index);

            Logging.WriteString("Rewrote file transfer header");

            index = 0;
            try
            {
                while (index < filetransferheader.Length)
                {
                    index += socket.Write(filetransferheader, index, filetransferheader.Length - index);
                }
            }
            catch (Exception ex)
            {
                string message = "Error negotiating file transfer:"
                                 + Environ.NewLine + ex.Message;
                CancelFileTransfer(message);
            }

            // Open the file for writing
            try
            {
                _datachunk      = new byte[8192];
                _datastream     = (new StreamWriter(LocalFileName, false)).BaseStream;
                _streamposition = 0;
            }
            catch (Exception)
            {
                throw new Exception("Can't open target file for writing");
            }

            Logging.WriteString("File opened for writing");

            // Signal the parent session that the file transfer has started
            parent.OnFileTransferProgress(Cookie, 0, FileHeader.Size);

            // Start receiving data
            socket.BeginRead(_datachunk, 0, _datachunk.Length, new AsyncCallback(SendFileTransferReceive), null);
            //socket.BeginReceive(_datachunk, 0, _datachunk.Length, SocketFlags.None, new AsyncCallback(SendFileTransferReceive), null);
        }