本文整理汇总了C#中SocketFlags.HasFlag方法的典型用法代码示例。如果您正苦于以下问题:C# SocketFlags.HasFlag方法的具体用法?C# SocketFlags.HasFlag怎么用?C# SocketFlags.HasFlag使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SocketFlags
的用法示例。
在下文中一共展示了SocketFlags.HasFlag方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ToFrameClientObservable
public static IObservable<DisposableByteBuffer> ToFrameClientObservable(this Socket socket, SocketFlags socketFlags, BufferManager bufferManager, Selector selector)
{
return Observable.Create<DisposableByteBuffer>(observer =>
{
var headerState = new BufferState(new byte[sizeof(int)], 0, sizeof(int));
var contentState = new BufferState(null, 0, -1);
var selectMode = socketFlags.HasFlag(SocketFlags.OutOfBand) ? SelectMode.SelectError : SelectMode.SelectRead;
selector.AddCallback(selectMode, socket, _ =>
{
try
{
if (headerState.Length > 0)
{
headerState.Advance(socket.Receive(headerState.Bytes, headerState.Offset, headerState.Length, socketFlags));
if (headerState.Length == 0)
{
contentState.Length = BitConverter.ToInt32(headerState.Bytes, 0);
contentState.Offset = 0;
contentState.Bytes = bufferManager.TakeBuffer(contentState.Length);
}
}
if (contentState.Bytes != null)
{
contentState.Advance(socket.Receive(contentState.Bytes, contentState.Offset, contentState.Length, socketFlags));
if (contentState.Length == 0)
{
var managedBuffer = contentState.Bytes;
var length = contentState.Offset;
observer.OnNext(new DisposableByteBuffer(managedBuffer, length, Disposable.Create(() => bufferManager.ReturnBuffer(managedBuffer))));
contentState.Bytes = null;
headerState.Length = headerState.Offset;
headerState.Offset = 0;
}
}
}
catch (Exception exception)
{
if (!exception.IsWouldBlock())
observer.OnError(exception);
}
});
return Disposable.Create(() => selector.RemoveCallback(SelectMode.SelectRead, socket));
});
}