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

原文:C#高性能大容量SOCKET并发(十一):编写上传客户端

客户端封装整体框架

客户端编程基于阻塞同步模式,只有数据正常发送或接收才返回,如果发生错误则抛出异常,基于TcpClient进行封装,主要类结构如下图:

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

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

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

DynamicBufferManager m_sendBuffer:用于把命令和数据同时写入到缓存中,调用一次发送,这样服务器就只会产生一次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,向服务器请求上传的文件,如果服务器有相同的文件,则返回是否传完,如果未传完,返回需要续传的文件位置,然后客户端则从上一个位置开始传输,传输数据服务器只接收,不应答,客户端传输完后,发完成(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#完成端口编程,仅用于学习和研究,切勿用于商业用途。水平有限,C#也属于初学,错误在所难免,欢迎指正和指导。邮箱地址:fansheng_hx@163.com。

上一篇:django实现文件上传(最简单的方法)


下一篇:C#高性能大容量SOCKET并发(五):粘包、分包、解包