Exemplo n.º 1
0
        private bool DataFromServerToClient(CAsyncServiceHandler sender, ushort reqId, CUQueue qData)
        {
            bool processed = false;

            switch (reqId)
            {
            case CStreamSerializationHelper.idReadDataFromServerToClient:
                if (qData.GetSize() > 0)
                {
                    lock (m_cs)
                    {
                        CStreamSerializationHelper.Write(m_s, qData);
                        if (Progress != null)
                        {
                            Progress.Invoke(this, (ulong)m_s.Position);
                        }
                    }
                    qData.SetSize(0);
                    processed = true;
                }
                break;

            default:
                break;
            }
            return(processed);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Read data from a source stream at server side and send its content onto a client
        /// </summary>
        /// <param name="PeerHandle">A peer socket handle to represent a client</param>
        /// <param name="source">A stream to a source file or other object</param>
        /// <returns>The number of data sent in bytes</returns>
        public static ulong ReadDataFromServerToClient(ulong PeerHandle, Stream source)
        {
            uint  res;
            ulong sent = 0;

            using (CScopeUQueue su = new CScopeUQueue())
            {
                CUQueue q    = su.UQueue;
                uint    read = CStreamSerializationHelper.Read(source, q);
                while (read != 0)
                {
                    unsafe
                    {
                        fixed(byte *p = q.m_bytes)
                        {
                            res = ServerCoreLoader.SendReturnData(PeerHandle, CStreamSerializationHelper.idReadDataFromServerToClient, read, p);
                        }
                    }
                    if (res == CSocketPeer.REQUEST_CANCELED || res == CSocketPeer.SOCKET_NOT_FOUND)
                    {
                        break;
                    }
                    sent += res;
                    q.SetSize(0);
                    read = CStreamSerializationHelper.Read(source, q);
                }
            }
            return(sent);
        }
Exemplo n.º 3
0
        private Stream m_s; //protected by m_cs
        private ulong SendDataFromClientToServer()
        {
            if (m_ash.AttachedClientSocket.BytesInSendingBuffer > CStreamSerializationHelper.STREAM_CHUNK_SIZE)
            {
                return(0);
            }
            ulong send = 0;

            using (CScopeUQueue su = new CScopeUQueue())
            {
                if (m_s == null)
                {
                    return(0);
                }
                uint read = CStreamSerializationHelper.Read(m_s, su.UQueue);
                while (read > 0)
                {
                    bool ok = m_ash.SendRequest(CStreamSerializationHelper.idWriteDataFromClientToServer, su.UQueue.m_bytes, read, (ar) =>
                    {
                        SendDataFromClientToServer();
                    });
                    if (Progress != null)
                    {
                        Progress.Invoke(this, (ulong)m_s.Position);
                    }

                    if (!ok)
                    {
                        m_s = null;
                        break;
                    }
                    send += read;
                    if (m_ash.AttachedClientSocket.BytesInSendingBuffer > 10 * CStreamSerializationHelper.STREAM_CHUNK_SIZE)
                    {
                        break;
                    }
                    read = CStreamSerializationHelper.Read(m_s, su.UQueue);
                    if (read == 0)
                    {
                        if (!m_ash.SendRequest(CStreamSerializationHelper.idUploadCompleted, (dc) =>
                        {
                            lock (m_cs)
                            {
                                if (Progress != null)
                                {
                                    Progress.Invoke(this, (ulong)m_s.Position);
                                }
                                m_s = null;
                            }
                        }))
                        {
                            m_s = null;
                        }
                    }
                }
            }
            return(send);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Write data from client to this server
 /// </summary>
 /// <param name="q">A memory queue containing data from a client</param>
 /// <param name="receiver">A stream at server side to receive data from a client</param>
 public static void WriteDataFromClientToServer(CUQueue q, Stream receiver)
 {
     CStreamSerializationHelper.Write(receiver, q);
 }