C# High Performance Large Capacity SOCKET Concurrency (8): Communication Protocol

Type of agreement

There are two types of protocols for developing Socket programs. One is described in text, similar to HTTP protocol, which defines character set. The advantage is compatibility and convenient debugging. , defines the byte order, the advantage is high performance, the disadvantage is compatibility and inconvenient debugging. This can be flexibly selected according to the application scenario. If your application is relatively stable, requires few changes, and has high performance requirements, you can use the code plus structure method. If your application requires constant expansion, but is not critical to performance, you can use text parsing. There are two typical application scenarios for these two protocols. Code plus structure is more used in middleware, because the encapsulation of the protocol is transparent, does not require joint debugging, and has high performance requirements; text parsing is more Applications in external interactions, such as communication with devices and mobile phones, require joint debugging, but the performance requirements are not so high.

Our Demo adopts the method of text parsing, which can be flexibly selected according to the application.

Defining the protocol has the following points of attention (convenient for different platforms to access).

byte order

Under different hardware platforms or operating systems, the byte order is inconsistent, and some have the high order first, the low order last, and some have the low order first. Windows is low-order first, high-order last, and each platform has a function to implement byte conversion. The byte order defined by TCP/IP is high-order first and low-order last. You can use the IPAddress class

[csharp] view plain copy
print ?
  1. //  
  2. // Summary:  
  3. // Convert the integer value from network byte order to host byte order.  
  4. //  
  5. // Parameters:  
  6. //   network:  
  7. // The number to convert in network byte order.  
  8. //  
  9. // return result:  
  10. // Integer value in host byte order.  
  11. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]  
  12. publicstaticint NetworkToHostOrder(int network);    
  13. //  
  14. // Summary:  
  15. // Convert the long value from network byte order to host byte order.  
  16. //  
  17. // Parameters:  
  18. //   network:  
  19. // The number to convert in network byte order.  
  20. //  
  21. // return result:  
  22. // A long value in host byte order.  
  23. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]  
  24. publicstaticlong NetworkToHostOrder(long network);    
  25. //  
  26. // Summary:  
  27. // Convert short values ​​from network byte order to host byte order.  
  28. //  
  29. // Parameters:  
  30. //   network:  
  31. // The number to convert in network byte order.  
  32. //  
  33. // return result:  
  34. // A short value represented in host byte order.  
  35. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]  
  36. publicstaticshort NetworkToHostOrder(short network);    
        //
        // Summary:
        // Convert the integer value from network byte order to host byte order.
        //
        // Parameters:
        //   network:
        // The number to convert in network byte order.
        //
        // return result:
        // Integer value in host byte order.
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public static int NetworkToHostOrder(int network);
        //
        // Summary:
        // Convert the long value from network byte order to host byte order.
        //
        // Parameters:
        //   network:
        // The number to convert in network byte order.
        //
        // return result:
        // A long value in host byte order.
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public static long NetworkToHostOrder(long network);
        //
        // Summary:
        // Convert short values ​​from network byte order to host byte order.
        //
        // Parameters:
        //   network:
        // The number to convert in network byte order.
        //
        // return result:
        // A short value represented in host byte order.
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public static short NetworkToHostOrder(short network);

To convert the network byte order to the local byte order, and vice versa, you can call

[csharp] view plain copy
print ?
  1. //  
  2. // Summary:  
  3. // Convert the integer value from host byte order to network byte order.  
  4. //  
  5. // Parameters:  
  6. //   host:  
  7. // The number to convert in host byte order.  
  8. //  
  9. // return result:  
  10. // Integer value in network byte order.  
  11. publicstaticint HostToNetworkOrder(int host);    
  12. //  
  13. // Summary:  
  14. // Convert the long value from host byte order to network byte order.  
  15. //  
  16. // Parameters:  
  17. //   host:  
  18. // The number to convert in host byte order.  
  19. //  
  20. // return result:  
  21. // A long value in network byte order.  
  22. publicstaticlong HostToNetworkOrder(long host);    
  23. //  
  24. // Summary:  
  25. // Convert short values ​​from host byte order to network byte order.  
  26. //  
  27. // Parameters:  
  28. //   host:  
  29. // The number to convert in host byte order.  
  30. //  
  31. // return result:  
  32. // A short value in network byte order.  
  33. publicstaticshort HostToNetworkOrder(short host);    
        //
        // Summary:
        // Convert the integer value from host byte order to network byte order.
        //
        // Parameters:
        //   host:
        // The number to convert in host byte order.
        //
        // return result:
        // Integer value in network byte order.
        public static int HostToNetworkOrder(int host);
        //
        // Summary:
        // Convert the long value from host byte order to network byte order.
        //
        // Parameters:
        //   host:
        // The number to convert in host byte order.
        //
        // return result:
        // A long value in network byte order.
        public static long HostToNetworkOrder(long host);
        //
        // Summary:
        // Convert short values ​​from host byte order to network byte order.
        //
        // Parameters:
        //   host:
        // The number to convert in host byte order.
        //
        // return result:
        // A short value in network byte order.
        public static short HostToNetworkOrder(short host);

to convert local byte order to network byte order.

A more popular approach is to use network byte order, so that the specification is uniform. We use the Windows byte order here, that is, the low order is first and the high order is last, which is just the opposite of the network byte order.

character set

The most suitable character set is to use UTF-8. This encoding is free. For some embedded systems that do not support Chinese, all English can be converted to UTF-8 format without conversion, which has advantages for cross-platform.

packet format

The two protocol styles of Code plus structure and text analysis all use the same data packet format, that is, a 4-byte length is sent first, followed by the content, and the next one is also a 4-byte length first, followed by the content. . The structure can be defined as a file stream structure with byte alignment.

Protocol sample

Active: detect connection (heartbeat packet)

client -> server
{
[Request]
Command=Active
}
Server->Client
{
[Response]
Command= Active
Code= Error Code#error code
Message=Message#If there is an error, return the error description information
}


DEMO download address: http://download.csdn.net/detail/sqldebug_fan/7467745
Disclaimer: This code is only to demonstrate C# completion port programming, only for learning and research, not for commercial use. The level is limited, and C# is also a beginner. Errors are inevitable. Corrections and guidance are welcome. Email address: [email protected].

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325992220&siteId=291194637