protected virtual void OnServeCommand(ServerCommData data) { var handler = this.ServeCommand; if (handler != null) { handler( this, new ServeCommandEventArgs(data)); } }
/// <summary> /// Running thread handler /// </summary> protected override void Worker() { #if NET45 System.Diagnostics.Trace.WriteLine("start"); #elif MF_FRAMEWORK_VERSION_V4_1 || MF_FRAMEWORK_VERSION_V4_2 Microsoft.SPOT.Debug.Print("start"); #endif //loop, until the host closes while (this._closing == false) { //look for incoming data int length = this.Port.Available; if (length > 0) { var buffer = new byte[length]; EndPoint remote = new IPEndPoint(IPAddress.Any, 0); //read the data from the physical port this.Port.ReceiveFrom( buffer, ref remote); //try to decode the incoming data var data = new ServerCommData(this.Protocol); data.IncomingData = new ByteArrayReader(buffer); CommResponse result = this.Protocol .Codec .ServerDecode(data); if (result.Status == CommResponse.Ack) { //the command is recognized, so call the host back this.OnServeCommand(data); //encode the host data this.Protocol .Codec .ServerEncode(data, data.Address); //return the resulting data to the remote caller byte[] outgoing = ((IByteArray)data.OutgoingData).Data; this.Port.SendTo( outgoing, remote); } } Thread.Sleep(0); } #if NET45 System.Diagnostics.Trace.WriteLine("close"); #elif MF_FRAMEWORK_VERSION_V4_1 || MF_FRAMEWORK_VERSION_V4_2 Microsoft.SPOT.Debug.Print("close"); #endif //marks the server not running this.IsRunning = false; }
/// <summary> /// Running thread handler /// </summary> private void Worker() { #if NET45 System.Diagnostics.Trace.WriteLine("start"); #elif MF_FRAMEWORK_VERSION_V4_1 || MF_FRAMEWORK_VERSION_V4_2 Microsoft.SPOT.Debug.Print("start"); #endif //start the local timer, which gets the session dying int counter = IdleTimeout; int grace = 0; //create a writer for the incoming data ByteArrayWriter writer = null; var buffer = new byte[CacheSize]; using (Timer timer = new Timer( _ => { counter--; if (--grace == 0) { writer = null; } }, state: null, dueTime: 1000, period: 1000)) { //loop until the host closes, or the timer expires while ( this.Port.IsOpen && this._closing == false && counter > 0) { //look for incoming data int length = this.Port.BytesToRead; if (length > 0) { grace = 2; if (length > CacheSize) { length = CacheSize; } //read the data from the physical port this.Port.Read( buffer, 0, length); //append the data to the writer if (writer == null) { writer = new ByteArrayWriter(); } writer.WriteBytes( buffer, 0, length); //try to decode the incoming data var data = new ServerCommData(this.Protocol); data.IncomingData = writer.ToReader(); #if NET45 //System.Diagnostics.Trace.WriteLine("RX: " + ByteArrayHelpers.ToHex(((IByteArray)data.IncomingData).Data)); #elif MF_FRAMEWORK_VERSION_V4_1 || MF_FRAMEWORK_VERSION_V4_2 Microsoft.SPOT.Debug.Print("RX: " + ByteArrayHelpers.ToHex(((IByteArray)data.IncomingData).Data)); #endif CommResponse result = this.Protocol .Codec .ServerDecode(data); if (result.Status == CommResponse.Ack) { //the command is recognized, so call the host back this.OnServeCommand(data); //encode the host data this.Protocol .Codec .ServerEncode(data, 0); //return the resulting data to the remote caller byte[] outgoing = ((IByteArray)data.OutgoingData).Data; if (this.Port.IsOpen) { this.Port.Write( outgoing, 0, outgoing.Length); } else { break; } //reset the timer counter = IdleTimeout; writer = null; } //else if (result.Status == CommResponse.Ignore) //{ // writer = null; //} } Thread.Sleep(0); } } #if NET45 System.Diagnostics.Trace.WriteLine("close"); #elif MF_FRAMEWORK_VERSION_V4_1 || MF_FRAMEWORK_VERSION_V4_2 Microsoft.SPOT.Debug.Print("close"); #endif //marks the server not running this.IsRunning = false; }
/// <summary> /// Running thread handler /// </summary> protected override void Worker() { #if NET45 System.Diagnostics.Trace.WriteLine("start"); #elif MF_FRAMEWORK_VERSION_V4_1 || MF_FRAMEWORK_VERSION_V4_2 Microsoft.SPOT.Debug.Print("start"); #endif try { //start the local timer, which gets the session dying var counter = IdleTimeout; Port.ReceiveTimeout = 1000; var grace = 0; //create a writer for the incoming data ByteArrayWriter writer = null; var buffer = new byte[CacheSize]; //loop, until the host closes, or the timer expires while ( _closing == false && counter-- > 0) { if (--grace == 0) { writer = null; } //look for incoming data int length; try { //read the data from the physical port length = Port.Receive( buffer, SocketFlags.None); //check whether the remote has closed if (length == 0) { break; } } catch (ObjectDisposedException) { break; } catch (SocketException ex) { //no data received due socket timeout Debug.WriteLine(ex.Message); length = 0; } if (length > 0) { grace = 2; //append the data to the writer if (writer == null) { writer = new ByteArrayWriter(); } writer.WriteBytes( buffer, 0, length ); //try to decode the incoming data var data = new ServerCommData(this.Protocol); data.IncomingData = writer.ToReader(); CommResponse result; try { result = this.Protocol .Codec .ServerDecode(data); } catch (Exception ex) { Debug.WriteLine(ex.Message); result = new CommResponse(null, CommResponse.Ignore); } if (result.Status == CommResponse.Ack) { //the command is recognized, so call the host back OnServeCommand(data); //encode the host data Protocol .Codec .ServerEncode(data); //return the resulting data to the remote caller var outgoing = data.OutgoingData.ToByteArray(); if (Port.Connected) { try { Port.Send(outgoing); } catch (Exception ex) { Trace.TraceError(ex.Message); } } //reset the timer counter = IdleTimeout; writer = null; } else if (result.Status == CommResponse.Ignore) { writer = null; } } Thread.Sleep(0); } } catch (ObjectDisposedException) { } finally { //ensure the local socket disposal Port.Close(); } #if NET45 System.Diagnostics.Trace.WriteLine("close"); #elif MF_FRAMEWORK_VERSION_V4_1 || MF_FRAMEWORK_VERSION_V4_2 Microsoft.SPOT.Debug.Print("close"); #endif //marks the server not running IsRunning = false; Disconnected?.Invoke(); }