High performance TCP socket connection shutdown release centralized scheduling class

Source: Internet
Author: User

<summary>///socket Shutdown scheduling class, this class provides static access, but this class can instantiate///</summary>//<remarks>//Socket connection off    Closed class///3 queues//1.shotdown shutdown queue///1///2.close queue interval 2 seconds///2//3.dispose queue interval 1 second//step 3 </remarks> public class Closesoketdispather {///<summary>//Static single case//&LT;/S        ummary> private static readonly Closesoketdispather instance = new Closesoketdispather ();        <summary>///Create a socket shutdown schedule instance/////</summary> public closesoketdispather ()        {Start ();         }///<summary>///Run status///</summary> public bool Isrun {get; protected set;} <summary>////For Single instance access///</summary> public static Closesoketdispather Inst            ance {get {return instance; }}///<summary>// Start initializing//</summary> protected virtual void start () {if (!                Isrun) {//shutdown Task.Factory.StartNew (cycleshutdown);                Close Task.Factory.StartNew (cycleclose);            Dispose Task.Factory.StartNew (cycledispose);        } Isrun = true; }///<summary>//Close queue///</summary> protected Concurrentqueue<socketclosein        fo> shutdownqueue = new concurrentqueue<socketcloseinfo> (); <summary>///close queue///</summary> protected concurrentqueue<socketcloseinfo> Cl        Osequeue = new concurrentqueue<socketcloseinfo> (); <summary>//Release queue///</summary> protected concurrentqueue<socketcloseinfo> Di        Sposequeue = new concurrentqueue<socketcloseinfo> (); <summary>///close to Image pool        </summary> protected Basepool<socketcloseinfo> Closepool = new basepool<socketcloseinfo&        gt; ();        <summary>///Add to the need to close the queue.            </summary>//<param name= "socket" ></param> public void Push (socket socket) {                if (socket! = NULL) {Socketcloseinfo info = Closepool.pop (); Info.                socket = socket;            Shutdownqueue.enqueue (info); }} #region Step 1 Shutdown//<summary>///Cycle off//</summary> Prote            CTED void Cycleshutdown () {while (true) {Shutdown ();        }}///<summary>//Cycle off socket///</summary> protected void Shutdown ()            {Socketcloseinfo info;        if (Shutdownqueue.trydequeue (out info)) {try {            if (info. Socket! = null) {info.                    Socket.shutdown (Socketshutdown.both); }//Add info.                Shutdowntime = DateTime.Now; } catch (Exception ex) {Logs.write (Logtype.error, String.                Format ("Turn off Sokcet on image exception: {0}", ex));                        } finally {if (info! = null) {                    Closequeue.enqueue (info); }}} else {//pause 100 ms System.Threading.Thre Ad.            SpinWait (100); }} #endregion #region step 2Close//<summary>///Cycle off//</summary&gt        ;            protected void Cycleclose () {while (true) {closes ();        }}//<summary>Close eligible data///</summary> protected void closes () {//Temp variable var tmp = n            EW list<socketcloseinfo> ();            Socketcloseinfo info; while (Closequeue.trydequeue (out info)) {//close shout occurs when the if (info.                Shutdowntime.addseconds (1.01) < DateTime.Now) {Close (info); } else {tmp.                ADD (info); }}//Add no end data to the collection for next check foreach (var item in TMP) {//Add to Collection            The next check Closequeue.enqueue (item);        }//Next recirculation System.Threading.Thread.Sleep (100);  }///<summary>//Cycle off socket//</summary> protected void Close (Socketcloseinfo Info) {try {if (info).              Socket! = null) {      Info.                Socket.close (); }//Add info.            Closetime = DateTime.Now; } catch (Exception ex) {Logs.write (Logtype.error, String.            Format ("Turn off Sokcet on image exception: {0}", ex));                    } finally {//Added to the release queue if (info! = null) {                Add to queue Disposequeue.enqueue (info); }}} #endregion #region step 3Dispose//<summary>///Cycle off// </summary> protected void Cycledispose () {while (true) {Dispose            S ();        }}///<summary>///close eligible data///</summary> protected void disposes ()            {//Temp variable var tmp = new list<socketcloseinfo> ();            Socketcloseinfo info; while (Disposequeue.trydequeue (oUT info) {//Close the time shout occurs if (info.                Shutdowntime.addseconds (1.01) < DateTime.Now) {Socketdispose (info); } else {tmp.                ADD (info); }}//Add no end data to the collection for next check foreach (var item in TMP) {//Add to Collection            The next check Disposequeue.enqueue (item);        }//Next recirculation System.Threading.Thread.Sleep (100); }///<summary>//Cycle off socket///</summary> protected void Socketdispose (socketc Loseinfo info) {try {if (info). Socket! = null) {info.                Socket.dispose (); }} catch (Exception ex) {Logs.write (Logtype.error, String.            Format ("Turn off Sokcet on image exception: {0}", ex));            }Finally {//also pool Gaveback (info); }} #endregion///<summary>///Pool//</summary>/<param Nam e= "obj" > Pool data pair like </param> protected void Gaveback (Socketcloseinfo obj) {obj.            Socket = null; Obj.            Shutdowntime = Datetime.minvalue; Obj.            Closetime = Datetime.minvalue;            Also into the pool.        Closepool.gaveback (obj);        }///<summary>//Socket closing information///</summary> protected class Socketcloseinfo {//<summary>//For socket to image///</summary> public socket SOC            Ket {get; set;} <summary>//Stop time///</summary> public DateTime shutdowntime {get; set ; }//<summary>//Close time///</summary> public DateTime Closetime {get; set;} }    }

High performance TCP socket connection shutdown release centralized scheduling class

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.