In a general communication system, we use a network to transmit a small single information (dozens of bytes, a maximum of several K or dozens of K), but sometimes, the information to be transmitted is very large. For example, a data report generated in the memory may be several Mb in size. We call this large size information "big data block information" (Blob ).
1. Several Methods for sending Blob
In ESFramework/ESPlus, the maximum size of custom information sent is limited. This restriction is due to the maximum size of a single message (100 kb by default, you can use the SetMaxLengthOfMessage static method of GlobalUtil to modify it ). In earlier versions, if you want to send messages of huge size, you can use the following solutions:
(1) modify the default value of the maximum size of a single message allowed by the framework, for example, from kb to 10 MB.
(2) file transfer. The large size information to be sent is first stored in a file, and then transmitted to the recipient using the transfer file function provided by ESPlus.
(3) The sender can manually split the information, send the information fragments one by one, and then splice the received fragments into a complete information on the receiver.
We should try our best to avoid solution (1. We recommend that you do not send a large Blob through a single message. Although the TCP connection allows us to send a large data size to it at a time, it is very dangerous for some systems to do so. Why? Because when the large data size has not been sent, it will be blocked to continue sending any other data on the TCP connection. This means that if a large data size needs to be sent for 30 s, the entire channel will be exclusive to Blob within these 30 s, and no other data will be sent, include emergency command information that may be necessary.
In addition, solution (1) requires the framework to reserve more memory as the data buffer. In some cases, this may be a great waste.
Solution (2) involves hard disk I/O and uses files as a transit, which is not intuitive enough.
Solution (3) is the best. Blob fragments can be sent across other information. However, manual implementation of this solution is complicated. Fortunately, the implementation of this solution has been built in the latest ESPlus 3.0.
II. The sending Blob method provided by ESPlus 3.0
ESPlus 3.0 provides several APIs for sending Blob in the custom information space:
1.ICustomizeOutterSendBlob method: used to send Blob information to other online users or servers on the client.
/// <Summary>
/// Send large data block information to online users or servers. This method will not be returned until the data is sent. If you are worried about blocking the calling thread for a long time, consider calling this method asynchronously.
/// </Summary>
/// <Param name = "targetUserID"> Target User ID for receiving messages. If it is null, the receiver is the server. </Param>
/// <Param name = "informationType"> Custom information type </param>
/// <Param name = "blobInfo"> big data block Information </param>
/// <Param name = "fragmentSize"> Fragment Size During multipart transfer </param>
Void SendBlob (string targetUserID, int informationType, byte [] blobInfo, int fragmentSize );
2.ICustomizeControllerSendBlob method: used to send Blob information to an online user on the server.
/// <Summary>
/// Send big data block information to an online user whose ID is userID. This method will not be returned until the data is sent. If you are worried about blocking the calling thread for a long time, consider calling this method asynchronously.
/// </Summary>
/// <Param name = "userID"> User ID for receiving messages </param>
/// <Param name = "informationType"> Custom information type </param>
/// <Param name = "blobInfo"> big data block Information </param>
/// <Param name = "fragmentSize"> Fragment Size During multipart transfer </param>
Void SendBlob (string userID, int informationType, byte [] blobInfo, int fragmentSize );
Pay attention to the following points when sending Blob information:
(1) Select the appropriate fragmentSize ). The value of the fragmentSize parameter depends on the smooth status of the network and the time required for sending a single fragment.
(2) Both the sender and receiver must regard Blob information as a whole. When a Blob fragment is transmitted, if either the receiver or the sender is offline, the entire Blob information is transmitted. The receiver will never submit incomplete blob information to the custom information processor (ICustomizeHandler) for processing.
(3) The SendBlob method sends Blob fragments one by one in the current call thread. If Blob information is large and you do not want to block the calling thread, use Asynchronous call.
We take the common sending image as an example. Because a larger image may contain several Mb, Blob is usually used for sending. We use the asynchronous call method:
private RapidPassiveEngine rapidPassiveEngine = ...;
public static int ImageInformationType = 101;
public void SendImage(Image img, string destUserID)
{
MemoryStream memoryStream = new MemoryStream() ;
img.Save(memoryStream, img.RawFormat);
byte[] blob = memoryStream.ToArray();
memoryStream.Close();
CbGeneric<byte[], string> cb = new CbGeneric<byte[], string>(this.SendBlobThread);
cb.BeginInvoke(blob, destUserID, null, null);
}
private void SendBlobThread(byte[] blob ,string destUserID)
{
this.rapidPassiveEngine.CustomizeOutter.SendBlob(destUserID, ImageInformationType, blob, 2048);
}
(4) At the receiver, no matter whether the user-defined information or Blob information is received, it is submitted to the same method (ICustomizeHandler. HandleInformation method) for processing.
We can process the image just sent in the receiver as follows:
Public void HandleInformation (string sourceUserID, int informationType, byte [] info)
{
If (informationType = ImageInformationType)
{
MemoryStream memoryStream = new MemoryStream (info );
Image img = Image. FromStream (memoryStream );
MemoryStream. Close ();
//... Subsequent business logic
}
}
Iii. Blob information and P2P
If both parties send and receive Blob information are online users, and a P2P channel has been established between the two users, Blob information will be directly transmitted through the P2P channel without being transferred through the server. This is not different from general custom information. (For details about how to create a P2P channel between two clients, refer to ESFramework development manual (04)-reliable P2P)
Read more ESFramework development manual articles.
Certificate -----------------------------------------------------------------------------------------------------------------------------------------------
Download the free version of ESFramework and demo source code
For any questions about ESFramework, please contact us:
Tel: 027-87638960
Q: 372841921
Mail: esframework@oraycn.com