Using system;
Namespace jh.sockets {/// /// Thread status signal definition /// summary> public enum jhthreadstatus {stop, // Stop Running, // is running EXIT // Exit} /// < Summary> /// Thread State Parameters /// summary> Class Parm {Public Parm (Object Obj): this (jhthreadstatus.stop, obj) {} public parm (jhthreadstatus nstatus, object obj) {status = nstatus; Objparm = Obj;} jhthreadstatus status; Object objParm; /// /// context object /// summary> public object objParm {get {Return ObjParm;} set {objParm = value;}} /// < Summary> /// Control status /// summary> public jhthreadstatus status {get {return status;} set {status = value;}}} /// /// communication data processing interface /// < / summary> public interface IOBSERVER {// // Data Processing Portal // Void DEAL (CommData CD);
// // Data processing overload method. Returns true if you do not want the later processor to continue processing data, otherwise returns false; // Bool Ondeal (CommData CD);
// // Additional itself initialization operation // bool init ();
// // Initialization interface // bool beforeinit ();
// Initialization post-processing interface // bool afterinit ();
// // Delete the front processing interface // bool beforeexit ();
// // Next observer // IOBSERVER NEXT {Get; set;}
// // Previous observer // IOBSERVER Prev {Get; set;}
// // Add an observer to the back, if there is no action // Void Add (IOBSERVER OBSERVER) if there is already an existing linked list;
// // Insert the observer to the next observer's front // void insert (IOBSERVER OBSERVER); // Delete the observer Void Remove (IOBSERVER OBSERVER); // Does the Bool ISEXIST (IOBSERVER OBSERVER) in the list;} / // /// Communication data processing class base class, can only be derived /// summary> public Abstract Class Observer: IOBSERVER {/// /// Data processing entry. // < / summary> /// TCPCLIENT class instance param> /// memory data stream param> public void deal (commdata cd) {cd.data .Position = 0; IF (ONDEAL (CD) == false Return; if (Next! = Null) {Next.deal (CD);} return;} /// /// data processing overload method . /// summary> /// TCPCLIENT class instance param> /// memory data stream param> /// Processing result, if you do not want the later processor to continue processing data, return false, otherwise returns true; returns> Public Virtual Bool Ondeal (CommData CD) {Return True;}
// Additional initialization operation Public Virtual Bool Init () {Return True;} // Advanced Initial Interface PUBLIC Virtual Bool BEFOREINIT () {Return True;} // Initialization Post Processing Interface Public Virtual Bool Afterinit () {Return True; } // Remove the pre-processing interface public virtual bool beforeeexit () {return true;} // Next observer observer next = null; public IOBSERVER NEXT {GET {return next;} set {next = value;}} // An observer IOBSERVER Prev = NULL; Public IOBSERVER Prev {Get {Return Prev;}} // Add Observer Public Void Add (IOBSERVER OBSERVER) {if (this == Observer) Return; // There is already IF (next == null) {next = Observer; // reaches the bottom NEXT.PREV = this;} else next.add (observer); // Add to the back} /// //////////// //// Insert the observer to the next observer's front /// summary> /// param> public void insert (IOBSERVER OBSERVER) {// is equal to ourselves IF (this == Observer) Return; // Find if there is already a linked list if (Next! = null && next.isexist (observer)) Next.remove (OBServer); // Observer.next = next; if (next! = Null) Next.Prev = Observer; Next = Observer; Observer.Prev = this;} /// /// Delete observation /// summary> /// param> public void remove (iBServer observer) {if (observer == this) {if (prev! = Null) prev.next = Next; if (next! = Null) Next.prev = prev;} else {if (next! = Null) Next.Remove (OBServer);}} /// / / / Find if /// < / summary> ///
"OBSERVER"> param> /// returns> Public Bool ISEXIST (IOBSERVER OBSERVER) {if (observer == this) Return true; if (next == null) Return False; Else Return NEXT. ISEXIST (OBSERVER);}} /// /// log processing base class, it is derived from the base class of the communication data processing class. /// summary> Class log: observer {public log () {} public log (String slogfile) {logfile = slogfile;} ~ log () {} // log file with path name Private string logfile; public string logfile {Get {Return Logfile;} set {logfile = value;}}}
Using system.collection; using system.ioting;
Namespace jh.sockets {/// /// Data Concentration Management Class /// summary> public class command_ = 0; int ID; /// /// no parameter construction Method /// summary> public command () {id = index ; dnflag = new parm (this); console.writeline ("Jh Data Distributor {0} constructed", ID);} thread t = null; public void Start () {if (dnFlag.Status = JhThreadStatus.Running!) {dnFlag.Status = JhThreadStatus.Running; t = new Thread (new ThreadStart (DataNotiyfyThread)); t.Name = "JH data distributing" id. Tostring (); t.start (); console.writeline ("Jh Data Distributing Thread of Jh Data Distributor {0} Started", ID);}} public void stop ()}} public void stop ()}} public void stop ()}} public void stop ()}}} ) {Lock (this) {dnflag.status = jhthreadstatus.stop;}}
} /// /// Destructure Method /// summary> ~ command () {stop (); console.writeline ("jh data distributor {0} unconstructed", ID);} public void datanotiyfythread () {Try {while (dnflag.status == jhthreadstatus.Running) {deALDATA (); thread.sleep (100); // console.writeLine ("Distribution Currency {0} Work", ID);}} catch (Exception e) {Console.WriteLine (e.Message);} finally {lock (this) {dnFlag.Status = JhThreadStatus.Exit;} Console.WriteLine ( "JH data distributing thread of JH data distributor {0} exited", ID);}} PARM DNFLAG = NULL; ArrayList Adata = New ArrayList (); InnerobServer root = new innerobserver (); /// /// observer root node class definition /// summary> Internal Class InnerObserver: Observer {} public void deALDATA () {if (this) {if (root.next! = Null) {root.next.deal ((CommData) Adata [0]);} Removeat (0);}}} /// /// unprocessed data number /// summary> public int int INT DataUnt {get {return aparta.count;}} /// // / / / ///////////////////////////// / summary> INTERNAL COMMDATA THIS [INDEX] {Return (CommData) Adata [Index ];}} /// /// Delete data /// summary> /// Index param> public void removeat (int index) {ix INDEX> = 0 && index // ////// summary> ////// Client connection param> /// data flow param>
Public Void Adddata (Jhclient Client, MemoryStream S, INT LEN) {CommData CD = New Commdata (Client, S, Len, This); Lock (this) {adction.add (cd);}} /// / // Add observer /// summary> /// param> public void address {loc (this) {root.add (observer);}} / // /// Delete Observer /// summary> /// param> public void remove (IOBSERVER OBSERVER) {Lock (this) {root.remove Observer);}} /// /// Insert the observer //////// summary> /// param> public void insert (IOBSERVER OBSERVER) { Lock (this) {root.insert (observer);} // Take the observer // int getobservercount () // {// return (int) m_aobservers.getCount (); //}}