Exemplo n.º 1
0
        /// <summary>
        /// Download a stream from server to a client. Internally, it will also fake an empty request (CStreamSerializationHelper.idReadDataFromServerToClient) on behalf on the client
        /// </summary>
        /// <param name="PeerHandle">A peer socket handle to represent a client</param>
        /// <param name="source">A valid stream at server side</param>
        /// <param name="fileSize">File size in bytes. It will be -1 if there is error</param>
        /// <param name="errMsg">An error message. It will be empty string with zero length if no error is found</param>
        public static void Download(ulong PeerHandle, Stream source, out ulong fileSize, out string errMsg)
        {
            if (source == null)
            {
                fileSize = ulong.MaxValue;
                errMsg   = "Source stream not available";
                return;
            }
            else if (!source.CanRead)
            {
                fileSize = ulong.MaxValue;
                errMsg   = "Source stream not readable";
                return;
            }
            errMsg = "";
            try
            {
                fileSize = (ulong)source.Length;
            }
            catch (Exception)
            {
                fileSize = ulong.MaxValue;
            }

            unsafe
            {
                byte[] temp = null;
                fixed(byte *p = temp)
                {
                    ServerCoreLoader.MakeRequest(PeerHandle, CStreamSerializationHelper.idReadDataFromServerToClient, p, (uint)0);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Download a file from server to a client. Internally, it will also fake an empty request (CStreamSerializationHelper.idReadDataFromServerToClient) on behalf on the client
        /// </summary>
        /// <param name="PeerHandle">A peer socket handle to represent a client</param>
        /// <param name="RemoteFilePath">A path to a file</param>
        /// <param name="fileSize">File size in bytes. It will be -1 if there is error</param>
        /// <param name="errMsg">An error message. It will be empty string with zero length if no error is found</param>
        /// <returns>A file stream</returns>
        public static FileStream DownloadFile(ulong PeerHandle, string RemoteFilePath, out ulong fileSize, out string errMsg)
        {
            FileStream fs = null;

            try
            {
                fs       = new FileStream(RemoteFilePath, FileMode.Open);
                fileSize = (ulong)fs.Length;
                unsafe
                {
                    byte [] temp = null;
                    fixed(byte *p = temp)
                    {
                        ServerCoreLoader.MakeRequest(PeerHandle, CStreamSerializationHelper.idReadDataFromServerToClient, p, (uint)0);
                    }
                }
                errMsg = "";
            }
            catch (Exception err)
            {
                fileSize = ulong.MaxValue;
                errMsg   = err.Message;
            }
            return(fs);
        }