C#高性能大容量SOCKET并发(十一):编写上传client

client封装总体框架

client编程基于堵塞同步模式,仅仅有数据正常发送或接收才返回,假设错误发生则抛出异常,基于TcpClient进行封装,主要类结构例如以下图:


TcpClient:NET系统封装,实现了底层Socket操作,提供了堵塞和非堵塞调用;

OutgoingDataAssembler m_outgoingDataAssembler:协议组装器,用来组装往外发送的命令,主要用于组装协议格式;

DynamicBufferManager m_sendBuffer:用于把命令和数据同一时候写入到缓存中,调用一次发送,这样server就仅仅会产生一次IOCP回调,能够提高性能;

IncomingDataParser m_incomingDataParser:收到数据的解析器,用于解析返回的内容,主要是解析文本格式;

protected DynamicBufferManager m_recvBuffer:接收数据的缓存,数据存到缓存中后,能够解析命令和数据;

TcpClient说明,堵塞和非堵塞

TcpClient封装了NET的底层Socket操作,基于TCP协议,提供了堵塞和非堵塞模式调用,详细是设置m_tcpClient.Client.Blocking = true表示使用堵塞模式,反之则使用非堵塞模式。堵塞模式表示接收完指定长度的数据才返回,非堵塞模式表示收到一点数据就返回。

如我们调用m_tcpClient.Client.Receive(m_recvBuffer.Buffer, sizeof(int), packetLength, SocketFlags.None),如果传入的长度为1024,堵塞模式一点要等到数据达到1024长度才返回,否则一直等待Socket超时或者链路断了,非堵塞模式则不同,增加收到8字节了,则返回调用者,调用者使用循环继续接受1024-8=1016的数据。

发送命令

发送数据和服务端同样,主要是对数据进行组包,然后调用发送函数发送,详细代码例如以下:

        public void SendCommand(byte[] buffer, int offset, int count)
        {
            string commandText = m_outgoingDataAssembler.GetProtocolText();
            byte[] bufferUTF8 = Encoding.UTF8.GetBytes(commandText);
            int totalLength = sizeof(int) + bufferUTF8.Length + count; //获取总大小
            m_sendBuffer.Clear();
            m_sendBuffer.WriteInt(totalLength, false); //写入总大小
            m_sendBuffer.WriteInt(bufferUTF8.Length, false); //写入命令大小
            m_sendBuffer.WriteBuffer(bufferUTF8); //写入命令内容
            m_sendBuffer.WriteBuffer(buffer, offset, count); //写入二进制数据
            m_tcpClient.Client.Send(m_sendBuffer.Buffer, 0, m_sendBuffer.DataCount, SocketFlags.None);
        }

接收命令

接收命令和发送相反,先接收长度,然后接收内容,然后对数据进行解包,详细代码例如以下:

        public bool RecvCommand(out byte[] buffer, out int offset, out int size)
        {
            m_recvBuffer.Clear();
            m_tcpClient.Client.Receive(m_recvBuffer.Buffer, sizeof(int), SocketFlags.None);
            int packetLength = BitConverter.ToInt32(m_recvBuffer.Buffer, 0); //获取包长度
            if (NetByteOrder)
                packetLength = System.Net.IPAddress.NetworkToHostOrder(packetLength); //把网络字节顺序转为本地字节顺序
            m_recvBuffer.SetBufferSize(sizeof(int) + packetLength); //保证接收有足够的空间
            m_tcpClient.Client.Receive(m_recvBuffer.Buffer, sizeof(int), packetLength, SocketFlags.None);
            int commandLen = BitConverter.ToInt32(m_recvBuffer.Buffer, sizeof(int)); //取出命令长度
            string tmpStr = Encoding.UTF8.GetString(m_recvBuffer.Buffer, sizeof(int) + sizeof(int), commandLen);
            if (!m_incomingDataParser.DecodeProtocolText(tmpStr)) //解析命令
            {
                buffer = null;
                offset = 0;
                size = 0;
                return false;
            }
            else
            {
                buffer = m_recvBuffer.Buffer;
                offset = commandLen + sizeof(int) + sizeof(int);
                size = packetLength - offset;
                return true;
            }
        }


命令交互

封装了底层Socket操作和协议解析后,实现一个命令交互如登录代码例如以下:

        public bool DoLogin(string userName, string password)
        {
            try
            {
                m_outgoingDataAssembler.Clear();
                m_outgoingDataAssembler.AddRequest();
                m_outgoingDataAssembler.AddCommand(AsyncSocketServer.ProtocolKey.Login);
                m_outgoingDataAssembler.AddValue(AsyncSocketServer.ProtocolKey.UserName, userName);
                m_outgoingDataAssembler.AddValue(AsyncSocketServer.ProtocolKey.Password, AsyncSocketServer.BasicFunc.MD5String(password));
                SendCommand();
                bool bSuccess = RecvCommand();
                if (bSuccess)
                {
                    bSuccess = CheckErrorCode();
                    if (bSuccess)
                    {
                        m_userName = userName;
                        m_password = password;
                    }
                    return bSuccess;
                }
                else
                    return false;
            }
            catch (Exception E)
            {
                //记录日志
                m_errorString = E.Message;
                return false;
            }
        }

上传协议

上传协议主要分为三个命令,第一个是Upload,向server请求上传的文件,假设server有同样的文件,则返回是否传完,假设未传完,返回须要续传的文件位置,然后client则从上一个位置開始传输,数据传输server仅仅接收,不应答,client传输完后,发完毕(EOF)命令。因此三个命令封装代码例如以下:

        public bool DoUpload(string dirName, string fileName, ref long fileSize)
        {
            bool bConnect = ReConnectAndLogin(); //检測连接是否还在,假设断开则重连并登录
            if (!bConnect)
                return bConnect;
            try
            {
                m_outgoingDataAssembler.Clear();
                m_outgoingDataAssembler.AddRequest();
                m_outgoingDataAssembler.AddCommand(AsyncSocketServer.ProtocolKey.Upload);
                m_outgoingDataAssembler.AddValue(AsyncSocketServer.ProtocolKey.DirName, dirName);
                m_outgoingDataAssembler.AddValue(AsyncSocketServer.ProtocolKey.FileName, fileName);
                SendCommand();
                bool bSuccess = RecvCommand();
                if (bSuccess)
                {
                    bSuccess = CheckErrorCode();
                    if (bSuccess)
                    {
                        bSuccess = m_incomingDataParser.GetValue(AsyncSocketServer.ProtocolKey.FileSize, ref fileSize);
                    }
                    return bSuccess;
                }
                else
                    return false;
            }
            catch (Exception E)
            {
                //记录日志
                m_errorString = E.Message;
                return false;
            }
        }

        public bool DoData(byte[] buffer, int offset, int count)
        {
            try
            {
                m_outgoingDataAssembler.Clear();
                m_outgoingDataAssembler.AddRequest();
                m_outgoingDataAssembler.AddCommand(AsyncSocketServer.ProtocolKey.Data);
                SendCommand(buffer, offset, count);
                return true;
            }
            catch (Exception E)
            {
                //记录日志
                m_errorString = E.Message;
                return false;
            }
        }

        public bool DoEof(Int64 fileSize)
        {
            try
            {
                m_outgoingDataAssembler.Clear();
                m_outgoingDataAssembler.AddRequest();
                m_outgoingDataAssembler.AddCommand(AsyncSocketServer.ProtocolKey.Eof);
                SendCommand();
                bool bSuccess = RecvCommand();
                if (bSuccess)
                    return CheckErrorCode();
                else
                    return false;
            }
            catch (Exception E)
            {
                //记录日志
                m_errorString = E.Message;
                return false;
            }
        }
调用过程:

        protected static bool SendFile(string fileName, ClientUploadSocket uploadSocket)
        {
            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite);
            try
            {
                try
                {
                    long fileSize = 0;
                    if (!uploadSocket.DoUpload("", Path.GetFileName(fileName), ref fileSize))
                        throw new Exception(uploadSocket.ErrorString);
                    fileStream.Position = fileSize;
                    byte[] readBuffer = new byte[PacketSize];
                    while (fileStream.Position < fileStream.Length)
                    {
                        int count = fileStream.Read(readBuffer, 0, PacketSize);
                        if (!uploadSocket.DoData(readBuffer, 0, count))
                            throw new Exception(uploadSocket.ErrorString);
                    }
                    if (!uploadSocket.DoEof(fileStream.Length))
                        throw new Exception(uploadSocket.ErrorString);
                    return true;
                }
                catch (Exception E)
                {
                    Console.WriteLine("Upload File Error: " + E.Message);
                    return false;
                }
            }
            finally
            {
                fileStream.Close();
            }
        }

DEMO下载地址:http://download.csdn.net/detail/sqldebug_fan/7467745
免责声明:此代码仅仅是为了演示C#完毕port编程,仅用于学习和研究,切勿用于商业用途。水平有限,C#也属于初学,错误在所难免,欢迎指正和指导。邮箱地址:fansheng_hx@163.com。

原文地址:https://www.cnblogs.com/zfyouxi/p/4022372.html