Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize ESFramework4.0 File transfer

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will show you how to achieve ESFramework4.0 file transfer. The content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.

ESFramework, is a set of highly reusable, flexible, simple and powerful .NET communication framework, all written in C #. ESFramework has built-in support for Tcp and Udp, and supports text and streaming protocols, providing a variety of network engines for server and client developers to easily use. Using the ESFramework framework, you can develop your efficient and stable network communication applications more quickly at a higher level without knowing Socket and caring about all the trivial things related to communication at the bottom. Such as IM system, video conference system, data acquisition system and so on, all systems that need distributed communication can use ESFramework framework.

I. File transfer process of ESPlus

ESPlus defines the standard process for file transfer, which can be shown in the following figure:

(1) the sender initiates a request to transfer the file.

(2) the recipient agrees or refuses to accept the documents. If rejected, the process ends; otherwise, proceed to the next step.

(3) the sender sends the file data and the receiver receives the file data.

(4) if the receiver or sender disconnects or cancels the file transfer during the file transfer process, the file transfer is interrupted and the process ends. If the file transfer process has been normal, then the transfer of the file will be completed.

There are several points that need to be explained:

(1) the sender can be a client or a server, and so can the receiver. But regardless of the category of the sender and receiver, they follow this file transfer process; just as all ESFramework communication engines share the same set of message processing skeleton processes.

(2) when the receiver agrees to receive, the framework will automatically search for whether there is a matching continuation item, and if so, it will start the breakpoint to resume transmission. Of course, we can control whether the breakpoint resume function is enabled through the properties of the file receiving manager. For more information about breakpoint continuation, you can refer to the principle and implementation of ESFramework 4.0file breakpoint continuation.

(3) the thread of file transfer is automatically controlled by the framework, as long as the sender receives the reply that the receiver agrees to receive, the framework will automatically send file packets in the background thread; similarly, at this time, the receiver will also automatically process the received file packets.

(4) the sender or receiver may cancel the document being transmitted at any time.

(5) when the file transfer is interrupted or completed, both the sender and the receiver will have the corresponding event notification.

II. Infrastructure used by ESPlus to support file transfer

1.TransmittingFileInfo

For each file transfer task, both sender and receiver need an object to represent it. TransmittingFileInfo is the encapsulation of a file transfer project, which contains relevant information such as sender ID, receiver ID, file name and so on.

Most of the properties of TransmittingFileInfo are valid for both the sender and receiver, while several properties are valid only for the sender (such as SendingFileParas) and several properties are valid only for the receiver (such as LocalSaveFilePath), which are detailed in the help documentation. Moreover, some attributes, such as OriginFileLastUpdateTime, exist to support breakpoint continuation capabilities.

2.FileTransDisrupttedType

ESPlus uses the FileTransDisrupttedType enumeration to define all possible causes for file transfer interruptions:

Public enum FileTransDisrupttedType {/ actively cancel / ActiveCancel, / cancel / DestCancel, / drop / DestOffline, / network interruption, / SelfOffline, / the other party refuses to receive files / DestReject / other reasons Such as file read failure / OtherCause}

3.IFileTransferingEvents interface

ESPlus defines an IFileTransferingEvents interface that exposes all status and events related to file transfer:

Public interface IFileTransferingEvents {/ this event is triggered when a file starts to be transferred. This event is triggered when the continuation of a file starts. / / event CbGeneric FileTransStarted; / (the FileTransStarted event will no longer be triggered) / / event CbGeneric FileResumedTransStarted; / progress of the file transfer. This event is triggered when the parameters are fileID (file number), total (file size), transfered (bytes transferred) / event CbFileSendedProgress FileTransProgress; / file transfer is interrupted. This event is triggered when / / event CbGeneric FileTransDisruptted; / file transfer is complete. / event CbGeneric FileTransCompleted;}

By booking these events, we can know when each transferred file starts (or breakpoint resumes), when it is completed, the real-time progress of the delivery, the reason for the interruption of the transmission, and so on. Note that these events are triggered in the background thread, and if you need to update the UI in the event handler, you need to forward the call to the UI thread.

