📄 asynctcpsession.cs
字号:
namespace NCindy.Session.AIO
{
using NCindy;
using NCindy.Packet;
using NCindy.Util.Logging;
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading;
public class AsyncTcpSession : AsyncSocketSession
{
private static readonly ILogger log = LogFactory.CreateLogger(MethodBase.GetCurrentMethod().ReflectedType);
protected AsyncCallback socketEndSendFileCallback;
private ManualResetEvent startWaitHandle;
public event EventHandler<SessionEventArgs> FileSent;
public AsyncTcpSession(IPEndPoint remoteIPEndPoint) : base(SessionType.Tcp, remoteIPEndPoint)
{
this.socketEndSendFileCallback = new AsyncCallback(this.SocketEndSendFile);
}
public AsyncTcpSession(Socket socket) : base(socket)
{
base.sessionType = SessionType.Tcp;
this.socketEndSendFileCallback = new AsyncCallback(this.SocketEndSendFile);
base.State = SessionState.Opened;
base.reuseSocket = true;
}
protected override void DoBeginReceive(IBuffer buffer)
{
base.pendingReceiveResult = base.innerSocket.BeginReceive(buffer.GetInnerByteArray(), 0, 0x2000, SocketFlags.None, base.socketEndRecvCallback, buffer);
}
protected override IAsyncResult DoBeginSend(byte[] content, int offset, int size, object state)
{
return base.innerSocket.BeginSend(content, offset, size, SocketFlags.None, base.socketEndSendCallback, state);
}
protected override void DoClose()
{
base.innerSocket.Shutdown(SocketShutdown.Both);
if (base.reuseSocket)
{
base.innerSocket.Disconnect(true);
}
else
{
base.innerSocket.Close();
}
}
protected override IPacket DoEndReceive(IAsyncResult asyncResult)
{
IBuffer content = (IBuffer) asyncResult.AsyncState;
int num = base.innerSocket.EndReceive(asyncResult);
if (log.IsInfoEnabled)
{
log.Info("Received size: " + num);
}
if (num <= 0)
{
content.Release();
throw new RemoteSocketClosedException();
}
content.Limit = num;
return new DefaultPacket(content, this.RemoteEndPoint);
}
protected override void InternalOpen(AsyncFuture future)
{
this.startWaitHandle = new ManualResetEvent(false);
base.innerSocket.BeginConnect(this.RemoteEndPoint, new AsyncCallback(this.SocketEndConnect), future);
future.AddWaitHandle(this.startWaitHandle);
}
public IFuture SendFile(string fileName, byte[] preBuffer, byte[] postBuffer)
{
IFuture future2;
AsyncFuture future = new AsyncFuture(this);
future.AssociatedObject = fileName;
try
{
future.AsyncResult = base.innerSocket.BeginSendFile(fileName, preBuffer, postBuffer, 0, this.socketEndSendFileCallback, future);
future2 = future;
}
catch (Exception exception)
{
base.SessionCaughtException(exception);
throw;
}
return future2;
}
protected void SocketEndConnect(IAsyncResult asyncResult)
{
AsyncFuture asyncState = (AsyncFuture) asyncResult.AsyncState;
try
{
base.innerSocket.EndConnect(asyncResult);
base.State = SessionState.Opened;
asyncState.IsSucceeded = true;
base.SocketBeginReceive();
}
catch (Exception exception)
{
asyncState.IsSucceeded = false;
base.State = SessionState.Initial;
base.SessionCaughtException(exception);
}
finally
{
if (this.startWaitHandle != null)
{
this.startWaitHandle.Set();
}
}
}
public void SocketEndSendFile(IAsyncResult asyncResult)
{
AsyncFuture asyncState = (AsyncFuture) asyncResult.AsyncState;
if (log.IsInfoEnabled)
{
log.Info("Sent file: " + asyncState.AssociatedObject);
}
try
{
base.innerSocket.EndSendFile(asyncResult);
}
catch (Exception exception)
{
base.SessionCaughtException(exception);
}
if (this.FileSent != null)
{
SessionEventArgs args = new SessionEventArgs(this, asyncState.AssociatedObject);
try
{
this.FileSent.Invoke(this, args);
}
catch (Exception exception2)
{
log.Warn("Process FileSent Event failed.", exception2);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -