本文整理汇总了C#中ITransport.AddEvent方法的典型用法代码示例。如果您正苦于以下问题:C# ITransport.AddEvent方法的具体用法?C# ITransport.AddEvent怎么用?C# ITransport.AddEvent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ITransport
的用法示例。
在下文中一共展示了ITransport.AddEvent方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Visit
public static void Visit(
IVisitorNetbiosReceiveLoop host, ITransport server,
NetbiosTransport transport, int localEP, int remoteEP, ThreadManager thread)
{
byte[] data = null;
while (!thread.ShouldExit)
{
try
{
// received data from server.
data = transport.Receive(remoteEP);
// if the server close the socket, return.
if (data == null)
{
server.AddEvent(new TransportEvent(EventType.Disconnected, remoteEP, localEP, null));
break;
}
host.VisitorAddReceivedData(ArrayUtility.SubArray<byte>(data, 0));
}
// the connection is disconnected.
catch (InvalidOperationException ex)
{
server.AddEvent(new TransportEvent(EventType.Disconnected, remoteEP, localEP, ex));
break;
}
catch (Exception ex)
{
// handle exception event, return.
server.AddEvent(new TransportEvent(EventType.Exception, remoteEP, localEP, ex));
break;
}
}
}
示例2: Visit
/// <summary>
/// the received loop for tcp connection.
/// </summary>
/// <param name="host">
/// an IVisitorTcpReceiveLoop interface that specifies the host of visitor.
/// </param>
/// <param name="server">
/// an ITransport object that provides AddEvent, it must be TcpClient or TcpServerConnection.
/// </param>
/// <param name="stream">
/// a Stream object that specifies the underlayer transport stream.<para/>
/// if DirectTcp, it's the stream of TcpClient.GetStream().<para/>
/// if Tcp over Ssl, it's SslStream.
/// </param>
/// <param name="thread">
/// a ThreadManager object that specifies the received thread.
/// </param>
/// <param name="bufferSize">
/// an int value that specifies the max buffer size.
/// </param>
public static void Visit(
IVisitorTcpReceiveLoop host, ITransport server,
Stream stream, ThreadManager thread, int bufferSize)
{
byte[] data = new byte[bufferSize];
while (!thread.ShouldExit)
{
int receivedLength = 0;
try
{
// received data from server.
receivedLength = stream.Read(data, 0, data.Length);
// if the server close the socket, return.
if (receivedLength == 0)
{
server.AddEvent(host.VisitorCreateTransportEvent(EventType.Disconnected, null));
break;
}
host.VisitorAddReceivedData(ArrayUtility.SubArray<byte>(data, 0, receivedLength));
}
catch (IOException ex)
{
SocketException socketException = ex.InnerException as SocketException;
// if the server disconnect the socket.
if (socketException != null
&& (socketException.SocketErrorCode == SocketError.ConnectionReset
|| socketException.SocketErrorCode == SocketError.Interrupted
|| socketException.SocketErrorCode == SocketError.ConnectionAborted))
{
// handle the disconnected event, return.
server.AddEvent(host.VisitorCreateTransportEvent(EventType.Disconnected, ex));
}
else
{
// handle exception event, return.
server.AddEvent(host.VisitorCreateTransportEvent(EventType.Exception, ex));
}
break;
}
}
}
示例3: Visit
public static void Visit(
IVisitorUdpReceiveLoop host, ITransport server,
UdpClient udpClient, ThreadManager thread, bool isLspHooked)
{
byte[] data = null;
// get the endpoint of tcp client.
IPEndPoint localEP = host.LspHookedLocalEP;
while (!thread.ShouldExit)
{
try
{
// received data from server.
IPEndPoint remoteEP = null;
data = udpClient.Receive(ref remoteEP);
if (data == null)
{
break;
}
if(isLspHooked)
{
int numBytesReceived = data.Length;
if (numBytesReceived < Marshal.SizeOf(typeof(LspUdpHeader)))
{
throw new FormatException("Invalid LSP udp packet received");
}
//Get udp header
byte[] udpHeaderBuffer = ArrayUtility.SubArray(data, 0, Marshal.SizeOf(typeof(LspUdpHeader)));
IntPtr p = Marshal.AllocHGlobal(udpHeaderBuffer.Length);
Marshal.Copy(udpHeaderBuffer, 0, p, udpHeaderBuffer.Length);
LspUdpHeader udpHeader = (LspUdpHeader)Marshal.PtrToStructure(p, typeof(LspUdpHeader));
Marshal.FreeHGlobal(p);
//get address
byte[] srcAddressArray = ArrayUtility.SubArray<byte>(data,
Marshal.SizeOf(typeof(LspUdpHeader)), udpHeader.HeaderLength -
Marshal.SizeOf(typeof(LspUdpHeader)));
string srcAddress = Encoding.ASCII.GetString(srcAddressArray);
//replacement
numBytesReceived = numBytesReceived - udpHeader.HeaderLength;
byte[] msgBody = new byte[numBytesReceived];
Array.Copy(data, udpHeader.HeaderLength, msgBody, 0, numBytesReceived);
//endPoint is real remote client endpoint, remoteEP is LspDll's endpoint
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(srcAddress), udpHeader.Port);
//map from the real endpoint to the lsp dll endpoint
//when sending response to the real endpoint, response will be sent to the lsp dll endpoint
//and the lsp dll will relay it to the real endpoint
LspConsole.Instance.SetMappedIPEndPoint(
(IPEndPoint)udpClient.Client.LocalEndPoint, (IPEndPoint)endPoint,
(IPEndPoint)remoteEP, StackTransportType.Udp);
host.VisitorAddReceivedData(msgBody, localEP, (IPEndPoint)endPoint);
}
else
{
host.VisitorAddReceivedData(data, localEP, remoteEP);
}
}
catch (Exception ex)
{
// handle exception event, return.
server.AddEvent(new TransportEvent(EventType.Exception, localEP, ex));
break;
}
}
}