4.SendingFileParas

This object contains only two properties: SendingSpanInMSecs and FilePackageSize. The sender can specify the frequency of sending file packets and the size of each packet through the SendingFileParas object. Generally speaking, in order to achieve the fastest transmission speed, SendingSpanInMSecs can be set to 0. The size of the FilePackageSize depends on the quality of the network environment of the sender and receiver. On Internet, it can be set to about 2048 or 4096, while in the local network, it can be set to 204800 or greater (the transmission speed of the local network can reach more than 30M/s).

5.IFileController

Through the ESPlus.Application.FileTransfering.IFileController interface, we can submit a request to send a file, and we can actively cancel the file being received or sent. IFileController can be used on both the client side and the user server side.

Public interface IFileController {/ this event interface exposes the real-time status of all files being sent. / IFileTransferingEvents FileSendingEvents {get;} / this event interface exposes the real-time status of all files being received. / IFileTransferingEvents FileReceivingEvents {get;} / prepare to send the file. If the other party agrees to receive, the background will automatically send the file; if the other party refuses to receive, the send will be canceled. / / if the file to be sent does not exist or is occupied, the corresponding exception is thrown. / the user who received the file ID / the path to the sent file / other additional comments. If you are in a FTP-like service, this parameter can be the path where the file is saved / the unique number void BeginSendFile (string accepterID, string filePath, string comment, out string fileID) that returns the file to be sent; / ready to send the file. If the other party agrees to receive, the background will automatically send the file; if the other party refuses to receive, the send will be canceled. / / if the file to be sent does not exist or is occupied, the corresponding exception is thrown. / the user who received the file ID / the path to the sent file / other additional comments. If you are in a service like FTP, this parameter can be the path to save the file / send parameter setting. Input null, which means that the default setting of IFileSenderManager is adopted. / / returns the unique number void BeginSendFile (string accepterID, string filePath, string comment,SendingFileParas paras, out string fileID) of the file to be sent; / actively cancels the file being sent or received and notifies the other party. / void CancelFileTransfering (string fileID); / cancel the transferring items associated with a user. / Target user ID. Void CancelFileTransferingAbout (string destUserID); / get information about the file being sent or received. / / TransmittingFileInfo GetFileInfo (string fileID);}

(1) BeginSendFile is used to submit a request to the receiver to send a file. If the other party agrees, the backend will automatically start transmitting the file. This method has an out parameter fileID, which is used to mark the unique number of the file transfer project. For example, if you plan to send the same file to two friends, the BeginSendFile method will be called twice, and the fileID will be different. That is, fileID is used to mark file transfer projects, not files.

This method has two overloads, and the difference is that the second BeginSendFile method has an extra SendingFileParas parameter, which is used to actively control the size and transmission frequency of file packets.

When used by the client, the BeginSendFile method can submit not only a request to send a file to other online users, but also a request to send a file directly to the server-that is, the receiver of the file is the server. We just need to pass the accepterID parameter into the NetServer.SystemUserID to indicate that the server, rather than another user, will receive the file to be sent.

(2) the GetFileInfo method can get any project information that is being sent or received.

(3) the CancelFileTransfering method is used to cancel a file transfer project being sent or received. When this method is called, the framework automatically notifies the user at the other end of the file transfer and triggers the FileTransDisruptted event in FileReceivingEvents or FileSendingEvents, while the other end automatically triggers the FileTransDisruptted event.

(4) the CancelFileTransferingAbout method is used to cancel transferring items associated with a specified user. For example, if we are chatting with aa01 users and have multiple files being transferred with aa01, if we want to close the chat window with aa01, we usually call the CancelFileTransferingAbout method to cancel all file transfers related to aa01 before closing. So you often see a similar prompt: "you have a file in progress with aa01. Closing the current window will cause the file being transferred to be interrupted. Are you sure you want to close it?" . If the user confirms that it is off, it is time for us to call the CancelFileTransferingAbout method.

(5) the FileSendingEvents property is used to expose the real-time status of all ongoing file transfer projects as the sender; the FileReceivingEvents property is used to expose the real-time status of all ongoing file transfer projects as the receiver.

6.IFileHandler

The IFileHandler interface will be called back by the framework to implement the file transfer mechanism. Like IFileController, it can be used on both the client and the user server.

We need to implement the ESPlus.Application.FileTransfering.IFileHandler interface to get notifications related to file transfer requests:

Public interface IFileBusinessHandler: does IBusinessHandler {/ agree to receive the file? / / the ID of the sender. If NetServer.SystemUserID, it is sent by the server. / / File name. / / File size. / / other additional comments. If you are in a service like FTP, this parameter can be the path / file ID where the file is saved. / / if the upload can be resumed, it will not be null. / / the return value is the path where the file is saved. If it is null, it means to refuse to receive / refuse to resume the file. If the parameter resumedFileItem is not null and the return path is equal to resumedFileItem.LocalFileSavePath, it means continued transmission; otherwise, save. String ReadyToAcceptFile (string senderID, string fileName, long fileLength, string comment, string fileID, ResumedFileItem resumedFileItem); / the recipient's reply to a request for a file sent by himself-agree / reject. / Information about the file transfer project / whether the other party agrees with void OnResponseOfReceiver (TransmittingFileInfo info, bool agreed);}

(1) the ReadyToAcceptFile method is called back by the framework when the current user is the receiver, while the OnResponseOfReceiver method is called back by the framework when the current user is the sender.

(2) both methods of the IFileHandler interface will be called by the framework in the background thread. If you need to refresh the application's UI when implementing this method, be sure to forward it to the UI thread.

(3) when the sender submits the request to send the file, the framework will call back the ReadyToAcceptFileAsyn method at the receiver to ask whether it agrees to receive. If so, ReadyToAcceptFileAsyn should return a valid path to store the received file, otherwise, return null.

(4) when the receiver agrees or refuses to receive the file, the framework calls back the OnResponseOfReceiver method at the sender to notify the sender. In general, when an application implements the OnResponseOfReceiver method, it only needs to inform the sender of the file at most, without any additional processing. Because the framework has taken care of everything for you.

When the receiver agrees to receive the file, events related to the file transfer project are triggered one after another through the IFileTransferingEvents interface exposed by IFileOutter.

three。 Client

Like ESPlus's Basic application or CustomizeInfo application, supporting file transfer function on the client side also needs to use the corresponding "Outter" component and implement the corresponding "BusinessHandler" interface.

1.IFileOutter

The ESPlus.Application.FileTransfering.Passive.IFileOutter interface inherits from IFileController and adds an attribute and a method: public interface IFileOutter: IOutter, the message priority that IFileController {/ uses when sending file packets. / DataPriority DataPriority4SendingFile {get; set;} / initialize file transfer viewer control. / / File transfer Viewer control object / the ID of the target user. The returned viewer displays all file transfer status related to the user. If null is passed in, the real-time status of file transfers with any user is displayed. Void InitializeFileTransferingViewer (FileTransferingViewer viewer, string destUserID);}

(1) We can control the priority of sending file packets by setting the DataPriority4SendingFile attribute. In a general system, it can be set to Common or Low, but must not be set to CanBeDiscarded, otherwise the file received by the receiver may be incomplete.

(2) ESPlus provides the default status viewer control FileTransferingViewer for the transfer project. If there are no special requirements, you can directly use it in the project to display the real-time status of file transfer. Its interface screenshot is as follows:

All you have to do is drag and drop the control onto your UI, then pass it into the InitializeFileTransferingViewer method of IFileOutter for initialization, and it will work properly.

The second parameter of the InitializeFileTransferingViewer method, destUserID, indicates the status of all file transfer items associated with which friend the current FileTransferingViewer control wants to display. Using QQ as an analogy, if you are transferring files with multiple friends at the same time, there will be multiple chat windows, and each chat window will have a FileTransferingViewer instance, and this FileTransferingViewer instance will only show the transfer items of the friends corresponding to the current chat window. Depending on this, the progress viewer for transferring files to aa01 users will not be displayed on your chat window with aa02.

If your FileTransferingViewer viewer needs to capture the real-time status of all projects being transferred, the destUserID parameter can be passed in null when the InitializeFileTransferingViewer method is called.

In addition, FileTransferingViewer implements the IFileTransferingViewer interface:

Public interface IFileTransferingViewer {/ this event is triggered when a file starts to resume. The parameter is FileName-isSend / event CbGeneric FileResumedTransStarted; / when a file has been transferred, the event is triggered. The parameter is FileName-isSend / event CbGeneric FileTransCompleted; / when a file transfer is interrupted, the event is triggered. The parameter is FileName-isSend-FileTransDisrupttedType / event CbGeneric FileTransDisruptted; / this event is triggered when a file transfer starts. The parameter is FileName-isSend / event CbGeneric FileTransStarted; / when all files have been transferred, the event is triggered. This event is triggered when the cancel button is clicked to terminate a file transfer. / / event CbSimple AllTaskFinished; / / event CbCancelFile CancelFileButtonClicked; / whether any files are currently being transferred. / / bool IsFileTransfering ();}

You can also follow the status of the file transfer project captured by the FileTransferingViewer viewer (not necessarily all, as mentioned earlier) through this interface, and the events of this interface are triggered in the UI thread, and you can directly manipulate the UI display in its handler.

2.IFileBusinessHandler

The client's ESPlus.Application.FileTransfering.Passive.IFileBusinessHandler inherits directly from IFileHandler with no additional content:

Public interface IFileBusinessHandler: IBusinessHandler, IFileHandler {}

four。 Server side

The server can also receive the file sent by the client (that is, upload), or even send the file to the client (that is, download), which follows the same file transfer process.

1.IFileTransferingController

If the server is required to participate in the sending and receiving of files, like the client, the server's ESPlus.Application.FileTransfering.Server.IFileTransferingController interface inherits from IFileController to submit a request for sending a file or cancel a file in progress.

2.IFileBusinessHandler

The server needs to implement the ESPlus.Application.FileTransfering.Server.IFileBusinessHandler interface, which also inherits directly from IFileHandler, with no additional content.

5. Rapid engine support for file transfer

The new version of the Rapid engine (which can be downloaded from the end of the ESFramework 4.0 overview) adds support for file transfer, and friends who use the Rapid engine can easily take advantage of the file transfer function provided by the framework.

1. Client

IRapidPassiveEngine's Initialize method adds an overload:

Void Initialize (string userID, string serverIP, int serverPort, IBasicBusinessHandler basicHandler, ICustomizeInfoBusinessHandler customizeHandler, IFileBusinessHandler fileHandler)

The overload adds an IFileBusinessHandler parameter to support the file transfer mechanism.

In addition, IRapidPassiveEngine adds an IFileOutter read-only attribute, which is exposed so that we can submit a request to send a file or monitor the status of the file transfer. Of course, if no valid IFileBusinessHandler reference is passed in the Initialize method, the FileOutter property returns null.

two。 Server side

Like the client, the Initialize method of the server IRapidServerEngine adds an overload:

Void Initialize (int port, ICustomizeInfoBusinessHandler customizeInfoBusinessHandler,IFileBusinessHandler fileBusinessHandler, IFriendsManager friendsManager, IGroupManager groupManager)

IRapidServerEngine provides an IFileTransferingController read-only property that is used to submit a request to send a file or to monitor the status of a file transfer. Similarly, if no valid IFileBusinessHandler reference is passed in the Initialize method, the FileTransferingController property returns null.

The Rapid engine has assembled all the components related to file transfer for us, and we can use it directly. If we have time later, we will delve more deeply into the principle behind ESPlus's file transfer mechanism and how to build the function of file transfer step by step.

ESPlus makes it so easy to add file transfer function to ESFramework-based communication system, and even based on the file transfer function provided by ESPlus, we can quickly implement a file server to support file upload and download.

The above is how to achieve ESFramework4.0 file transfer of all the content, more and how to achieve ESFramework4.0 file transfer related content can search the previous article or browse the following article to learn ha! I believe the editor will add more knowledge to you. I hope you can support it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report