mirror of
https://github.com/reactos/reactosdbg.git
synced 2024-11-23 03:39:43 +00:00
e6810d7597
- Do not duplicate register and process updates upon breaking into kdbg, fixes multiple thread lists - Remove end line hack from named pipe implementation - Handle all exception types for sockets, fixes a crash when disconnecting while receiving data svn path=/trunk/tools/reactosdbg/; revision=1101
79 lines
2.6 KiB
C#
79 lines
2.6 KiB
C#
using System;
|
|
using System.Text;
|
|
using System.Net.Sockets;
|
|
|
|
namespace AbstractPipe
|
|
{
|
|
public class SocketPipe : Pipe
|
|
{
|
|
Socket mSocket;
|
|
byte []mBuf = new byte[4096];
|
|
IAsyncResult mResult;
|
|
|
|
public event PipeReceiveEventHandler PipeReceiveEvent;
|
|
public event PipeErrorEventHandler PipeErrorEvent;
|
|
|
|
public bool Write(string output)
|
|
{
|
|
lock (this)
|
|
{
|
|
if (mSocket == null) return false;
|
|
try
|
|
{
|
|
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;
|
|
}
|
|
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)
|
|
{
|
|
try
|
|
{
|
|
int bytes = mSocket.EndReceive(result);
|
|
string datastr = UTF8Encoding.UTF8.GetString(mBuf, 0, bytes);
|
|
if (PipeReceiveEvent != null)
|
|
PipeReceiveEvent.Invoke(this, new PipeReceiveEventArgs(datastr));
|
|
do
|
|
{
|
|
mResult = mSocket.BeginReceive
|
|
(mBuf, 0, mBuf.Length, SocketFlags.None, TriggerReadable, this);
|
|
} while (mResult.CompletedSynchronously);
|
|
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
mSocket.Close();
|
|
if (PipeErrorEvent != null)
|
|
PipeErrorEvent.Invoke(this, new PipeErrorEventArgs(e.Message));
|
|
}
|
|
}
|
|
|
|
public SocketPipe(Socket socket)
|
|
{
|
|
mSocket = socket;
|
|
do
|
|
{
|
|
mResult = mSocket.BeginReceive
|
|
(mBuf, 0, mBuf.Length, SocketFlags.None, TriggerReadable, this);
|
|
} while (mResult.CompletedSynchronously);
|
|
}
|
|
}
|
|
}
|