Cassini source analysis (2)

xiaoxiao2021-03-06  74

We started from the part of the launcher.

The entry that starts is the main function, which only exists in CassiniWebserver, and CassiniWebServer inherits from the Form class, but we see that the class does not implement code (just provide an entry). In the main function, just two lines of code:

[Stathread]

Public static int main (string [] args) {

Application.run (New CassiniForm (ARGS));

Return 0;

}

Static Method Application.run launches a CassiniForm instance under the current thread environment (STATHREAD), start the message loop, and makes the form visible.

We learned from MSDN:

The Application class has methods for launching and stopping applications and threads, and processing Windows messages. Call RUN to start the application message loop on the current thread and you can choose to make a form visible. Call exit or exitthread to stop the message loop. When your program is in a loop, call Doevents to process the message. Call AddMessageFilter to monitor Windows messages to add a message filter to the application message pump. IMessageFilter allows you to block an event or perform special operations before calling an event handler.

Since we analyze CassiniForm, let's take a look at this inherited class from form.

Reading code We see:

In addition to some GUI elements support member variables, there are several members private variables:

Private static string _appath; // Used to store ASP.NET startup application physical path

Private static string _portstring; // Web Server listening port number

Private static string _virtroot; // Application virtual directory

Private cassini.server _server; // the web server source code

There is no special function for the CassiniForm, we see a key Start (), from the name, we can guess the Web Server boot process. Take a closer look:

Private void start ()

{

_apppath = appdirtextbox.text;

IF (_Apppath.length == 0 ||! Directory.exists (_AppPath) {

ShowError ("Invalid Application Directory);

AppdiRTextBox.selectall ();

AppdiRTextBox.focus ();

Return;

}

_portstring = portTextBox.Text;

INT portnumber = -1;

Try {

Portnumber = int32.parse (_portstring);

}

Catch {

}

IF (portnumber <= 0) {

ShowError ("Invalid Port");

PortTextBox.selectall ();

PortTextBox.focus ();

Return;

}

_VIRTROOT = vRootTextBox.Text;

IF (_VIRTROOT.LENGTH == 0 ||! _VIRTROOT.STARTSWITH ("/")) {

ShowError ("Invalid Virtual Root"); vroottextbox.selectall ();

vroottextbox.focus ();

Return;

}

// The above paragraph is the inspection parameters to see if the parameters entered by the user meet the requirements. From here, we have also seen how important the software's fault is wrong, and most of the foreign programmers are very valued, it is worth learning. Key implementation is the following two lines of code

Try {

_Server = new cassini.server (portnumber, _virtroot, _apppath);

_Server.Start ();

}

// We can know here, our New a Cassini.server then calls the Start process. An error will report an error. There is no Catch (Exception E) in the source code, which I have added it in order to check the error prompt information. By doing this, I know how to execute after compiler.

Catch (Exception E) {

Messagebox.show (e.tostring ());

SHOWERROR

"Cassini Managed Web Server Failed to Start Listening On Port" Portnumber "./R/N"

"Possible Conflict With Another Web Server on The Same Port.");

PortTextBox.selectall ();

PortTextBox.focus ();

Return;

}

STARTBUTTON.ENABED = false

AppdiRTextBox.enabled = false;

PortTextBox.enabled = false;

vroottextBox.enabled = false;

Browselabel.visible = true;

Browselink.text = getLinkText ();

Browselink.visible = true;

Browselink.focus ();

}

CassiniForm is almost almost. Shuiyu, let's take a look at Cassini.server. This class is actually the main class of our entire Cassini.

The main data members of Cassini.Server are:

Private int _port; // port, probably the port of Web Server

Private string _virtualpath; // virtual directory, that is, the virtual path executed by the application

Private string _physicalpath; // physical path

Private string _INSTALLPATH; //

Private waitcallback _restartCallback; // Used to restart the callback function of Host

Private host _host; //asp.net application's real host, from the name

The constructor is last called:

_RestartCallback = New WaitCallback (RestartCallback); / / Specifies a callback function to use it for Restart. RestartCallback mainly is CreateHost () and start (), but it is necessary to note that the thread scheduling method is used.

_INSTALLPATH = GetInstallPathandConfigureaspnetifNeeded (); // This function search The registry gets the path to the ASPNET_ISAPI.DLL. The DLL performs the basic framework function of ASP.NET (of course after the ASP.NET application is established). Createhost ();

Start () is very simple, it is to determine if the HOTS exists and starts Host, it seems that the Cassini.server class is to see what Createhost is going.

Private void createhost () {

_host = (host) ApplicationHost.createApplicationHost (TypeOf (Host), _VirtualPath, _physicalPath;

_host.configure (this, _port, _virtualpath, _physicalpath, _installpath);

}

It turned out to call the only way CreateApplicationHost for calling ApplicationHost created an AppDomain environment that executed ASP.NET.

Where Host is a name that is provided by the user provided by the user in the new application domain. Here is a custom cassini.host, which is used to host the ASP.NET application.

转载请注明原文地址:https://www.9cbs.com/read-92812.html

New Post(0)