zoukankan      html  css  js  c++  java
  • C#高性能大容量SOCKET并发(十一):编写上传客户端

    客户端封装整体框架

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

    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的数据。

    发送命令

    发送数据和服务端相同,主要是对数据进行组包,然后调用发送函数发送,具体代码如下:

    [csharp] view plain copy
     
    1. public void SendCommand(byte[] buffer, int offset, int count)  
    2. {  
    3.     string commandText = m_outgoingDataAssembler.GetProtocolText();  
    4.     byte[] bufferUTF8 = Encoding.UTF8.GetBytes(commandText);  
    5.     int totalLength = sizeof(int) + bufferUTF8.Length + count; //获取总大小  
    6.     m_sendBuffer.Clear();  
    7.     m_sendBuffer.WriteInt(totalLength, false); //写入总大小  
    8.     m_sendBuffer.WriteInt(bufferUTF8.Length, false); //写入命令大小  
    9.     m_sendBuffer.WriteBuffer(bufferUTF8); //写入命令内容  
    10.     m_sendBuffer.WriteBuffer(buffer, offset, count); //写入二进制数据  
    11.     m_tcpClient.Client.Send(m_sendBuffer.Buffer, 0, m_sendBuffer.DataCount, SocketFlags.None);  
    12. }  

    接收命令

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

    [csharp] view plain copy
     
    1. public bool RecvCommand(out byte[] buffer, out int offset, out int size)  
    2. {  
    3.     m_recvBuffer.Clear();  
    4.     m_tcpClient.Client.Receive(m_recvBuffer.Buffer, sizeof(int), SocketFlags.None);  
    5.     int packetLength = BitConverter.ToInt32(m_recvBuffer.Buffer, 0); //获取包长度  
    6.     if (NetByteOrder)  
    7.         packetLength = System.Net.IPAddress.NetworkToHostOrder(packetLength); //把网络字节顺序转为本地字节顺序  
    8.     m_recvBuffer.SetBufferSize(sizeof(int) + packetLength); //保证接收有足够的空间  
    9.     m_tcpClient.Client.Receive(m_recvBuffer.Buffer, sizeof(int), packetLength, SocketFlags.None);  
    10.     int commandLen = BitConverter.ToInt32(m_recvBuffer.Buffer, sizeof(int)); //取出命令长度  
    11.     string tmpStr = Encoding.UTF8.GetString(m_recvBuffer.Buffer, sizeof(int) + sizeof(int), commandLen);  
    12.     if (!m_incomingDataParser.DecodeProtocolText(tmpStr)) //解析命令  
    13.     {  
    14.         buffer = null;  
    15.         offset = 0;  
    16.         size = 0;  
    17.         return false;  
    18.     }  
    19.     else  
    20.     {  
    21.         buffer = m_recvBuffer.Buffer;  
    22.         offset = commandLen + sizeof(int) + sizeof(int);  
    23.         size = packetLength - offset;  
    24.         return true;  
    25.     }  
    26. }  

    命令交互

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

    [csharp] view plain copy
     
    1. public bool DoLogin(string userName, string password)  
    2. {  
    3.     try  
    4.     {  
    5.         m_outgoingDataAssembler.Clear();  
    6.         m_outgoingDataAssembler.AddRequest();  
    7.         m_outgoingDataAssembler.AddCommand(AsyncSocketServer.ProtocolKey.Login);  
    8.         m_outgoingDataAssembler.AddValue(AsyncSocketServer.ProtocolKey.UserName, userName);  
    9.         m_outgoingDataAssembler.AddValue(AsyncSocketServer.ProtocolKey.Password, AsyncSocketServer.BasicFunc.MD5String(password));  
    10.         SendCommand();  
    11.         bool bSuccess = RecvCommand();  
    12.         if (bSuccess)  
    13.         {  
    14.             bSuccess = CheckErrorCode();  
    15.             if (bSuccess)  
    16.             {  
    17.                 m_userName = userName;  
    18.                 m_password = password;  
    19.             }  
    20.             return bSuccess;  
    21.         }  
    22.         else  
    23.             return false;  
    24.     }  
    25.     catch (Exception E)  
    26.     {  
    27.         //记录日志  
    28.         m_errorString = E.Message;  
    29.         return false;  
    30.     }  
    31. }  

    上传协议

    上传协议主要分为三个命令,第一个是Upload,向服务器请求上传的文件,如果服务器有相同的文件,则返回是否传完,如果未传完,返回需要续传的文件位置,然后客户端则从上一个位置开始传输,传输数据服务器只接收,不应答,客户端传输完后,发完成(EOF)命令。因此三个命令封装代码如下:

    [csharp] view plain copy
     
    1. public bool DoUpload(string dirName, string fileName, ref long fileSize)  
    2. {  
    3.     bool bConnect = ReConnectAndLogin(); //检测连接是否还在,如果断开则重连并登录  
    4.     if (!bConnect)  
    5.         return bConnect;  
    6.     try  
    7.     {  
    8.         m_outgoingDataAssembler.Clear();  
    9.         m_outgoingDataAssembler.AddRequest();  
    10.         m_outgoingDataAssembler.AddCommand(AsyncSocketServer.ProtocolKey.Upload);  
    11.         m_outgoingDataAssembler.AddValue(AsyncSocketServer.ProtocolKey.DirName, dirName);  
    12.         m_outgoingDataAssembler.AddValue(AsyncSocketServer.ProtocolKey.FileName, fileName);  
    13.         SendCommand();  
    14.         bool bSuccess = RecvCommand();  
    15.         if (bSuccess)  
    16.         {  
    17.             bSuccess = CheckErrorCode();  
    18.             if (bSuccess)  
    19.             {  
    20.                 bSuccess = m_incomingDataParser.GetValue(AsyncSocketServer.ProtocolKey.FileSize, ref fileSize);  
    21.             }  
    22.             return bSuccess;  
    23.         }  
    24.         else  
    25.             return false;  
    26.     }  
    27.     catch (Exception E)  
    28.     {  
    29.         //记录日志  
    30.         m_errorString = E.Message;  
    31.         return false;  
    32.     }  
    33. }  
    34.   
    35. public bool DoData(byte[] buffer, int offset, int count)  
    36. {  
    37.     try  
    38.     {  
    39.         m_outgoingDataAssembler.Clear();  
    40.         m_outgoingDataAssembler.AddRequest();  
    41.         m_outgoingDataAssembler.AddCommand(AsyncSocketServer.ProtocolKey.Data);  
    42.         SendCommand(buffer, offset, count);  
    43.         return true;  
    44.     }  
    45.     catch (Exception E)  
    46.     {  
    47.         //记录日志  
    48.         m_errorString = E.Message;  
    49.         return false;  
    50.     }  
    51. }  
    52.   
    53. public bool DoEof(Int64 fileSize)  
    54. {  
    55.     try  
    56.     {  
    57.         m_outgoingDataAssembler.Clear();  
    58.         m_outgoingDataAssembler.AddRequest();  
    59.         m_outgoingDataAssembler.AddCommand(AsyncSocketServer.ProtocolKey.Eof);  
    60.         SendCommand();  
    61.         bool bSuccess = RecvCommand();  
    62.         if (bSuccess)  
    63.             return CheckErrorCode();  
    64.         else  
    65.             return false;  
    66.     }  
    67.     catch (Exception E)  
    68.     {  
    69.         //记录日志  
    70.         m_errorString = E.Message;  
    71.         return false;  
    72.     }  
    73. }  
    调用过程:
    [csharp] view plain copy
     
    1. protected static bool SendFile(string fileName, ClientUploadSocket uploadSocket)  
    2. {  
    3.     FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite);  
    4.     try  
    5.     {  
    6.         try  
    7.         {  
    8.             long fileSize = 0;  
    9.             if (!uploadSocket.DoUpload("", Path.GetFileName(fileName), ref fileSize))  
    10.                 throw new Exception(uploadSocket.ErrorString);  
    11.             fileStream.Position = fileSize;  
    12.             byte[] readBuffer = new byte[PacketSize];  
    13.             while (fileStream.Position < fileStream.Length)  
    14.             {  
    15.                 int count = fileStream.Read(readBuffer, 0, PacketSize);  
    16.                 if (!uploadSocket.DoData(readBuffer, 0, count))  
    17.                     throw new Exception(uploadSocket.ErrorString);  
    18.             }  
    19.             if (!uploadSocket.DoEof(fileStream.Length))  
    20.                 throw new Exception(uploadSocket.ErrorString);  
    21.             return true;  
    22.         }  
    23.         catch (Exception E)  
    24.         {  
    25.             Console.WriteLine("Upload File Error: " + E.Message);  
    26.             return false;  
    27.         }  
    28.     }  
    29.     finally  
    30.     {  
    31.         fileStream.Close();  
    32.     }  
    33. }  

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

    http://blog.csdn.net/sqldebug_fan/article/details/29391551

  • 相关阅读:
    Python进阶03 模块
    Python进阶02 文本文件的输入输出
    Python进阶01 词典
    Python基础10 反过头来看看
    Python基础09 面向对象的进一步拓展
    Python基础08 面向对象的基本概念
    Python基础07 函数
    Vuex源码分析(转)
    Vue2.x双向数据绑定
    Angular2的双向数据绑定
  • 原文地址:https://www.cnblogs.com/findumars/p/5850825.html
Copyright © 2011-2022 走看看