📄 authenticatedfilter.cs
字号:
namespace NCindy.Filter
{
using NCindy;
using NCindy.Session.AIO;
using NCindy.Util;
using NCindy.Util.Logging;
using System;
using System.IO;
using System.Net.Sockets;
using System.Threading;
public abstract class AuthenticatedFilter : SessionFilterAdapter
{
protected AuthFilterState filterState;
internal readonly DualBufferStream innerStream;
protected ISession session;
public AuthenticatedFilter()
{
this.innerStream = new DualBufferStream(this);
}
internal AuthFilterState FilterState
{
get
{
return this.filterState;
}
set
{
this.filterState = value;
}
}
internal ISession Session
{
get
{
return this.session;
}
set
{
this.session = value;
}
}
internal class DualBufferStream : Stream
{
private static readonly ILogger log = LogFactory.CreateLogger(MethodBase.GetCurrentMethod().ReflectedType);
private AuthenticatedFilter parentFilter;
private readonly MemoryStream readStream = new MemoryStream();
private SpinWaitLock readStreamLock = new SpinWaitLock();
private long readStreamReadPosition;
private long readStreamWritePosition;
private Socket socket;
private readonly MemoryStream writeStream = new MemoryStream();
private SpinWaitLock writeStreamLock = new SpinWaitLock();
private long writeStreamReadPosition;
private long writeStreamWritePosition;
public DualBufferStream(AuthenticatedFilter filter)
{
this.parentFilter = filter;
this.socket = ((AsyncSocketSession) this.parentFilter.Session).InnerSocket;
}
public override void Flush()
{
this.writeStream.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
return this.ReadFromRStream(buffer, offset, count);
}
internal int ReadFromRStream(byte[] buffer, int offset, int count)
{
int num;
if (log.IsInfoEnabled)
{
log.Info("Enter ReadStreamRead. SSLFilter.filterState = " + this.parentFilter.FilterState);
}
if (this.parentFilter.FilterState == AuthFilterState.Authenticating)
{
log.Info("readStreamWritePosition = " + this.readStreamWritePosition);
while (this.readStreamWritePosition <= 0)
{
Thread.Sleep(0x3e8);
}
}
if (log.IsInfoEnabled)
{
log.Info("DualStream Read. ReadStream position " + this.readStream.Position);
}
this.readStreamLock.Enter();
try
{
this.readStream.Position = this.readStreamReadPosition;
num = this.readStream.Read(buffer, offset, count);
this.readStreamReadPosition += num;
if (this.readStreamReadPosition == this.readStreamWritePosition)
{
if (log.IsInfoEnabled)
{
log.Info("Reset read stream position.");
}
this.readStreamReadPosition = this.readStreamWritePosition = 0;
}
}
finally
{
this.readStreamLock.Exit();
}
if (log.IsInfoEnabled)
{
log.Info("Read size: " + num);
}
return num;
}
internal int ReadFromWStream(byte[] buffer, int offset, int count)
{
if (log.IsInfoEnabled)
{
log.Info("Enter WriteStreamRead.");
}
int num = 0;
this.writeStreamLock.Enter();
try
{
if (log.IsInfoEnabled)
{
log.Info("Write stream read position = " + this.writeStreamReadPosition);
}
this.writeStream.Position = this.writeStreamReadPosition;
num = this.writeStream.Read(buffer, offset, count);
this.writeStreamReadPosition += num;
if (log.IsInfoEnabled)
{
log.Info("readSize = " + num);
}
if (this.writeStreamReadPosition != this.writeStreamWritePosition)
{
return num;
}
if (log.IsInfoEnabled)
{
log.Info("Reset write stream position.");
}
this.writeStreamReadPosition = this.writeStreamWritePosition = 0;
}
catch (Exception exception)
{
log.Error("WriteStreamRead failed.", exception);
}
finally
{
this.writeStreamLock.Exit();
}
return num;
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
this.WriteToWStream(buffer, offset, count);
}
internal void WriteToRStream(byte[] buffer, int offset, int count)
{
this.readStreamLock.Enter();
try
{
this.readStream.Position = this.readStreamWritePosition;
this.readStream.Write(buffer, offset, count);
this.readStreamWritePosition += count;
}
finally
{
this.readStreamLock.Exit();
}
}
internal void WriteToWStream(byte[] buffer, int offset, int count)
{
if (log.IsInfoEnabled)
{
log.Info("DualStream Write.");
}
if (this.parentFilter.FilterState == AuthFilterState.Authenticating)
{
if (log.IsInfoEnabled)
{
log.Info("DualStream Write direct through networkstream");
}
this.socket.Send(buffer, offset, count, SocketFlags.None);
}
else
{
this.writeStreamLock.Enter();
try
{
this.writeStream.Position = this.writeStreamWritePosition;
this.writeStream.Write(buffer, offset, count);
this.writeStreamWritePosition += count;
if (log.IsInfoEnabled)
{
log.Info("New write stream position = " + this.writeStreamWritePosition);
}
}
finally
{
this.writeStreamLock.Exit();
}
}
}
public override bool CanRead
{
get
{
return this.readStream.CanRead;
}
}
public override bool CanSeek
{
get
{
return false;
}
}
public override bool CanWrite
{
get
{
return this.writeStream.CanWrite;
}
}
public override long Length
{
get
{
throw new NotSupportedException();
}
}
public override long Position
{
get
{
throw new NotSupportedException();
}
set
{
throw new NotSupportedException();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -