FileZilla source code analysis 4

Source: Internet
Author: User
Tags ftp client filezilla filezilla ftp filezilla ftp client

FileZillaIt is a fast and reliable FTP client and server-side open source code program with a variety of features and intuitive interfaces. This article analyzes the source code of FileZilla.

<Type = "text/javascript"> <src = "http://pagead2.googlesyndication.com/pagead/show_ads.js" type = "text/javascript"> <type = "text/javascript"> <src = "http://pagead2.googlesyndication.com/pagead/show_ads.js" type = "text/javascript">

When creating (), CServer listens to the standard FTP port 21 through CListenSocket and listens to the admin port through CAdminListenSocket (default: 14147). Both classes inherit from CAsyncSocketEx, this class is the base class for all socket processing in FileZilla. This class can clearly understand the socket processing mechanism.

The class name is derived from the MFC class CAsyncSocket. CAsyncSocketEx is fully compatible with CAsyncSocket. The code written on CAsyncSocket can be compiled in CAsyncSocketEx without any change, CAsyncSocketEx also provides some functional extensions and performance optimizations.

CAsyncSocketEx is closely related to CAsyncSocketExHelperWindow and CAsyncSocketExLayer. CAsyncSocketExLayer is similar to Interceptor in J2EE.

CAsyncSocketEx adopts the message processing mechanism, that is, the listening port has activity, such as data to be received and sent, to realize the communication of such information by sending messages, here, the windows socket API is WSAAsyncSelect. Its prototype is:

Int WSAAsyncSelect (

SOCKET s,

HWND hWnd,

Unsigned int wMsg,

Long lEvent

);

Parameters

S

[In] Descriptor identifying the socket for which event notification is required.

HWnd

[In] Handle identifying the window that will receive a message when a network event occurs.

WMsg

[In] Message to be sent Ed when a network event occurs.

LEvent

[In] Bitmask that specifies a combination of network events in which the application is interested.

Simply put, This method allows windows to send the message wMsg to the window hWnd when the specified event lEvent occurs on the SOCKET s specified by windows.

Because windows message mechanism must use a windows window, CAsyncSocketEx must create a windows window to receive such messages. This is the main function of CAsyncSocketExHelperWindow, of course, this window does not have to be displayed, as long as the system knows that windows exists (that is, hWnd.

In CAsyncSocketEx, a static linked list is defined:

Static struct t_AsyncSocketExThreadDataList

{

T_AsyncSocketExThreadDataList * pNext;

T_AsyncSocketExThreadData * pThreadData;

} * M_spAsyncSocketExThreadDataList;

This linked list maintains a t_AsyncSocketExThreadData chain. Let's take a look at the definition of this struct:

Struct t_AsyncSocketExThreadData

{

CAsyncSocketExHelperWindow * m_pHelperWindow;

Int nInstanceCount;

DWORD nThreadId;

Std: list layerclosenoworkflow;

} * M_pLocalAsyncSocketExThreadData;

You can see the name. This is a thread-related structure. In fact, this structure describes a distribution thread.

In the implementation of FileZilla, the entire static class relationship is as follows:

A CAsyncSocketEx represents a socket, that is, the socket for listening on a port, such as the standard FTP 21 port and admin port mentioned above.

A CAsyncSocketExHelperWindow represents a thread responsible for message distribution, that is, it is responsible for receiving socket (CAsyncSocketEx) activities, and then distributing them to different processing classes CAsyncSocketEx. Each CAsyncSocketExHelperWindow corresponds to one distribution thread, that is, one distribution thread has only one CAsyncSocketExHelperWindow, and vice versa. The t_AsyncSocketExThreadData Structure describes the relationship between the distribution thread and CAsyncSocketExHelperWindow.

CAsyncSocketExHelperWindow can be used to distribute multiple CAsyncSocketEx instances, while CAsyncSocketEx can only be distributed by one CAsyncSocketExHelperWindow. Now let's take a closer look at the structure t_AsyncSocketExThreadData:

Struct t_AsyncSocketExThreadData

{

CAsyncSocketExHelperWindow * m_pHelperWindow; // CAsyncSocketExHelperWindow corresponding to this thread

Int nInstanceCount; // The current distribution thread corresponds to several CAsyncSocketEx

DWORD nThreadId; // threadID of the current thread

Std: list layerclosenoworkflow; // I will talk about this later.

} * M_pLocalAsyncSocketExThreadData;

This code is defined in the class CAsyncSocketEx, that is, m_pLocalAsyncSocketExThreadData defines the distribution thread corresponding to the current CAsyncSocketEx, that is, CAsyncSocketExHelperWindow.

The global m_spAsyncSocketExThreadDataList defines a t_AsyncSocketExThreadData (distribution thread) linked list, that is, FileZilla can have multiple distribution threads, each distribution thread corresponds to multiple sockets, that is, CAsyncSocketEx.

A Practical scenario:

When FileZilla Server is started, it listens to two ports by default: 21 and admin. Therefore, there are two sockets, namely, two CAsyncSocketEx.

The two CAsyncSocketEx share a distribution thread: t_AsyncSocketExThreadData

When a user connects to the server through FTP and downloads the file through the get/mget command, the FTP server starts a transmission thread to listen on a temporary port, and a CAsyncSocketEx is added, A distribution thread responsible for the CAsyncSocketEx is also added. Therefore, a node is added to m_spAsyncSocketExThreadDataList.

The status is:

One m_spAsyncSocketExThreadDataList chain, two t_AsyncSocketExThreadData, and three CAsyncSocketEx.

Through the complete description in this article, you should know the source code of FileZilla and hope to help you!

  • FileZilla
  • FileZilla source code analysis 1
  • FileZilla source code analysis 2
  • FileZilla code analysis 3
  • FileZilla: free server software
  • New Version FileZilla 3.3.1.0 bug fixes
  • Key Vulnerability in FileZilla FTP client hard-coded password Index

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.