本文整理汇总了C#中SocketAsyncEventArgs类的典型用法代码示例。如果您正苦于以下问题:C# SocketAsyncEventArgs类的具体用法?C# SocketAsyncEventArgs怎么用?C# SocketAsyncEventArgs使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SocketAsyncEventArgs类属于命名空间,在下文中一共展示了SocketAsyncEventArgs类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SocketAsyncEventCompleted
static void SocketAsyncEventCompleted(object sender, SocketAsyncEventArgs e)
{
e.Completed -= SocketAsyncEventCompleted;
var token = (ConnectToken)e.UserToken;
e.UserToken = null;
token.Callback(sender as Socket, token.State, e);
}
示例2: OnGetSocket
protected override void OnGetSocket(SocketAsyncEventArgs e)
{
try
{
#if SILVERLIGHT
var sslStream = new SslStream(new NetworkStream(Client));
sslStream.BeginAuthenticateAsClient(HostName, OnAuthenticated, sslStream);
#else
var securityOption = Security;
if (securityOption == null)
{
throw new Exception("securityOption was not configured");
}
#if NETSTANDARD
AuthenticateAsClientAsync(new SslStream(new NetworkStream(Client), false, ValidateRemoteCertificate), Security);
#else
var sslStream = new SslStream(new NetworkStream(Client), false, ValidateRemoteCertificate);
sslStream.BeginAuthenticateAsClient(HostName, securityOption.Certificates, securityOption.EnabledSslProtocols, false, OnAuthenticated, sslStream);
#endif
#endif
}
catch (Exception exc)
{
if (!IsIgnorableException(exc))
OnError(exc);
}
}
示例3: Success
public void Success()
{
ManualResetEvent completed = new ManualResetEvent(false);
if (Socket.OSSupportsIPv4)
{
using (Socket receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
int port = receiver.BindToAnonymousPort(IPAddress.Loopback);
receiver.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sender.Bind(new IPEndPoint(IPAddress.Loopback, 0));
sender.SendTo(new byte[1024], new IPEndPoint(IPAddress.Loopback, port));
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
args.SetBuffer(new byte[1024], 0, 1024);
args.Completed += OnCompleted;
args.UserToken = completed;
Assert.True(receiver.ReceiveMessageFromAsync(args));
Assert.True(completed.WaitOne(Configuration.PassingTestTimeout), "Timeout while waiting for connection");
Assert.Equal(1024, args.BytesTransferred);
Assert.Equal(sender.LocalEndPoint, args.RemoteEndPoint);
Assert.Equal(((IPEndPoint)sender.LocalEndPoint).Address, args.ReceiveMessageFromPacketInfo.Address);
sender.Dispose();
}
}
}
示例4: DisconnectAsync_Success
public void DisconnectAsync_Success()
{
AutoResetEvent completed = new AutoResetEvent(false);
IPEndPoint loopback = new IPEndPoint(IPAddress.Loopback, 0);
using (var server1 = SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, loopback))
using (var server2 = SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, loopback))
{
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.Completed += OnCompleted;
args.UserToken = completed;
args.RemoteEndPoint = server1.EndPoint;
args.DisconnectReuseSocket = true;
using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
Assert.True(client.ConnectAsync(args));
completed.WaitOne();
Assert.Equal(SocketError.Success, args.SocketError);
Assert.True(client.DisconnectAsync(args));
completed.WaitOne();
Assert.Equal(SocketError.Success, args.SocketError);
args.RemoteEndPoint = server2.EndPoint;
Assert.True(client.ConnectAsync(args));
completed.WaitOne();
Assert.Equal(SocketError.Success, args.SocketError);
}
}
}
示例5: Bot
public Bot(BotManager manager,BotDescriptor BotDesc, int BotId)
{
FBotId = BotId;
FManager = manager;
FBotDesc = BotDesc;
FBufferManager = new BufferManager();
FBufferManager.Initialize(100, 100);
FPacketSpliter = new PacketSpliter();
FPacketSpliter.OnPacket += OnPacket;
FConnArg = new SocketAsyncEventArgs();
FConnArg.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed);
FConnArg.UserToken = this;
FConnArg.RemoteEndPoint = FBotDesc.Host;
FDisconnArg = new SocketAsyncEventArgs();
FDisconnArg.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed);
FDisconnArg.UserToken = this;
FDisconnArg.DisconnectReuseSocket = true;
FSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
FTimer = new Timer(OnTimer);
}
示例6: StartReceiving
private void StartReceiving(object sender, SocketAsyncEventArgs args)
{
// если входящее подключение принято
switch (args.SocketError)
{
case SocketError.Success:
Log.Trace("{0} подключен", args.AcceptSocket.RemoteEndPoint);
var recieveArgs = _clientPool.TakeObject();
recieveArgs.AcceptSocket = args.AcceptSocket;
//args.AcceptSocket.BeginReceive
//if (!args.AcceptSocket.ReceiveAsync(recieveArgs.AcceptSocket))
ProcessReceive(recieveArgs);
break;
case SocketError.OperationAborted:
break;
default:
Log.Error("Подключение не принято: {0}", args.SocketError);
break;
}
// освобождаем принятый сокет и начинаем новый прием
args.AcceptSocket = null;
StartAccepting();
}
示例7: Success
public void Success()
{
AutoResetEvent completed = new AutoResetEvent(false);
if (Socket.OSSupportsIPv4)
{
int port, port1;
using (SocketTestServer.SocketTestServerFactory(IPAddress.Loopback, out port))
using (SocketTestServer.SocketTestServerFactory(IPAddress.Loopback, out port1))
{
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.Completed += OnCompleted;
args.UserToken = completed;
args.RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, port);
args.DisconnectReuseSocket = true;
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Assert.True(client.ConnectAsync(args));
Assert.True(completed.WaitOne(Configuration.PassingTestTimeout), "Timed out while waiting for connection");
Assert.Equal<SocketError>(SocketError.Success, args.SocketError);
client.Disconnect(true);
args.RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, port1);
Assert.True(client.ConnectAsync(args));
Assert.True(completed.WaitOne(Configuration.PassingTestTimeout), "Timed out while waiting for connection");
Assert.Equal<SocketError>(SocketError.Success, args.SocketError);
client.Dispose();
}
}
}
示例8: AsyncEventArgsCompleted
protected void AsyncEventArgsCompleted(object sender, SocketAsyncEventArgs e)
{
if (e.LastOperation == SocketAsyncOperation.Send)
ProcessSend(e);
else
ProcessReceive(e);
}
示例9: GetNonDefaultIPPacketInformation
private IPPacketInformation GetNonDefaultIPPacketInformation()
{
const int ReceiveTimeout = 5000;
using (var receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
using (var sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
int port = receiver.BindToAnonymousPort(IPAddress.Loopback);
var waitHandle = new ManualResetEvent(false);
SocketAsyncEventArgs receiveArgs = new SocketAsyncEventArgs {
RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, port),
UserToken = waitHandle
};
receiveArgs.SetBuffer(new byte[1], 0, 1);
receiveArgs.Completed += (_, args) => ((ManualResetEvent)args.UserToken).Set();
Assert.True(receiver.ReceiveMessageFromAsync(receiveArgs));
// Send a few packets, in case they aren't delivered reliably.
for (int i = 0; i < Configuration.UDPRedundancy; i++)
{
sender.SendTo(new byte[1], new IPEndPoint(IPAddress.Loopback, port));
}
Assert.True(waitHandle.WaitOne(ReceiveTimeout));
return receiveArgs.ReceiveMessageFromPacketInfo;
}
}
示例10: NewBuffer
private SocketAsyncEventArgs NewBuffer()
{
SocketAsyncEventArgs e = new SocketAsyncEventArgs();
e.SetBuffer(new byte[1024], 0, 1024);
return e;
}
示例11: Socket_ConnectAsyncDnsEndPoint_Success
public void Socket_ConnectAsyncDnsEndPoint_Success()
{
Assert.True(Capability.IPv4Support());
int port;
SocketTestServer server = SocketTestServer.SocketTestServerFactory(IPAddress.Loopback, out port);
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.RemoteEndPoint = new DnsEndPoint("localhost", port);
args.Completed += OnConnectAsyncCompleted;
ManualResetEvent complete = new ManualResetEvent(false);
args.UserToken = complete;
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Assert.True(sock.ConnectAsync(args));
complete.WaitOne();
Assert.Equal(SocketError.Success, args.SocketError);
Assert.Null(args.ConnectByNameError);
complete.Dispose();
sock.Dispose();
server.Dispose();
}
示例12: GetNonDefaultIPPacketInformation
private IPPacketInformation GetNonDefaultIPPacketInformation()
{
const int ReceiveTimeout = 5000;
using (var receiver = new Socket(SocketType.Dgram, ProtocolType.Udp))
using (var sender = new Socket(SocketType.Dgram, ProtocolType.Udp))
{
int port = receiver.BindToAnonymousPort(IPAddress.Loopback);
var waitHandle = new ManualResetEvent(false);
SocketAsyncEventArgs receiveArgs = new SocketAsyncEventArgs {
RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, port),
UserToken = waitHandle
};
receiveArgs.SetBuffer(new byte[1], 0, 1);
receiveArgs.Completed += (_, args) => ((ManualResetEvent)args.UserToken).Set();
Assert.True(receiver.ReceiveMessageFromAsync(receiveArgs));
sender.SendTo(new byte[1], new IPEndPoint(IPAddress.Loopback, port));
Assert.True(waitHandle.WaitOne(ReceiveTimeout));
return receiveArgs.ReceiveMessageFromPacketInfo;
}
}
示例13: BeginRequestTime
/// <summary>
/// Connects to the server and begins listening for time updates.
/// </summary>
public void BeginRequestTime()
{
byte[] buffer = new byte[48];
buffer[0] = 0x1B;
for (var i = 1; i < buffer.Length; ++i)
{
buffer[i] = 0;
}
var endPoint = new DnsEndPoint(_ServerAddress, 123);
_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
var socketArgs = new SocketAsyncEventArgs() { RemoteEndPoint = endPoint };
socketArgs.Completed += (o, e) =>
{
if (e.SocketError == SocketError.Success)
{
var sArgs = new SocketAsyncEventArgs()
{
RemoteEndPoint = endPoint
};
sArgs.Completed += new EventHandler<SocketAsyncEventArgs>(sArgs_Completed);
sArgs.SetBuffer(buffer, 0, buffer.Length);
sArgs.UserToken = buffer;
_Socket.SendAsync(sArgs);
}
};
_Socket.ConnectAsync(socketArgs);
}
示例14: makeSocketAsyncEventArgs
//生成自定义AsyncEventArg
private SocketAsyncEventArgs makeSocketAsyncEventArgs()
{
SocketAsyncEventArgs re = new SocketAsyncEventArgs();
re.UserToken = new AsyncUserToken(m_userTokenBufferLength);
re.SetBuffer(((AsyncUserToken)re.UserToken).buffer, 0, m_userTokenBufferLength);
re.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed);
return re;
}
示例15: SendData
public void SendData(string line)
{
byte[] data = System.Text.Encoding.UTF8.GetBytes(line + "\n");
SocketAsyncEventArgs socketAsyncData = new SocketAsyncEventArgs();
socketAsyncData.SetBuffer(data, 0, data.Length);
bool res = _clientSocket.SendAsync(socketAsyncData);
Debug.Log("Send line:" + line + " res:" + res);
}