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 analyze the source code of FileZilla

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article shows you how to carry out FileZilla source code analysis, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

FileZilla is a fast and reliable FTP client and server-side open source program with a variety of features and intuitive interfaces. Today, I will analyze 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 CServer is in Create (), it listens to the standard FTP 21 port through CListenSocket and the admin port through CAdminListenSocket (default is 14147). Both classes inherit from CAsyncSocketEx, which is the base class for all socket processing in FileZilla. Figuring out this class can clearly understand the mechanism of socket processing.

The name of this class comes from the MFC class CAsyncSocket,CAsyncSocketEx is fully compatible with CAsyncSocket, the code written on CAsyncSocket can be compiled under CAsyncSocketEx without a word, and CAsyncSocketEx has made some functional extensions and performance optimizations.

CAsyncSocketEx is closely related to the other two classes, CAsyncSocketExHelperWindow and CAsyncSocketExLayer, and the role of CAsyncSocketExLayer is similar to that of Interceptor in J2EE, which can be left out here.

CAsyncSocketEx adopts the mechanism of message processing, that is, the listening port is active, such as data to be received or sent, and the communication of this kind of information is realized by sending messages. Here, the API of windows socket is WSAAsyncSelect, and 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 received 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 a message wMsg to the window hWnd when the specified event lEvent occurs on the socket specified by SOCKET s.

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

In CAsyncSocketEx, a linked list of static is defined:

Static struct t_AsyncSocketExThreadDataList

{

T_AsyncSocketExThreadDataList * pNext

T_AsyncSocketExThreadData * pThreadData

} * m_spAsyncSocketExThreadDataList

This linked list maintains a t_AsyncSocketExThreadData chain. Take a look at the definition of this struct:

Struct t_AsyncSocketExThreadData

{

CAsyncSocketExHelperWindow * m_pHelperWindow

Int nInstanceCount

DWORD nThreadId

Std::list layerCloseNotify

} * m_pLocalAsyncSocketExThreadData

As you can see from the name, this is a structure related to thread thread, which in fact describes a distribution thread.

In the implementation of FileZilla, the entire static class relationship looks like this:

A CAsyncSocketEx represents a socket, that is, a socket that listens on a port, such as the standard FTP 21 port, admin port, and so on.

A CAsyncSocketExHelperWindow represents a thread responsible for message distribution, that is, the activity that receives socket (CAsyncSocketEx) and distributes it to a different processing class, CAsyncSocketEx. Each CAsyncSocketExHelperWindow corresponds to one distribution thread, that is, a distribution thread has only one CAsyncSocketExHelperWindow, and vice versa. The structure t_AsyncSocketExThreadData describes the relationship between the distribution thread and the CAsyncSocketExHelperWindow.

CAsyncSocketExHelperWindow can be distributed for multiple CAsyncSocketEx, while CAsyncSocketEx can only be distributed by one CAsyncSocketExHelperWindow. Now take a closer look at the structure t_AsyncSocketExThreadData:

Struct t_AsyncSocketExThreadData

{

CAsyncSocketExHelperWindow * mroompHelperWindow. the CAsyncSocketExHelperWindow corresponding to this thread.

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

DWORD nThreadId; / / threadID of the current thread

Std::list layerCloseNotify; / / We'll 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, namely CAsyncSocketExHelperWindow.

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

Take a real-world scenario:

When FileZilla Server starts, two ports are listening by default: 21 and admin, so there are two socket, two CAsyncSocketEx.

The two CAsyncSocketEx share a distribution thread: t_AsyncSocketExThreadData

When a user connects to server through FTP and downloads files through the get/mget command, the FTP server starts a transfer thread to listen on a temporary port, and a CAsyncSocketEx is added, as well as a distribution thread responsible for the CAsyncSocketEx, so a node is added to the m_spAsyncSocketExThreadDataList.

The situation at this time is:

One m_spAsyncSocketExThreadDataList chain, two t_AsyncSocketExThreadData, three CAsyncSocketEx.

The above content is how to analyze the source code of FileZilla. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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

Servers

Wechat

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

12
Report