Author: arty Date: Sat Aug 9 07:33:02 2008 New Revision: 35222
URL: http://svn.reactos.org/svn/reactos?rev=35222&view=rev Log: Sorry, forgotten file from Gregor Schnieder's patch.
Added: trunk/tools/reactosdbg/Pipe/serialpipe.cs Modified: trunk/tools/reactosdbg/Pipe/socketpipe.cs
Added: trunk/tools/reactosdbg/Pipe/serialpipe.cs URL: http://svn.reactos.org/svn/reactos/trunk/tools/reactosdbg/Pipe/serialpipe.cs... ============================================================================== --- trunk/tools/reactosdbg/Pipe/serialpipe.cs (added) +++ trunk/tools/reactosdbg/Pipe/serialpipe.cs [iso-8859-1] Sat Aug 9 07:33:02 2008 @@ -1,0 +1,88 @@ +using System; +using System.Text; +using System.IO.Ports; + +namespace AbstractPipe +{ + public class SerialPipe : Pipe + { + SerialPort mSerialPort; + int mBufSize; + + public event PipeReceiveEventHandler PipeReceiveEvent; + public event PipeErrorEventHandler PipeErrorEvent; + + public SerialPipe(SerialPort port) + { + mSerialPort = port; + mBufSize = mSerialPort.WriteBufferSize; + //try to get older input once without timeout (to avoid blocking behaviour) + mSerialPort.ReadTimeout = 10; + try + { + String buf = mSerialPort.ReadLine(); + if (buf.Length > 0 && PipeReceiveEvent != null) + PipeReceiveEvent.Invoke(this, new PipeReceiveEventArgs(buf)); + } + catch (TimeoutException) { } + catch (Exception e) + { + if (PipeErrorEvent != null) + PipeErrorEvent.Invoke(this, new PipeErrorEventArgs(e.Message)); + } + mSerialPort.ReadTimeout = -1; + //set up internal handlers + mSerialPort.DataReceived += SerialPortDataReceived; + mSerialPort.ErrorReceived += SerialPortErrorReceived; + } + + public bool Write(string output) + { + lock (this) + { + if (mSerialPort == null) return false; + try + { + byte[] outbuf = UTF8Encoding.UTF8.GetBytes(output); + int off = 0, len; + //supply the output according to the buffer size, might not be needed + do + { + if (output.Length - off > mBufSize) + { + len = mBufSize; + } + else + { + len = output.Length - off; + } + mSerialPort.Write(outbuf, off, len); + off += len; + } + while (off < outbuf.Length); + return off == outbuf.Length; + } + catch (Exception e) + { + mSerialPort.Close(); + if (PipeErrorEvent != null) + PipeErrorEvent.Invoke(this, new PipeErrorEventArgs(e.Message)); + return false; + } + } + } + + public void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs args) + { + if (PipeReceiveEvent != null) + PipeReceiveEvent.Invoke(this, new PipeReceiveEventArgs(mSerialPort.ReadExisting())); + } + + public void SerialPortErrorReceived(object sender, SerialErrorReceivedEventArgs args) + { + mSerialPort.Close(); + if (PipeErrorEvent != null) + PipeErrorEvent.Invoke(this, new PipeErrorEventArgs(args.EventType.ToString())); + } + } +}
Modified: trunk/tools/reactosdbg/Pipe/socketpipe.cs URL: http://svn.reactos.org/svn/reactos/trunk/tools/reactosdbg/Pipe/socketpipe.cs... ============================================================================== --- trunk/tools/reactosdbg/Pipe/socketpipe.cs [iso-8859-1] (original) +++ trunk/tools/reactosdbg/Pipe/socketpipe.cs [iso-8859-1] Sat Aug 9 07:33:02 2008 @@ -15,28 +15,31 @@
public bool Write(string output) { - if (mSocket == null) return false; - try + lock (this) { - byte[] outbuf = UTF8Encoding.UTF8.GetBytes(output); - int off = 0, res; - do + if (mSocket == null) return false; + try { - res = mSocket.Send(outbuf, off, outbuf.Length - off, SocketFlags.None); - if (res > 0) - off += res; + byte[] outbuf = UTF8Encoding.UTF8.GetBytes(output); + int off = 0, res; + do + { + res = mSocket.Send(outbuf, off, outbuf.Length - off, SocketFlags.None); + if (res > 0) + off += res; + } + while (off < outbuf.Length && res != -1); + return off == outbuf.Length; } - while (off < outbuf.Length && res != -1); - return off == outbuf.Length; + catch (System.Net.Sockets.SocketException se) + { + mSocket.Close(); + if (PipeErrorEvent != null) + PipeErrorEvent.Invoke(this, new PipeErrorEventArgs(se.Message)); + return false; + } + catch (Exception) { return false; } } - catch (System.Net.Sockets.SocketException se) - { - mSocket.Close(); - if (PipeErrorEvent != null) - PipeErrorEvent.Invoke(this, new PipeErrorEventArgs(se.Message)); - return false; - } - catch (Exception) { return false; } }
public void TriggerReadable(IAsyncResult result)