INTERNAL CLASS HOST: MARSHALBYREFOBJECT {......}
First, we see that Host can only be used in the Cassini project because it is the class definition of INTERNAL. In addition, inheriting from MARSHALBYREFOBJECT, allowing access to objects across application domains in applications that support remote processing. We associate ASP.NET's execution of the application is that the application domain is divided into boundaries, and as Host must be able to support cross-application domain so that multiple applications are executed within their boundaries.
Let's take a look at its member variables:
Private bool _started; // Have you started
private bool _stopped; // is stopped
Private server_server; // Double mutual father
Private int _port; // The following should be an internal variable that the web server should have
Private string_virtualpath;
Private string _lowercasedvirtualpath;
Private string _lowercasedvirtualpathwithtrailingslash;
PRIVATE STRING _PHYSICALPATH;
PRIVATE STRING _INSTALLPATH;
PRIVATE STRING _PHYSICALCLIENTScriptpath;
Private string _lowercasedClientScriptPathwithTrailingslashv10;
Private string _lowercasedClientScriptPathwithTrailingslashv11;
Private socket _socket; // Sockets for communication
Private waitcallback _onstart; // callback function
PRIVATE WAITCALLBACK _ONSOCKETACCEPT;
Private eventHandler _onAppdomainunload; // event monitoring, when an application domain will take out, processing by the Host class.
Public Override Object InitializelifetimeService () {
Return null; // NEVER EXPIRE LEASE
} Is a member of MarshalByrefObject, as comment in the source code, tells the .NET Framework through returns null, never expires such instances. Distributed garbage collection is responsible for controlling the survival of server applications and is responsible for deleting them during their survival expiration. Traditionally, distributed garbage collection uses reference counts and ping. This can work well when there are several clients per object, but thousands of clients are very efficient at each object. The survival service can use the function of the traditional distributed garbage collector, and can expand well when the number of clients increases.
The Configure function is worth analysis because it appears in server.createhost, see:
Public Void Configure (Server Server, Int Port, String Virtualpath, String PhysicalPath) {
_server = server;
_port = port;
_VIRTUALPATH = VirtualPath;
_lowerCasedVirtualPath = CultureInfo.InvariantCulture.TextInfo.ToLower (_virtualPath); _ lowerCasedVirtualPathWithTrailingSlash = virtualPath.EndsWith ( "/") virtualPath:? virtualPath "/";
_LowercaseDvirtualPathwithTrailingslash = CultureInfo.invariantculture.TextInfo.tolower (_LowercaseDvirtualPathwithtrailingslash);
_physicalpath = PhysicalPath;
_INSTALLPATH = INSTALLPATH;
// The above is assignment and parameter inspection code
_physicalclientscriptpath = installpath "//asp.netclientfiles//";
// The following begins to determine the client scripting path provided by ASP.NET
String version4 = fileversioninfo.getvessionInfo (TypeOf (httpruntime) .Module.fullyqualifiedName) .fileversion; // Find the version information of the current ASP.NET runtime module
String Version3 = Version4.substring (0, Version4.lastIndexof ('.'));
_LowerCaseDClientScriptPathwithTrailingslashv10 = "/ aspnet_client / system_web /" Version4.Replace ('.', '_') "/";
_LowerCaseDClientScriptPathwithTrailingslashv11 = "/ aspnet_client / system_web /" Version3.Replace ('.', '_') "/";
// The following starts to set the callback function for the socket.
_onSocketAccept = New WaitCallback (onsocketaccept);
_onstart = new waitcallback (onstart);
// Start Watching for app domain unloading
_onappdomainunload = new eventhandler (onappdomainunload);
Thread.getdomain (). Domainunload = _onappdomainunload;
}
Obviously, the config function has made the following:
1. Incorporate some configuration parameters to the internal variable of the HOST class
2. Determine some of the environments provided by ASP.NET, such as script environments, the path exists, so that some components of ASP.NET can be successfully performed (just like in IIS)
3, prepare some callback functions, event monitor function processing events
External use of Host also passed the start process, let's take a look:
Public void start () {
IF (_started) // has been started, not 2 instances
Throw new invalidopertyleException ();
_Socket = new socket (addressfamily.internetwork, sockettype.internet, protocoltype.tcp); _ socket.bind (new iPaddress.Any, _port);
_Socket.Listen ((int) socketoptionname.maxconnections);
_started = true;
Threadpool.queueuserworkItem (_OnStart);
}
This function is completed:
1. Exclude two instances
2, start listening on a given port
3, start _onstart callback function thread, run the listener sleeve, but is asynchronous call, the start function is returned immediately.
How is the _onstart function to deal with?
Private void onstart (Object unused) {
While (_started) {
Try {
Socket socket = _socket.accept ();
Threadpool.queueUserWorkItem (_onSocketAccept, socket);
}
Catch {
Thread.sleep (100);
}
}
_stopped = true;
}
This thread has been loop, calls the Accept function of the socket, when a connection is connected, call a new thread (_onSocketAccept) and a new Socket, then generate a new thread to handle the customer request. After the exception is paused, continue the socket introduced by Accept. Once _started flags are set to FALSE, stop listening exit.
Private void OnSocketAccept (Object AcceptedSocket) {
Connection conn = new connection (this, (socket) acceptedSocket;
CONN.PROCESSONEREQUEST ();
}
This thread will now generate a Connection object and call the processONEREQUEST to process. It seems that the process of truly handling a Web Request is within the Connection object.