mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-05-06 11:32:59 +00:00
SSL
This commit is contained in:
parent
5991cec92d
commit
cca00ca97e
@ -306,9 +306,9 @@ namespace Esyur.Net.HTTP
|
|||||||
else
|
else
|
||||||
ipAdd = IPAddress.Parse(IP);
|
ipAdd = IPAddress.Parse(IP);
|
||||||
|
|
||||||
// if (ssl)
|
if (SSL)
|
||||||
// listener = new SSLSocket(new IPEndPoint(ipAdd, port), new X509Certificate2(certificate));
|
listener = new SSLSocket(new IPEndPoint(ipAdd, Port), new X509Certificate2(Certificate));
|
||||||
// else
|
else
|
||||||
listener = new TCPSocket(new IPEndPoint(ipAdd, Port));
|
listener = new TCPSocket(new IPEndPoint(ipAdd, Port));
|
||||||
|
|
||||||
Start(listener);
|
Start(listener);
|
||||||
|
@ -1200,7 +1200,7 @@ namespace Esyur.Net.IIP
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rt is System.Collections.IEnumerable && !(rt is Array || rt is Structure))
|
if (rt is System.Collections.IEnumerable && !(rt is Array || rt is Structure || rt is string))
|
||||||
{
|
{
|
||||||
var enu = rt as System.Collections.IEnumerable;
|
var enu = rt as System.Collections.IEnumerable;
|
||||||
|
|
||||||
@ -1361,7 +1361,7 @@ namespace Esyur.Net.IIP
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rt is System.Collections.IEnumerable && !(rt is Array))
|
if (rt is System.Collections.IEnumerable && !(rt is Array || rt is Structure || rt is string))
|
||||||
{
|
{
|
||||||
var enu = rt as System.Collections.IEnumerable;
|
var enu = rt as System.Collections.IEnumerable;
|
||||||
|
|
||||||
|
@ -43,15 +43,19 @@ namespace Esyur.Net.Sockets
|
|||||||
{
|
{
|
||||||
Socket sock;
|
Socket sock;
|
||||||
byte[] receiveBuffer;
|
byte[] receiveBuffer;
|
||||||
|
|
||||||
|
bool held;
|
||||||
|
|
||||||
|
//ArraySegment<byte> receiveBufferSegment;
|
||||||
|
|
||||||
NetworkBuffer receiveNetworkBuffer = new NetworkBuffer();
|
NetworkBuffer receiveNetworkBuffer = new NetworkBuffer();
|
||||||
|
|
||||||
object sendLock = new object();
|
readonly object sendLock = new object();
|
||||||
|
|
||||||
Queue<byte[]> sendBufferQueue = new Queue<byte[]>();
|
Queue<KeyValuePair<AsyncReply<bool>, byte[]>> sendBufferQueue = new Queue<KeyValuePair<AsyncReply<bool>, byte[]>>();// Queue<byte[]>();
|
||||||
|
|
||||||
bool asyncSending;
|
bool asyncSending;
|
||||||
|
bool began = false;
|
||||||
|
|
||||||
SocketState state = SocketState.Initial;
|
SocketState state = SocketState.Initial;
|
||||||
|
|
||||||
@ -60,80 +64,119 @@ namespace Esyur.Net.Sockets
|
|||||||
public event ISocketCloseEvent OnClose;
|
public event ISocketCloseEvent OnClose;
|
||||||
public event DestroyedEvent OnDestroy;
|
public event DestroyedEvent OnDestroy;
|
||||||
|
|
||||||
|
SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs();
|
||||||
|
|
||||||
SslStream ssl;
|
SslStream ssl;
|
||||||
X509Certificate2 cert;
|
X509Certificate2 cert;
|
||||||
bool server;
|
bool server;
|
||||||
string hostname;
|
string hostname;
|
||||||
|
|
||||||
private void Connected(Task t)
|
|
||||||
{
|
|
||||||
if (server)
|
|
||||||
{
|
|
||||||
ssl.AuthenticateAsServerAsync(cert).ContinueWith(Authenticated);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ssl.AuthenticateAsClientAsync(hostname).ContinueWith(Authenticated);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public AsyncReply<bool> Connect(string hostname, ushort port)
|
public async AsyncReply<bool> Connect(string hostname, ushort port)
|
||||||
{
|
{
|
||||||
var rt = new AsyncReply<bool>();
|
var rt = new AsyncReply<bool>();
|
||||||
|
|
||||||
|
state = SocketState.Connecting;
|
||||||
|
await sock.ConnectAsync(hostname, port);
|
||||||
|
|
||||||
|
if (server)
|
||||||
|
await ssl.AuthenticateAsServerAsync(cert);
|
||||||
|
else
|
||||||
|
await ssl.AuthenticateAsClientAsync(hostname);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
state = SocketState.Connecting;
|
state = SocketState.Established;
|
||||||
sock.ConnectAsync(hostname, port).ContinueWith((x) =>
|
OnConnect?.Invoke();
|
||||||
{
|
|
||||||
if (x.IsFaulted)
|
|
||||||
rt.TriggerError(x.Exception);
|
|
||||||
else
|
|
||||||
rt.Trigger(true);
|
|
||||||
|
|
||||||
Connected(x);
|
if (!server)
|
||||||
});
|
Begin();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
rt.TriggerError(ex);
|
state = SocketState.Terminated;
|
||||||
|
Close();
|
||||||
|
Global.Log(ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
return rt;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DataSent(Task task)
|
//private void DataSent(Task task)
|
||||||
{
|
//{
|
||||||
try
|
// try
|
||||||
{
|
// {
|
||||||
|
|
||||||
|
// if (sendBufferQueue.Count > 0)
|
||||||
|
// {
|
||||||
|
// byte[] data = sendBufferQueue.Dequeue();
|
||||||
|
// lock (sendLock)
|
||||||
|
// ssl.WriteAsync(data, 0, data.Length).ContinueWith(DataSent);
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// asyncSending = false;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// catch (Exception ex)
|
||||||
|
// {
|
||||||
|
// if (state != SocketState.Closed && !sock.Connected)
|
||||||
|
// {
|
||||||
|
// state = SocketState.Terminated;
|
||||||
|
// Close();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// asyncSending = false;
|
||||||
|
|
||||||
|
// Global.Log("SSLSocket", LogType.Error, ex.ToString());
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
private void SendCallback(IAsyncResult ar)
|
||||||
|
{
|
||||||
|
if (ar != null && ar.AsyncState != null)
|
||||||
|
((AsyncReply<bool>)ar.AsyncState).Trigger(true);
|
||||||
|
|
||||||
|
lock (sendLock)
|
||||||
|
{
|
||||||
if (sendBufferQueue.Count > 0)
|
if (sendBufferQueue.Count > 0)
|
||||||
{
|
{
|
||||||
byte[] data = sendBufferQueue.Dequeue();
|
var kv = sendBufferQueue.Dequeue();
|
||||||
lock (sendLock)
|
|
||||||
ssl.WriteAsync(data, 0, data.Length).ContinueWith(DataSent);
|
try
|
||||||
|
{
|
||||||
|
ssl.BeginWrite(kv.Value, 0, kv.Value.Length, SendCallback, kv.Key);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
asyncSending = false;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
kv.Key.Trigger(false);
|
||||||
|
|
||||||
|
if (state != SocketState.Closed && !sock.Connected)
|
||||||
|
{
|
||||||
|
state = SocketState.Terminated;
|
||||||
|
Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex2)
|
||||||
|
{
|
||||||
|
state = SocketState.Terminated;
|
||||||
|
}
|
||||||
|
|
||||||
|
Global.Log("TCPSocket", LogType.Error, ex.ToString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
asyncSending = false;
|
asyncSending = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
if (state != SocketState.Closed && !sock.Connected)
|
|
||||||
{
|
|
||||||
state = SocketState.Terminated;
|
|
||||||
Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
asyncSending = false;
|
|
||||||
|
|
||||||
Global.Log("SSLSocket", LogType.Error, ex.ToString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public IPEndPoint LocalEndPoint
|
public IPEndPoint LocalEndPoint
|
||||||
{
|
{
|
||||||
get { return (IPEndPoint)sock.LocalEndPoint; }
|
get { return (IPEndPoint)sock.LocalEndPoint; }
|
||||||
@ -185,14 +228,14 @@ namespace Esyur.Net.Sockets
|
|||||||
cert = certificate;
|
cert = certificate;
|
||||||
sock = Socket;
|
sock = Socket;
|
||||||
receiveBuffer = new byte[sock.ReceiveBufferSize];
|
receiveBuffer = new byte[sock.ReceiveBufferSize];
|
||||||
|
|
||||||
ssl = new SslStream(new NetworkStream(sock));
|
ssl = new SslStream(new NetworkStream(sock));
|
||||||
|
|
||||||
server = authenticateAsServer;
|
server = authenticateAsServer;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void Close()
|
public void Close()
|
||||||
{
|
{
|
||||||
if (state != SocketState.Closed && state != SocketState.Terminated)
|
if (state != SocketState.Closed && state != SocketState.Terminated)
|
||||||
@ -215,65 +258,137 @@ namespace Esyur.Net.Sockets
|
|||||||
OnClose?.Invoke();
|
OnClose?.Invoke();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void Send(byte[] message)
|
public void Send(byte[] message)
|
||||||
{
|
{
|
||||||
Send(message, 0, message.Length);
|
Send(message, 0, message.Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void Send(byte[] message, int offset, int size)
|
public void Send(byte[] message, int offset, int size)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
var msg = message.Clip((uint)offset, (uint)size);
|
||||||
|
|
||||||
lock (sendLock)
|
lock (sendLock)
|
||||||
{
|
{
|
||||||
if (asyncSending)
|
|
||||||
|
if (!sock.Connected)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (asyncSending || held)
|
||||||
{
|
{
|
||||||
sendBufferQueue.Enqueue(message.Clip((uint)offset, (uint)size));
|
sendBufferQueue.Enqueue(new KeyValuePair<AsyncReply<bool>, byte[]>(null, msg));// message.Clip((uint)offset, (uint)size));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
asyncSending = true;
|
asyncSending = true;
|
||||||
ssl.WriteAsync(message, offset, size).ContinueWith(DataSent);
|
try
|
||||||
|
{
|
||||||
|
ssl.BeginWrite(msg, 0, msg.Length, SendCallback, null);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
asyncSending = false;
|
||||||
|
state = SocketState.Terminated;
|
||||||
|
Close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Authenticated(Task task)
|
//public void Send(byte[] message)
|
||||||
{
|
//{
|
||||||
try
|
// Send(message, 0, message.Length);
|
||||||
{
|
//}
|
||||||
state = SocketState.Established;
|
|
||||||
OnConnect?.Invoke();
|
|
||||||
|
|
||||||
if (!server)
|
//public void Send(byte[] message, int offset, int size)
|
||||||
Begin();
|
//{
|
||||||
}
|
// lock (sendLock)
|
||||||
catch (Exception ex)
|
// {
|
||||||
|
// if (asyncSending)
|
||||||
|
// {
|
||||||
|
// sendBufferQueue.Enqueue(message.Clip((uint)offset, (uint)size));
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// asyncSending = true;
|
||||||
|
// ssl.WriteAsync(message, offset, size).ContinueWith(DataSent);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//private void DataReceived(Task<int> task)
|
||||||
|
//{
|
||||||
|
// try
|
||||||
|
// {
|
||||||
|
// if (state == SocketState.Closed || state == SocketState.Terminated)
|
||||||
|
// return;
|
||||||
|
|
||||||
|
// if (task.Result <= 0)
|
||||||
|
// {
|
||||||
|
// Close();
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)task.Result);
|
||||||
|
// OnReceive?.Invoke(receiveNetworkBuffer);
|
||||||
|
// if (state == SocketState.Established)
|
||||||
|
// ssl.ReadAsync(receiveBuffer, 0, receiveBuffer.Length).ContinueWith(DataReceived);
|
||||||
|
// }
|
||||||
|
// catch (Exception ex)
|
||||||
|
// {
|
||||||
|
// if (state != SocketState.Closed && !sock.Connected)
|
||||||
|
// {
|
||||||
|
// state = SocketState.Terminated;
|
||||||
|
// Close();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Global.Log("SSLSocket", LogType.Error, ex.ToString());
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
public bool Begin()
|
||||||
|
{
|
||||||
|
if (began)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
began = true;
|
||||||
|
|
||||||
|
if (state == SocketState.Established)
|
||||||
{
|
{
|
||||||
state = SocketState.Terminated;
|
ssl.BeginRead(receiveBuffer, 0, receiveBuffer.Length, ReceiveCallback, this);
|
||||||
Close();
|
return true;
|
||||||
Global.Log(ex);
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DataReceived(Task<int> task)
|
|
||||||
|
private void ReceiveCallback(IAsyncResult results)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// SocketError err;
|
if (state != SocketState.Established)
|
||||||
|
|
||||||
if (state == SocketState.Closed || state == SocketState.Terminated)
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (task.Result <= 0)
|
var bytesReceived = ssl.EndRead(results);
|
||||||
|
|
||||||
|
if (bytesReceived <= 0)
|
||||||
{
|
{
|
||||||
Close();
|
Close();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)bytesReceived);
|
||||||
|
|
||||||
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)task.Result);
|
|
||||||
OnReceive?.Invoke(receiveNetworkBuffer);
|
OnReceive?.Invoke(receiveNetworkBuffer);
|
||||||
if (state == SocketState.Established)
|
|
||||||
ssl.ReadAsync(receiveBuffer, 0, receiveBuffer.Length).ContinueWith(DataReceived);
|
ssl.BeginRead(receiveBuffer, 0, receiveBuffer.Length, ReceiveCallback, this);
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -287,19 +402,6 @@ namespace Esyur.Net.Sockets
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Begin()
|
|
||||||
{
|
|
||||||
if (state == SocketState.Established)
|
|
||||||
{
|
|
||||||
ssl.ReadAsync(receiveBuffer, 0, receiveBuffer.Length).ContinueWith(DataReceived);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public bool Trigger(ResourceTrigger trigger)
|
public bool Trigger(ResourceTrigger trigger)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
@ -313,50 +415,78 @@ namespace Esyur.Net.Sockets
|
|||||||
|
|
||||||
public async AsyncReply<ISocket> AcceptAsync()
|
public async AsyncReply<ISocket> AcceptAsync()
|
||||||
{
|
{
|
||||||
//var reply = new AsyncReply<ISocket>();
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return new SSLSocket(await sock.AcceptAsync(), cert, true);
|
var s = await sock.AcceptAsync();
|
||||||
|
return new SSLSocket(s, cert, true);
|
||||||
//sock.AcceptAsync().ContinueWith((x) =>
|
|
||||||
//{
|
|
||||||
// try
|
|
||||||
// {
|
|
||||||
// reply.Trigger(new SSLSocket(x.Result, cert, true));
|
|
||||||
// }
|
|
||||||
// catch
|
|
||||||
// {
|
|
||||||
// reply.Trigger(null);
|
|
||||||
// }
|
|
||||||
|
|
||||||
//}, null);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
state = SocketState.Terminated;
|
state = SocketState.Terminated;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
//return reply;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void Hold()
|
public void Hold()
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
held = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Unhold()
|
public void Unhold()
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
try
|
||||||
|
{
|
||||||
|
SendCallback(null);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
held = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public AsyncReply<bool> SendAsync(byte[] message, int offset, int length)
|
public AsyncReply<bool> SendAsync(byte[] message, int offset, int length)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
|
||||||
|
var msg = message.Clip((uint)offset, (uint)length);
|
||||||
|
|
||||||
|
lock (sendLock)
|
||||||
|
{
|
||||||
|
if (!sock.Connected)
|
||||||
|
return new AsyncReply<bool>(false);
|
||||||
|
|
||||||
|
var rt = new AsyncReply<bool>();
|
||||||
|
|
||||||
|
if (asyncSending || held)
|
||||||
|
{
|
||||||
|
sendBufferQueue.Enqueue(new KeyValuePair<AsyncReply<bool>, byte[]>(rt, msg));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
asyncSending = true;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ssl.BeginWrite(msg, 0, msg.Length, SendCallback, rt);// null);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
rt.TriggerError(ex);
|
||||||
|
asyncSending = false;
|
||||||
|
state = SocketState.Terminated;
|
||||||
|
Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rt;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public ISocket Accept()
|
public ISocket Accept()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
@ -128,21 +128,11 @@ namespace Esyur.Net.Sockets
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//if (receiveNetworkBuffer.Protected)
|
|
||||||
// Console.WriteLine();
|
|
||||||
|
|
||||||
//lock (receiveNetworkBuffer.SyncLock)
|
|
||||||
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)task.Result);
|
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)task.Result);
|
||||||
|
|
||||||
//Console.WriteLine("TC IN: " + (uint)task.Result + " " + DC.ToHex(receiveBuffer, 0, (uint)task.Result));
|
|
||||||
|
|
||||||
OnReceive?.Invoke(receiveNetworkBuffer);
|
OnReceive?.Invoke(receiveNetworkBuffer);
|
||||||
if (state == SocketState.Established)
|
if (state == SocketState.Established)
|
||||||
{
|
|
||||||
sock.ReceiveAsync(receiveBufferSegment, SocketFlags.None).ContinueWith(DataReceived);
|
sock.ReceiveAsync(receiveBufferSegment, SocketFlags.None).ContinueWith(DataReceived);
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -175,7 +165,7 @@ namespace Esyur.Net.Sockets
|
|||||||
OnReceive?.Invoke(receiveNetworkBuffer);
|
OnReceive?.Invoke(receiveNetworkBuffer);
|
||||||
|
|
||||||
if (state == SocketState.Established)
|
if (state == SocketState.Established)
|
||||||
while(!sock.ReceiveAsync(e))
|
while (!sock.ReceiveAsync(e))
|
||||||
{
|
{
|
||||||
if (e.SocketError != SocketError.Success)
|
if (e.SocketError != SocketError.Success)
|
||||||
{
|
{
|
||||||
@ -376,9 +366,10 @@ namespace Esyur.Net.Sockets
|
|||||||
asyncSending = true;
|
asyncSending = true;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, PacketSent, null);
|
sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, SendCallback, null);
|
||||||
}
|
}
|
||||||
catch {
|
catch
|
||||||
|
{
|
||||||
asyncSending = false;
|
asyncSending = false;
|
||||||
state = SocketState.Terminated;
|
state = SocketState.Terminated;
|
||||||
Close();
|
Close();
|
||||||
@ -389,7 +380,7 @@ namespace Esyur.Net.Sockets
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void PacketSent(IAsyncResult ar)
|
private void SendCallback(IAsyncResult ar)
|
||||||
{
|
{
|
||||||
if (ar != null && ar.AsyncState != null)
|
if (ar != null && ar.AsyncState != null)
|
||||||
((AsyncReply<bool>)ar.AsyncState).Trigger(true);
|
((AsyncReply<bool>)ar.AsyncState).Trigger(true);
|
||||||
@ -402,7 +393,7 @@ namespace Esyur.Net.Sockets
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
sock.BeginSend(kv.Value, 0, kv.Value.Length, SocketFlags.None, PacketSent, kv.Key);
|
sock.BeginSend(kv.Value, 0, kv.Value.Length, SocketFlags.None, SendCallback, kv.Key);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -482,7 +473,7 @@ namespace Esyur.Net.Sockets
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
PacketSent(null);
|
SendCallback(null);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -515,9 +506,9 @@ namespace Esyur.Net.Sockets
|
|||||||
asyncSending = true;
|
asyncSending = true;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, PacketSent, rt);// null);
|
sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, SendCallback, rt);// null);
|
||||||
}
|
}
|
||||||
catch(Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
rt.TriggerError(ex);
|
rt.TriggerError(ex);
|
||||||
asyncSending = false;
|
asyncSending = false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user