private void Start() { try { Core.SystemForm.Chat.scrollback.InsertText(messages.get("loading-server", Core.SelectedLanguage, new List<string> { this.Server }), Client.ContentLine.MessageStyle.System); Core.SystemForm.Status("Connecting to " + Server); if (!SSL) { _networkStream = new System.Net.Sockets.TcpClient(Server, Port).GetStream(); _StreamWriter = new System.IO.StreamWriter(_networkStream); _StreamReader = new System.IO.StreamReader(_networkStream, Encoding.UTF8); } if (SSL) { System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient(Server, Port); _networkSsl = new System.Net.Security.SslStream(client.GetStream(), false, new System.Net.Security.RemoteCertificateValidationCallback(Protocol.ValidateServerCertificate), null); _networkSsl.AuthenticateAsClient(Server); _StreamWriter = new System.IO.StreamWriter(_networkSsl); _StreamReader = new System.IO.StreamReader(_networkSsl, Encoding.UTF8); } Connected = true; Deliver(new Datagram("PING")); Deliver(new Datagram("LOAD")); Datagram login = new Datagram("AUTH", ""); login.Parameters.Add("user", nick); login.Parameters.Add("pw", password); Deliver(login); Deliver(new Datagram("GLOBALNICK")); Deliver(new Datagram("NETWORKLIST")); Deliver(new Datagram("STATUS")); keep = new System.Threading.Thread(_Ping); Core.SystemThreads.Add(keep); keep.Name = "pinger thread"; keep.Start(); } catch (System.Threading.ThreadAbortException) { return; } catch (Exception b) { Core.SystemForm.Chat.scrollback.InsertText(b.Message, Client.ContentLine.MessageStyle.System); return; } string text = ""; try { sBuffer = new Services.Buffer(this); Core.SystemForm.Status(getInfo()); while (!_StreamReader.EndOfStream && Connected) { text = _StreamReader.ReadLine(); Core.trafficscanner.Insert(Server, " >> " + text); while (Core.IsBlocked) { System.Threading.Thread.Sleep(100); } if (Valid(text)) { // if this return false the thread must be stopped now if (!Process(text)) { return; } continue; } } } catch (System.IO.IOException fail) { if (IsConnected && !disconnecting) { // we need to wrap this in another exception handler because the following functions are easy to throw some try { Core.SystemForm.Chat.scrollback.InsertText("Quit: " + fail.Message, Client.ContentLine.MessageStyle.System); Core.DebugLog("Clearing the sBuffer to prevent corrupted data being written"); sBuffer = null; Disconnect(); } catch (Exception f1) { Core.handleException(f1); } } } catch (System.Threading.ThreadAbortException) { Core.KillThread(System.Threading.Thread.CurrentThread, true); return; } catch (Exception fail) { if (IsConnected) { Core.handleException(fail); } Core.KillThread(System.Threading.Thread.CurrentThread); } }
/// <summary> /// This will close the protocol, that mean it will release all objects and memory, you should only call it when you want to remove this /// </summary> public override void Exit() { lock (this) { if (IsDestroyed) { return; } if (IsConnected) { Disconnect(); } disconnecting = true; int remaining = 0; lock (RemainingJobs) { remaining = RemainingJobs.Count; if (RemainingJobs.Count == 0) { FinishedLoading = true; } RemainingJobs.Clear(); } Core.DebugLog("Remaining jobs are now cleared"); if (sBuffer == null) { Core.DebugLog("Warning sBuffer == null"); } if (Configuration.Services.UsingCache && sBuffer != null) { if (FinishedLoading) { Core.SystemForm.Status("Writing the service cache"); sBuffer.Snapshot(); Core.SystemForm.Status("Done"); } else { Core.DebugLog("Didn't write the network cache because the services were still waiting on " + remaining.ToString() + " requests"); } } else { Core.DebugLog("Didn't write the network cache because it is disallowed"); } lock (NetworkList) { foreach (Network network in NetworkList) { network.Destroy(); if (Core.SelectedNetwork == network) { Core.SelectedNetwork = null; } } NetworkList.Clear(); } if (keep != null) { Core.KillThread(keep); keep = null; } if (sBuffer != null) { sBuffer.Destroy(); } sBuffer = null; if (_StreamWriter != null) _StreamWriter.Close(); if (_StreamReader != null) _StreamReader.Close(); _StreamWriter = null; _StreamReader = null; base.Exit(); destroyed = true; } }