本文整理汇总了C#中SocketAsyncOperation类的典型用法代码示例。如果您正苦于以下问题:C# SocketAsyncOperation类的具体用法?C# SocketAsyncOperation怎么用?C# SocketAsyncOperation使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SocketAsyncOperation类属于命名空间,在下文中一共展示了SocketAsyncOperation类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: NotLoggedFile
public static void NotLoggedFile(string filePath, Socket socket, SocketAsyncOperation completedOperation)
{
if (IsEnabled)
{
Log.NotLoggedFile(filePath, GetHashCode(socket), completedOperation);
}
}
示例2: CreateTaskFromCompletionHandler
private static Task CreateTaskFromCompletionHandler(SocketAsyncEventArgs socketAsyncEventArgs, SocketAsyncOperation socketAsyncOperation)
{
TaskCompletionSource<string> completionSource = new TaskCompletionSource<string>();
socketAsyncEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>((o, eventArgs) =>
{
if (eventArgs.LastOperation == socketAsyncOperation)
{
if (eventArgs.SocketError == SocketError.Success)
{
completionSource.SetResult("");
}
else
{
completionSource.SetException(new SocketException((int)eventArgs.SocketError));
}
}
});
return completionSource.Task;
}
示例3: TryUnsafeSocketOperation
private bool TryUnsafeSocketOperation(Socket socket, SocketAsyncOperation operation, SocketAsyncEventArgs socketAsyncEventArgs)
{
try
{
bool result = false;
switch (operation)
{
case SocketAsyncOperation.Accept:
result = socket.AcceptAsync(socketAsyncEventArgs);
break;
case SocketAsyncOperation.Send:
result = socket.SendAsync(socketAsyncEventArgs);
break;
case SocketAsyncOperation.Receive:
result = socket.ReceiveAsync(socketAsyncEventArgs);
break;
default:
throw new InvalidOperationException("Unknown case called, should program something for this");
}
if (!result)
{
OperationCallback(socket, socketAsyncEventArgs);
}
}
catch (SocketException ex)
{
if (operation != SocketAsyncOperation.Accept)
{
HandleCommunicationError(socket, ex);
}
return false;
}
catch (ObjectDisposedException)
{
// If disposed, handle communication error was already done and we're just catching up on other threads. suppress it.
return false;
}
return true;
}
示例4: StartOperationSendPackets
internal void StartOperationSendPackets() {
// Remember the operation type.
m_CompletedOperation = SocketAsyncOperation.SendPackets;
// Prevent mutithreaded manipulation of the list.
if (m_SendPacketsElements != null) {
m_SendPacketsElementsInternal = (SendPacketsElement[])m_SendPacketsElements.Clone();
}
// TransmitPackets uses an array of TRANSMIT_PACKET_ELEMENT structs as
// descriptors for buffers and files to be sent. It also takes a send size
// and some flags. The TRANSMIT_PACKET_ELEMENT for a file contains a native file handle.
// This function basically opens the files to get the file handles, pins down any buffers
// specified and builds the native TRANSMIT_PACKET_ELEMENT array that will be passed
// to TransmitPackets.
// Scan the elements to count files and buffers
m_SendPacketsElementsFileCount = 0;
m_SendPacketsElementsBufferCount = 0;
foreach (SendPacketsElement spe in m_SendPacketsElementsInternal) {
if(spe != null) {
if(spe.m_FilePath != null) {
m_SendPacketsElementsFileCount++;
}
if(spe.m_Buffer != null && spe.m_Count > 0) {
m_SendPacketsElementsBufferCount++;
}
}
}
// Attempt to open the files if any
if(m_SendPacketsElementsFileCount > 0) {
// Create arrays for streams and handles
m_SendPacketsFileStreams = new FileStream[m_SendPacketsElementsFileCount];
m_SendPacketsFileHandles = new SafeHandle[m_SendPacketsElementsFileCount];
// Loop through the elements attempting to open each files and get its handle
int index = 0;
foreach(SendPacketsElement spe in m_SendPacketsElementsInternal) {
if(spe != null && spe.m_FilePath != null) {
Exception fileStreamException = null;
try {
// Create a FileStream to open the file
m_SendPacketsFileStreams[index] =
new FileStream(spe.m_FilePath,FileMode.Open,FileAccess.Read,FileShare.Read);
}
catch (Exception ex) {
// Save the exception to throw after closing any previous successful file opens
fileStreamException = ex;
}
if (fileStreamException != null) {
// Got exception opening a file - do some cleanup then throw
for(int i = 0; i < m_SendPacketsElementsFileCount; i++) {
// Dereference handles
m_SendPacketsFileHandles[i] = null;
// Close any open streams
if(m_SendPacketsFileStreams[i] != null) {
m_SendPacketsFileStreams[i].Close();
m_SendPacketsFileStreams[i] = null;
}
}
throw fileStreamException;
}
// Get the file handle from the stream
ExceptionHelper.UnmanagedPermission.Assert();
try {
m_SendPacketsFileHandles[index] = m_SendPacketsFileStreams[index].SafeFileHandle;
}
finally {
SecurityPermission.RevertAssert();
}
index++;
}
}
}
CheckPinSendPackets();
}
示例5: StartOperationReceiveMessageFrom
internal void StartOperationReceiveMessageFrom() {
// Remember the operation type.
m_CompletedOperation = SocketAsyncOperation.ReceiveMessageFrom;
// WSARecvMsg uses a WSAMsg descriptor.
// The WSAMsg buffer is pinned with a GCHandle to avoid complicating the use of Overlapped.
// WSAMsg contains a pointer to a sockaddr.
// The sockaddr is pinned with a GCHandle to avoid complicating the use of Overlapped.
// WSAMsg contains a pointer to a WSABuffer array describing data buffers.
// WSAMsg also contains a single WSABuffer describing a control buffer.
//
PinSocketAddressBuffer();
// Create and pin a WSAMessageBuffer if none already.
if(m_WSAMessageBuffer == null) {
m_WSAMessageBuffer = new byte[s_WSAMsgSize];
m_WSAMessageBufferGCHandle = GCHandle.Alloc(m_WSAMessageBuffer, GCHandleType.Pinned);
m_PtrWSAMessageBuffer = Marshal.UnsafeAddrOfPinnedArrayElement(m_WSAMessageBuffer, 0);
}
// Create and pin an appropriately sized control buffer if none already
IPAddress ipAddress = (m_SocketAddress.Family == AddressFamily.InterNetworkV6
? m_SocketAddress.GetIPAddress() : null);
bool ipv4 = (m_CurrentSocket.AddressFamily == AddressFamily.InterNetwork
|| (ipAddress != null && ipAddress.IsIPv4MappedToIPv6)); // DualMode
bool ipv6 = m_CurrentSocket.AddressFamily == AddressFamily.InterNetworkV6;
if(ipv4 && (m_ControlBuffer == null || m_ControlBuffer.Length != s_ControlDataSize)) {
if(m_ControlBufferGCHandle.IsAllocated) {
m_ControlBufferGCHandle.Free();
}
m_ControlBuffer = new byte[s_ControlDataSize];
} else if(ipv6 && (m_ControlBuffer == null || m_ControlBuffer.Length != s_ControlDataIPv6Size)) {
if(m_ControlBufferGCHandle.IsAllocated) {
m_ControlBufferGCHandle.Free();
}
m_ControlBuffer = new byte[s_ControlDataIPv6Size];
}
if(!m_ControlBufferGCHandle.IsAllocated) {
m_ControlBufferGCHandle = GCHandle.Alloc(m_ControlBuffer, GCHandleType.Pinned);
m_PtrControlBuffer = Marshal.UnsafeAddrOfPinnedArrayElement(m_ControlBuffer, 0);
}
// If single buffer we need a pinned 1 element WSABuffer.
if(m_Buffer != null) {
if(m_WSARecvMsgWSABufferArray == null) {
m_WSARecvMsgWSABufferArray = new WSABuffer[1];
}
m_WSARecvMsgWSABufferArray[0].Pointer = m_PtrSingleBuffer;
m_WSARecvMsgWSABufferArray[0].Length = m_Count;
m_WSARecvMsgWSABufferArrayGCHandle = GCHandle.Alloc(m_WSARecvMsgWSABufferArray, GCHandleType.Pinned);
m_PtrWSARecvMsgWSABufferArray = Marshal.UnsafeAddrOfPinnedArrayElement(m_WSARecvMsgWSABufferArray, 0);
} else {
// just pin the multi-buffer WSABuffer
m_WSARecvMsgWSABufferArrayGCHandle = GCHandle.Alloc(m_WSABufferArray, GCHandleType.Pinned);
m_PtrWSARecvMsgWSABufferArray = Marshal.UnsafeAddrOfPinnedArrayElement(m_WSABufferArray, 0);
}
// Fill in WSAMessageBuffer
unsafe {
UnsafeNclNativeMethods.OSSOCK.WSAMsg* pMessage = (UnsafeNclNativeMethods.OSSOCK.WSAMsg*)m_PtrWSAMessageBuffer;;
pMessage->socketAddress = m_PtrSocketAddressBuffer;
pMessage->addressLength = (uint)m_SocketAddress.Size;
pMessage->buffers = m_PtrWSARecvMsgWSABufferArray;
if(m_Buffer != null) {
pMessage->count = (uint)1;
} else {
pMessage->count = (uint)m_WSABufferArray.Length;
}
if(m_ControlBuffer != null) {
pMessage->controlBuffer.Pointer = m_PtrControlBuffer;
pMessage->controlBuffer.Length = m_ControlBuffer.Length;
}
pMessage->flags = m_SocketFlags;
}
}
示例6: StartOperationWrapperConnect
internal void StartOperationWrapperConnect(MultipleConnectAsync args) {
m_CompletedOperation = SocketAsyncOperation.Connect;
m_MultipleConnect = args;
m_ConnectSocket = null;
}
示例7: StartOperationAccept
internal void StartOperationAccept() {
// Remember the operation type.
m_CompletedOperation = SocketAsyncOperation.Accept;
// AcceptEx needs a single buffer with room for two special sockaddr data structures.
// It can also take additional buffer space in front of those special sockaddr
// structures that can be filled in with initial data coming in on a connection.
// First calculate the special AcceptEx address buffer size.
// It is the size of two native sockaddr buffers with 16 extra bytes each.
// The native sockaddr buffers vary by address family so must reference the current socket.
m_AcceptAddressBufferCount = 2 * (m_CurrentSocket.m_RightEndPoint.Serialize().Size + 16);
// If our caller specified a buffer (willing to get received data with the Accept) then
// it needs to be large enough for the two special sockaddr buffers that AcceptEx requires.
// Throw if that buffer is not large enough.
if(m_Buffer != null) {
// Caller specified a buffer - see if it is large enough
if(m_Count < m_AcceptAddressBufferCount) {
throw new ArgumentException(SR.GetString(SR.net_buffercounttoosmall, "Count"));
}
// Buffer is already pinned.
} else {
// Caller didn't specify a buffer so use an internal one.
// See if current internal one is big enough, otherwise create a new one.
if(m_AcceptBuffer == null || m_AcceptBuffer.Length < m_AcceptAddressBufferCount) {
m_AcceptBuffer = new byte[m_AcceptAddressBufferCount];
}
CheckPinSingleBuffer(false);
}
}
示例8: StartOperationReceiveMessageFrom
internal unsafe void StartOperationReceiveMessageFrom()
{
this.m_CompletedOperation = SocketAsyncOperation.ReceiveMessageFrom;
this.PinSocketAddressBuffer();
if (this.m_WSAMessageBuffer == null)
{
this.m_WSAMessageBuffer = new byte[s_WSAMsgSize];
this.m_WSAMessageBufferGCHandle = GCHandle.Alloc(this.m_WSAMessageBuffer, GCHandleType.Pinned);
this.m_PtrWSAMessageBuffer = Marshal.UnsafeAddrOfPinnedArrayElement(this.m_WSAMessageBuffer, 0);
}
bool flag = this.m_CurrentSocket.AddressFamily == AddressFamily.InterNetwork;
bool flag2 = this.m_CurrentSocket.AddressFamily == AddressFamily.InterNetworkV6;
if (flag && ((this.m_ControlBuffer == null) || (this.m_ControlBuffer.Length != s_ControlDataSize)))
{
if (this.m_ControlBufferGCHandle.IsAllocated)
{
this.m_ControlBufferGCHandle.Free();
}
this.m_ControlBuffer = new byte[s_ControlDataSize];
}
else if (flag2 && ((this.m_ControlBuffer == null) || (this.m_ControlBuffer.Length != s_ControlDataIPv6Size)))
{
if (this.m_ControlBufferGCHandle.IsAllocated)
{
this.m_ControlBufferGCHandle.Free();
}
this.m_ControlBuffer = new byte[s_ControlDataIPv6Size];
}
if (!this.m_ControlBufferGCHandle.IsAllocated)
{
this.m_ControlBufferGCHandle = GCHandle.Alloc(this.m_ControlBuffer, GCHandleType.Pinned);
this.m_PtrControlBuffer = Marshal.UnsafeAddrOfPinnedArrayElement(this.m_ControlBuffer, 0);
}
if (this.m_Buffer != null)
{
if (this.m_WSARecvMsgWSABufferArray == null)
{
this.m_WSARecvMsgWSABufferArray = new WSABuffer[1];
}
this.m_WSARecvMsgWSABufferArray[0].Pointer = this.m_PtrSingleBuffer;
this.m_WSARecvMsgWSABufferArray[0].Length = this.m_Count;
this.m_WSARecvMsgWSABufferArrayGCHandle = GCHandle.Alloc(this.m_WSARecvMsgWSABufferArray, GCHandleType.Pinned);
this.m_PtrWSARecvMsgWSABufferArray = Marshal.UnsafeAddrOfPinnedArrayElement(this.m_WSARecvMsgWSABufferArray, 0);
}
else
{
this.m_WSARecvMsgWSABufferArrayGCHandle = GCHandle.Alloc(this.m_WSABufferArray, GCHandleType.Pinned);
this.m_PtrWSARecvMsgWSABufferArray = Marshal.UnsafeAddrOfPinnedArrayElement(this.m_WSABufferArray, 0);
}
UnsafeNclNativeMethods.OSSOCK.WSAMsg* ptrWSAMessageBuffer = (UnsafeNclNativeMethods.OSSOCK.WSAMsg*) this.m_PtrWSAMessageBuffer;
ptrWSAMessageBuffer->socketAddress = this.m_PtrSocketAddressBuffer;
ptrWSAMessageBuffer->addressLength = (uint) this.m_SocketAddress.Size;
ptrWSAMessageBuffer->buffers = this.m_PtrWSARecvMsgWSABufferArray;
if (this.m_Buffer != null)
{
ptrWSAMessageBuffer->count = 1;
}
else
{
ptrWSAMessageBuffer->count = (uint) this.m_WSABufferArray.Length;
}
if (this.m_ControlBuffer != null)
{
ptrWSAMessageBuffer->controlBuffer.Pointer = this.m_PtrControlBuffer;
ptrWSAMessageBuffer->controlBuffer.Length = this.m_ControlBuffer.Length;
}
ptrWSAMessageBuffer->flags = this.m_SocketFlags;
}
示例9: StartOperationReceiveMessageFrom
internal void StartOperationReceiveMessageFrom()
{
// Remember the operation type.
_completedOperation = SocketAsyncOperation.ReceiveMessageFrom;
InnerStartOperationReceiveMessageFrom();
}
示例10: StartOperationDisconnect
internal void StartOperationDisconnect()
{
// Remember the operation type.
_completedOperation = SocketAsyncOperation.Disconnect;
InnerStartOperationDisconnect();
}
示例11: StartOperationConnect
internal void StartOperationConnect()
{
// Remember the operation type.
_completedOperation = SocketAsyncOperation.Connect;
_multipleConnect = null;
_connectSocket = null;
InnerStartOperationConnect();
}
示例12: Register
public void Register(IActorRef connection, SocketAsyncOperation? initialOps)
{
_connection = connection;
}
示例13: DoOperation
internal void DoOperation (SocketAsyncOperation operation, Socket socket)
{
ThreadStart callback = null;
curSocket = socket;
switch (operation) {
case SocketAsyncOperation.Connect:
#if MOONLIGHT
socket.seed_endpoint = RemoteEndPoint;
#endif
callback = new ThreadStart (ConnectCallback);
SocketError = SocketError.Success;
LastOperation = operation;
break;
default:
throw new NotSupportedException ();
}
Thread t = new Thread (callback);
t.IsBackground = true;
t.Start ();
}
示例14: SetLastOperation
internal void SetLastOperation (SocketAsyncOperation op)
{
LastOperation = op;
}
示例15: ValidateCompletedEvent
private bool ValidateCompletedEvent(SocketAsyncEventArgs e, SocketAsyncOperation expected)
{
if (_error != null) return false;
if (e.SocketError != SocketError.Success)
{
SetError(e.SocketError);
return false;
}
if (e.LastOperation != expected)
{
SetError(new InvalidOperationException(string.Format(
"Excepted last operation {0} but last operation was {1}.",
expected,
e.LastOperation)));
return false;
}
return true;
}