本文整理汇总了C#中System.Net.Sockets.SocketAsyncEventArgs.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# SocketAsyncEventArgs.Dispose方法的具体用法?C# SocketAsyncEventArgs.Dispose怎么用?C# SocketAsyncEventArgs.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Sockets.SocketAsyncEventArgs
的用法示例。
在下文中一共展示了SocketAsyncEventArgs.Dispose方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Defaults
public void Defaults ()
{
SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
Assert.IsNull (saea.AcceptSocket, "AcceptSocket");
Assert.IsNull (saea.Buffer, "Buffer");
Assert.IsNull (saea.BufferList, "BufferList");
Assert.AreEqual (0, saea.BytesTransferred, "BytesTransferred");
Assert.AreEqual (0, saea.Count, "Count");
Assert.IsFalse (saea.DisconnectReuseSocket, "DisconnectReuseSocket");
Assert.AreEqual (SocketAsyncOperation.None, saea.LastOperation, "LastOperation");
Assert.AreEqual (0, saea.Offset, "Offset");
Assert.IsNull (saea.RemoteEndPoint, "RemoteEndPoint");
#if !MOBILE
Assert.IsNotNull (saea.ReceiveMessageFromPacketInfo, "ReceiveMessageFromPacketInfo");
Assert.IsNull (saea.SendPacketsElements, "SendPacketsElements");
Assert.AreEqual (TransmitFileOptions.UseDefaultWorkerThread, saea.SendPacketsFlags, "SendPacketsFlags");
#endif
Assert.AreEqual (-1, saea.SendPacketsSendSize, "SendPacketsSendSize");
Assert.AreEqual (SocketError.Success, saea.SocketError, "SocketError");
Assert.AreEqual (SocketFlags.None, saea.SocketFlags, "SocketFlags");
Assert.IsNull (saea.UserToken, "UserToken");
saea.Dispose ();
saea.Dispose (); // twice
}
示例2: Accept_Completed
static void Accept_Completed(object sender, SocketAsyncEventArgs e)
{
Socket client = e.AcceptSocket;
Socket server = sender as Socket;
if (sender == null) return;
SocketAsyncEventArgs sendArg = new SocketAsyncEventArgs();
SocketAsyncEventArgs receciveArg = new SocketAsyncEventArgs();
ConnectInfo info = new ConnectInfo();
info.tmpList = new ArrayList();
info.SendArg = sendArg;
info.ReceiveArg = receciveArg;
info.ServerSocket=server;
byte[] sendbuffers=Encoding.ASCII.GetBytes("welcome\n");
sendArg.SetBuffer(sendbuffers, 0, sendbuffers.Length);
sendbuffers=new byte[1024];
receciveArg.SetBuffer(sendbuffers, 0, 1024);
receciveArg.UserToken = info;
receciveArg.Completed += new EventHandler<SocketAsyncEventArgs>(Rececive_Completed);
client.SendAsync(sendArg);
client.ReceiveAsync(receciveArg);
e.Dispose();
}
示例3: Complete
void Complete(SocketAsyncEventArgs e, bool completeSynchronously)
{
TransportBase transport = null;
Exception exception = null;
if (e.SocketError != SocketError.Success)
{
exception = new SocketException((int)e.SocketError);
if (e.AcceptSocket != null)
{
e.AcceptSocket.Close(0);
}
}
else
{
Fx.Assert(e.AcceptSocket != null, "Must have a valid socket accepted.");
transport = new TcpTransport(e.AcceptSocket, this.transportSettings);
transport.Open();
}
e.Dispose();
this.callbackArgs.CompletedSynchronously = completeSynchronously;
this.callbackArgs.Exception = exception;
this.callbackArgs.Transport = transport;
if (!completeSynchronously)
{
this.callbackArgs.CompletedCallback(this.callbackArgs);
}
}
示例4: ConnectTo
public void ConnectTo(string ip, int port)
{
State = ConnectionState.NotConnected;
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress[] ips = Dns.GetHostAddresses(ip);
var addr = ips[0];
var hostEndPoint = new IPEndPoint(addr, port);
var connectEA = new SocketAsyncEventArgs();
connectEA.RemoteEndPoint = hostEndPoint;
connectEA.Completed += (object sender, SocketAsyncEventArgs e)=>{
switch (e.LastOperation)
{
case SocketAsyncOperation.Connect:
UnityEngine.Debug.Log("Connect success");
break;
default:
throw new ArgumentException("The last operation completed on the socket was not a receive or send");
}
connectEA.Dispose();
State = ConnectionState.Connected;
};
State = ConnectionState.Connecting;
socket.ConnectAsync(connectEA);
}
示例5: UdpCommunication
/// <summary>
/// Create a UdpCommunication object using an existing Socket.
/// </summary>
public UdpCommunication(Socket socket, IPEndPoint endPoint)
{
_socket = socket;
_endPoint = endPoint;
// Start asynchronous read
try
{
_receiveArgs = new SocketAsyncEventArgs();
_receiveArgs.UserToken = _socket;
_receiveArgs.RemoteEndPoint = _endPoint;
_readBuffer = new byte[READBUFFERSIZE];
_receiveArgs.SetBuffer(_readBuffer, 0, _readBuffer.Length);
_receiveArgs.Completed += new EventHandler<SocketAsyncEventArgs>(_socket_ReceivedData);
if (_socket.ReceiveFromAsync(_receiveArgs) == false) // Returns true if the I/O operation is pending. Returns false if the I/O operation completed synchronously and the SocketAsyncEventArgs.Completed event will not be raised.
_socket_ReceivedData(_socket, _receiveArgs);
}
catch (Exception ex)
{
// On failure free up the SocketAsyncEventArgs
if (_receiveArgs != null)
{
_receiveArgs.Completed -= new EventHandler<SocketAsyncEventArgs>(_socket_ReceivedData);
_receiveArgs.Dispose();
_receiveArgs = null;
}
throw;
}
}
示例6: ReceiveAsync
public System.Threading.Tasks.Task<ReceivedUdpData> ReceiveAsync()
{
ThrowIfDisposed();
var tcs = new TaskCompletionSource<ReceivedUdpData>();
var socketEventArg = new SocketAsyncEventArgs();
try
{
socketEventArg.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
socketEventArg.UserToken = tcs;
socketEventArg.SetBuffer(new Byte[SsdpConstants.DefaultUdpSocketBufferSize], 0, SsdpConstants.DefaultUdpSocketBufferSize);
socketEventArg.Completed += socketEventArg_ReceiveCompleted;
_Socket.ReceiveAsync(socketEventArg);
}
catch
{
socketEventArg.Dispose();
throw;
}
return tcs.Task;
}
示例7: FreeScEv
public static void FreeScEv(SocketAsyncEventArgs buf)
{
if (_evPool == null) {
return;
}
if (!_evPool.FreeObj(buf)) {
buf.Dispose();
}
}
示例8: Release
public void Release(SocketAsyncEventArgs args)
{
lock (_pool)
{
if (args.Buffer.Equals(_buffer))
_pool.Push(args);
else
args.Dispose();
}
_acquisitionGate.Release();
}
示例9: EndReceive
private void EndReceive(object sender, SocketAsyncEventArgs pArguments)
{
if (mDisconnected != 0) return;
if (pArguments.BytesTransferred <= 0)
{
Disconnect();
return;
}
mReceiveLength += pArguments.BytesTransferred;
if (mReceivingPacketLength == mReceiveLength)
{
if (mHeader) //parse headers
{
mReceivingPacketLength = BitConverter.ToInt32(mReceiveBuffer, 0);
mReceiveLength = 0;
mReceiveStart = 0;
mHeader = false;
// mReceiveBuffer = new byte[mReceivingPacketLength];
}
else
{ //parse packets
byte[] packetData = new byte[mReceivingPacketLength];
Buffer.BlockCopy(mReceiveBuffer, 0, packetData, 0, mReceivingPacketLength);
if (!mIVs)
{
InterPacket packet = new InterPacket(packetData);
if (packet.OpCode == InterHeader.IVS)
{
Log.WriteLine(LogLevel.Info, "IV data received");
packet.ReadBytes(mIVRecv);
mIVs = true;
}
else
{
Log.WriteLine(LogLevel.Info, "Got wrong packet.");
Disconnect();
}
}
else
{
packetData = InterCrypto.DecryptData(mIVRecv, packetData);
if (OnPacket != null)
{
InterPacket packet = new InterPacket(packetData);
this.OnPacket(this, new InterPacketReceivedEventArgs(packet, this));
}
}
//we reset this packet
mReceivingPacketLength = 4;
mReceiveLength = 0;
mReceiveStart = 0;
mHeader = true;
// mReceiveBuffer = new byte[4];
}
}
else
{
mReceiveStart += mReceivingPacketLength;
}
BeginReceive();
pArguments.Dispose();
}
示例10: ProduceEnd
private void ProduceEnd(ProduceEndType endType)
{
Action drained = () =>
{
switch (endType)
{
case ProduceEndType.SocketShutdownSend:
_socket.Shutdown(SocketShutdown.Send);
break;
case ProduceEndType.ConnectionKeepAlive:
ThreadPool.QueueUserWorkItem(_ => Go(true, null));
break;
case ProduceEndType.SocketDisconnect:
_services.Trace.Event(TraceEventType.Stop, TraceMessage.Connection);
_baton.Free();
var receiveSocketEvent = Interlocked.Exchange(ref _receiveSocketEvent, null);
// this has a race condition
if (receiveSocketEvent.Completed == null)
{
_services.Memory.FreeSocketEvent(receiveSocketEvent);
}
else
{
receiveSocketEvent.Completed = () => _services.Memory.FreeSocketEvent(receiveSocketEvent);
}
_socket.Shutdown(SocketShutdown.Receive);
var e = new SocketAsyncEventArgs();
Action cleanup = () =>
{
e.Dispose();
_disconnected(_socket);
};
e.Completed += (_, __) => cleanup();
if (!_socket.DisconnectAsync(e))
{
cleanup();
}
break;
}
};
if (!_socketSender.Flush(drained))
{
drained.Invoke();
}
}
示例11: BeginDisconnectCallbackAsync
public void BeginDisconnectCallbackAsync(object sender, SocketAsyncEventArgs e)
{
if (Disposed)
return;
BaseSocketConnection connection = null;
try
{
connection = (BaseSocketConnection)e.UserToken;
e.Completed -= new EventHandler<SocketAsyncEventArgs>(BeginDisconnectCallbackAsync);
e.UserToken = null;
e.Dispose();
e = null;
if (!connection.Active)
return;
lock (connection.Context.SyncActive)
{
CloseConnection(connection);
FireOnDisconnected(connection);
}
}
finally
{
Console.WriteLine(connection.Context.ConnectionId + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
DisposeConnection(connection);
RemoveSocketConnection(connection);
connection = null;
}
}
示例12: Complete
void Complete(SocketAsyncEventArgs e, bool completeSynchronously)
{
TransportBase transport = null;
Exception exception = null;
if (e.SocketError != SocketError.Success)
{
exception = new SocketException((int)e.SocketError);
if (e.AcceptSocket != null)
{
e.AcceptSocket.Dispose();
}
}
else
{
try
{
Fx.Assert(e.ConnectSocket != null, "Must have a valid socket accepted.");
e.ConnectSocket.NoDelay = true;
transport = new TcpTransport(e.ConnectSocket, this.transportSettings);
transport.Open();
}
catch (Exception exp)
{
if (Fx.IsFatal(exp))
{
throw;
}
exception = exp;
if (transport != null)
{
transport.SafeClose();
}
transport = null;
}
}
e.Dispose();
this.callbackArgs.CompletedSynchronously = completeSynchronously;
this.callbackArgs.Exception = exception;
this.callbackArgs.Transport = transport;
if (!completeSynchronously)
{
this.callbackArgs.CompletedCallback(this.callbackArgs);
}
}
示例13: ReceiveCompleted
private void ReceiveCompleted(object sender, SocketAsyncEventArgs e)
{
try
{
if (e.SocketError != SocketError.Success)
{
HandleSocketError(e.SocketError);
return;
}
// Check if remote host has closed connection.
if (e.BytesTransferred == 0)
{
Disconnect();
return;
}
// Indicate that block of data has been read into receive buffer.
this.receiveStream.WritePosition += e.BytesTransferred;
this.dataStreamReader.DiscardBufferedData();
// Read each terminated line of characters from data stream.
while (true)
{
// Read next line from data stream.
var line = this.dataStreamLineReader.ReadLine();
if (line == null)
break;
if (line.Length == 0)
continue;
ParseMessage(line);
}
// Continue reading data from socket.
ReceiveAsync();
}
catch (SocketException exSocket)
{
HandleSocketError(exSocket);
}
catch (ObjectDisposedException)
{
// Ignore.
}
#if !DEBUG
catch (Exception ex)
{
OnError(new IrcErrorEventArgs(ex));
}
#endif
finally
{
e.Dispose();
}
}
示例14: ConnectCompleted
private void ConnectCompleted(object sender, SocketAsyncEventArgs e)
{
try
{
if (e.SocketError != SocketError.Success)
{
HandleSocketError(e.SocketError);
return;
}
Debug.Assert(e.UserToken != null);
var token = (Tuple<bool, string, IrcRegistrationInfo>)e.UserToken;
// Create stream for received data. Use SSL stream on top of network stream, if specified.
this.receiveStream = new CircularBufferStream(socketReceiveBufferSize);
#if SILVERLIGHT
this.dataStream = this.receiveStream;
#else
this.dataStream = GetDataStream(token.Item1, token.Item2);
#endif
this.dataStreamReader = new StreamReader(this.dataStream, TextEncoding);
this.dataStreamLineReader = new SafeLineReader(this.dataStreamReader);
// Start sending and receiving data to/from server.
this.sendTimer.Change(0, Timeout.Infinite);
ReceiveAsync();
HandleClientConnected(token.Item3);
}
catch (SocketException exSocket)
{
HandleSocketError(exSocket);
}
catch (ObjectDisposedException)
{
// Ignore.
}
#if !DEBUG
catch (Exception ex)
{
OnConnectFailed(new IrcErrorEventArgs(ex));
}
#endif
finally
{
e.Dispose();
}
}
示例15: BeginConnectCallbackAsync
/// <summary>
/// Connect callback!
/// </summary>
/// <param name="ar"></param>
internal void BeginConnectCallbackAsync(object sender, SocketAsyncEventArgs e)
{
if (!Disposed)
{
BaseSocketConnection connection = null;
SocketConnector connector = null;
Exception exception = null;
if (e.SocketError == SocketError.Success)
{
try
{
connector = (SocketConnector)e.UserToken;
connection = new ClientSocketConnection(Context.Host, connector, connector.Socket);
//----- Adjust buffer size!
connector.Socket.ReceiveBufferSize = Context.Host.Context.SocketBufferSize;
connector.Socket.SendBufferSize = Context.Host.Context.SocketBufferSize; ;
//----- Initialize!
Context.Host.AddSocketConnection(connection);
connection.Active = true;
Context.Host.InitializeConnection(connection);
}
catch (Exception ex)
{
exception = ex;
if (connection != null)
{
Context.Host.DisposeConnection(connection);
Context.Host.RemoveSocketConnection(connection);
connection = null;
}
}
}
else
{
exception = new SocketException((int)e.SocketError);
}
if (exception != null)
{
FReconnectAttempted++;
ReconnectConnection(false, exception);
}
}
e.UserToken = null;
e.Dispose();
e = null;
}