Sorry, forgotten file from Gregor Schnieder's patch.

svn path=/trunk/tools/reactosdbg/; revision=769
This commit is contained in:
Art Yerkes 2008-08-09 12:33:02 +00:00
parent 2b8cb53b6a
commit cc097eca1f
2 changed files with 109 additions and 18 deletions

88
Pipe/serialpipe.cs Normal file
View File

@ -0,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()));
}
}
}

View File

@ -15,28 +15,31 @@ namespace AbstractPipe
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)