本文整理汇总了C#中System.Net.Sockets.SocketAsyncResult类的典型用法代码示例。如果您正苦于以下问题:C# SocketAsyncResult类的具体用法?C# SocketAsyncResult怎么用?C# SocketAsyncResult使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SocketAsyncResult类属于System.Net.Sockets命名空间,在下文中一共展示了SocketAsyncResult类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BeginConnect
IAsyncResult BeginConnect (IPAddress[] addresses, int port, AsyncCallback callback, object state)
{
if (disposed && closed)
throw new ObjectDisposedException (GetType ().ToString ());
if (addresses == null)
throw new ArgumentNullException ("addresses");
if (addresses.Length == 0)
throw new ArgumentException ("Empty addresses list");
if (this.AddressFamily != AddressFamily.InterNetwork &&
this.AddressFamily != AddressFamily.InterNetworkV6)
throw new NotSupportedException ("This method is only valid for addresses in the InterNetwork or InterNetworkV6 families");
if (port <= 0 || port > 65535)
throw new ArgumentOutOfRangeException ("port", "Must be > 0 and < 65536");
if (islistening)
throw new InvalidOperationException ();
SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.Connect);
req.Addresses = addresses;
req.Port = port;
connected = false;
return BeginMConnect (req);
}
示例2: BeginSend
public IAsyncResult BeginSend (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, AsyncCallback callback, object state)
{
ThrowIfDisposedAndClosed ();
if (buffers == null)
throw new ArgumentNullException ("buffers");
if (!is_connected)
throw new SocketException ((int)SocketError.NotConnected);
SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.SendGeneric) {
Buffers = buffers,
SockFlags = socketFlags,
};
QueueIOSelectorJob (writeQ, sockares.Handle, new IOSelectorJob (IOOperation.Write, BeginSendGenericCallback, sockares));
return sockares;
}
示例3: BeginSendToCallback
static void BeginSendToCallback (SocketAsyncResult sockares, int sent_so_far)
{
int total = 0;
try {
total = sockares.socket.SendTo_nochecks (sockares.Buffer, sockares.Offset, sockares.Size, sockares.SockFlags, sockares.EndPoint);
if (sockares.error == 0) {
sent_so_far += total;
sockares.Offset += total;
sockares.Size -= total;
}
if (sockares.Size > 0) {
IOSelector.Add (sockares.Handle, new IOSelectorJob (IOOperation.Write, s => BeginSendToCallback ((SocketAsyncResult) s, sent_so_far), sockares));
return; // Have to finish writing everything. See bug #74475.
}
sockares.Total = sent_so_far;
} catch (Exception e) {
sockares.Complete (e);
return;
}
sockares.Complete ();
}
示例4: BeginReceive
public IAsyncResult BeginReceive (byte[] buffer, int offset, int size, SocketFlags socket_flags, AsyncCallback callback, object state)
{
ThrowIfDisposedAndClosed ();
ThrowIfBufferNull (buffer);
ThrowIfBufferOutOfRange (buffer, offset, size);
SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Receive) {
Buffer = buffer,
Offset = offset,
Size = size,
SockFlags = socket_flags,
};
QueueIOSelectorJob (readQ, sockares.Handle, new IOSelectorJob (IOOperation.Read, BeginReceiveCallback, sockares));
return sockares;
}
示例5: BeginReceiveFrom
public IAsyncResult BeginReceiveFrom (byte[] buffer, int offset, int size, SocketFlags socket_flags, ref EndPoint remote_end, AsyncCallback callback, object state)
{
ThrowIfDisposedAndClosed ();
ThrowIfBufferNull (buffer);
ThrowIfBufferOutOfRange (buffer, offset, size);
if (remote_end == null)
throw new ArgumentNullException ("remote_end");
SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.ReceiveFrom) {
Buffer = buffer,
Offset = offset,
Size = size,
SockFlags = socket_flags,
EndPoint = remote_end,
};
QueueIOSelectorJob (readQ, sockares.Handle, new IOSelectorJob (IOOperation.Read, BeginReceiveFromCallback, sockares));
return sockares;
}
示例6: BeginConnect
public IAsyncResult BeginConnect (EndPoint end_point, AsyncCallback callback, object state)
{
ThrowIfDisposedAndClosed ();
if (end_point == null)
throw new ArgumentNullException ("end_point");
SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Connect) {
EndPoint = end_point,
};
// Bug #75154: Connect() should not succeed for .Any addresses.
if (end_point is IPEndPoint) {
IPEndPoint ep = (IPEndPoint) end_point;
if (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any)) {
sockares.Complete (new SocketException ((int) SocketError.AddressNotAvailable), true);
return sockares;
}
}
int error = 0;
if (connect_in_progress) {
// This could happen when multiple IPs are used
// Calling connect() again will reset the connection attempt and cause
// an error. Better to just close the socket and move on.
connect_in_progress = false;
safe_handle.Dispose ();
safe_handle = new SafeSocketHandle (Socket_internal (address_family, socket_type, protocol_type, out error), true);
if (error != 0)
throw new SocketException (error);
}
bool blk = is_blocking;
if (blk)
Blocking = false;
Connect_internal (safe_handle, end_point.Serialize (), out error);
if (blk)
Blocking = true;
if (error == 0) {
// succeeded synch
is_connected = true;
is_bound = true;
sockares.Complete (true);
return sockares;
}
if (error != (int) SocketError.InProgress && error != (int) SocketError.WouldBlock) {
// error synch
is_connected = false;
is_bound = false;
sockares.Complete (new SocketException (error), true);
return sockares;
}
// continue asynch
is_connected = false;
is_bound = false;
connect_in_progress = true;
IOSelector.Add (sockares.Handle, new IOSelectorJob (IOOperation.Write, BeginConnectCallback, sockares));
return sockares;
}
示例7: BeginMConnect
internal IAsyncResult BeginMConnect (SocketAsyncResult sockares)
{
SocketAsyncResult ares = null;
Exception exc = null;
AsyncCallback callback;
for (int i = sockares.CurrentAddress; i < sockares.Addresses.Length; i++) {
try {
sockares.CurrentAddress++;
ares = (SocketAsyncResult) BeginConnect (new IPEndPoint (sockares.Addresses [i], sockares.Port), null, sockares);
if (ares.IsCompleted && ares.CompletedSynchronously) {
ares.CheckIfThrowDelayedException ();
callback = ares.AsyncCallback;
if (callback != null)
ThreadPool.UnsafeQueueUserWorkItem (_ => callback (ares), null);
}
break;
} catch (Exception e) {
exc = e;
ares = null;
}
}
if (ares == null)
throw exc;
return sockares;
}
示例8: Worker
public Worker (SocketAsyncResult ares)
{
this.result = ares;
}
示例9: socket_pool_queue
static extern void socket_pool_queue (SocketAsyncCall d, SocketAsyncResult r);
示例10: Connect
public void Connect ()
{
if (result.EndPoint == null) {
result.Complete (new SocketException ((int)SocketError.AddressNotAvailable));
return;
}
SocketAsyncResult mconnect = result.AsyncState as SocketAsyncResult;
bool is_mconnect = (mconnect != null && mconnect.Addresses != null);
try {
int error_code;
EndPoint ep = result.EndPoint;
error_code = (int) result.Sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
if (error_code == 0) {
if (is_mconnect)
result = mconnect;
result.Sock.seed_endpoint = ep;
result.Sock.connected = true;
result.Sock.isbound = true;
result.Sock.connect_in_progress = false;
result.error = 0;
result.Complete ();
if (is_mconnect)
result.DoMConnectCallback ();
return;
}
if (!is_mconnect) {
result.Sock.connect_in_progress = false;
result.Complete (new SocketException (error_code));
return;
}
if (mconnect.CurrentAddress >= mconnect.Addresses.Length) {
mconnect.Complete (new SocketException (error_code));
if (is_mconnect)
mconnect.DoMConnectCallback ();
return;
}
mconnect.Sock.BeginMConnect (mconnect);
} catch (Exception e) {
result.Sock.connect_in_progress = false;
if (is_mconnect)
result = mconnect;
result.Complete (e);
if (is_mconnect)
result.DoMConnectCallback ();
return;
}
}
示例11: DispatcherCB
static void DispatcherCB (SocketAsyncResult sar)
{
SocketOperation op = sar.operation;
if (op == Socket.SocketOperation.Receive || op == Socket.SocketOperation.ReceiveGeneric ||
op == Socket.SocketOperation.RecvJustCallback)
sar.Worker.Receive ();
else if (op == Socket.SocketOperation.Send || op == Socket.SocketOperation.SendGeneric ||
op == Socket.SocketOperation.SendJustCallback)
sar.Worker.Send ();
else if (op == Socket.SocketOperation.ReceiveFrom)
sar.Worker.ReceiveFrom ();
else if (op == Socket.SocketOperation.SendTo)
sar.Worker.SendTo ();
else if (op == Socket.SocketOperation.Connect)
sar.Worker.Connect ();
else if (op == Socket.SocketOperation.Accept)
sar.Worker.Accept ();
else if (op == Socket.SocketOperation.AcceptReceive)
sar.Worker.AcceptReceive ();
else if (op == Socket.SocketOperation.Disconnect)
sar.Worker.Disconnect ();
// SendPackets and ReceiveMessageFrom are not implemented yet
/*
else if (op == Socket.SocketOperation.ReceiveMessageFrom)
async_op = SocketAsyncOperation.ReceiveMessageFrom;
else if (op == Socket.SocketOperation.SendPackets)
async_op = SocketAsyncOperation.SendPackets;
*/
else
throw new NotImplementedException (String.Format ("Operation {0} is not implemented", op));
}
示例12: BeginMConnect
IAsyncResult BeginMConnect (SocketAsyncResult req)
{
IAsyncResult ares = null;
Exception exc = null;
for (int i = req.CurrentAddress; i < req.Addresses.Length; i++) {
IPAddress addr = req.Addresses [i];
IPEndPoint ep = new IPEndPoint (addr, req.Port);
try {
req.CurrentAddress++;
ares = BeginConnect (ep, null, req);
if (ares.IsCompleted && ares.CompletedSynchronously) {
((SocketAsyncResult) ares).CheckIfThrowDelayedException ();
req.DoMConnectCallback ();
}
break;
} catch (Exception e) {
exc = e;
ares = null;
}
}
if (ares == null)
throw exc;
return req;
}
示例13: BeginAccept
public IAsyncResult BeginAccept (int receiveSize, AsyncCallback callback, object state)
{
ThrowIfDisposedAndClosed ();
if (receiveSize < 0)
throw new ArgumentOutOfRangeException ("receiveSize", "receiveSize is less than zero");
SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.AcceptReceive) {
Buffer = new byte [receiveSize],
Offset = 0,
Size = receiveSize,
SockFlags = SocketFlags.None,
};
QueueIOSelectorJob (readQ, sockares.Handle, new IOSelectorJob (IOOperation.Read, BeginAcceptReceiveCallback, sockares));
return sockares;
}
示例14: BeginAccept
public IAsyncResult BeginAccept(AsyncCallback callback, object state)
{
if (disposed && closed)
throw new ObjectDisposedException (GetType ().ToString ());
if (!isbound || !islistening)
throw new InvalidOperationException ();
SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.Accept);
int count;
lock (readQ) {
readQ.Enqueue (req.Worker);
count = readQ.Count;
}
if (count == 1)
socket_pool_queue (Worker.Dispatcher, req);
return req;
}
示例15: BeginDisconnect
public IAsyncResult BeginDisconnect (bool reuseSocket, AsyncCallback callback, object state)
{
ThrowIfDisposedAndClosed ();
SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Disconnect) {
ReuseSocket = reuseSocket,
};
IOSelector.Add (sockares.Handle, new IOSelectorJob (IOOperation.Write, BeginDisconnectCallback, sockares));
return sockares;
}