我正在编写一个从现有http服务器继承的https服务器。
Http服务器使用Socket
包裹public class ImprovedTCPServerSocket
{
private readonly Object m_SocketLocker = new Object();
protected Socket m_Socket;
public ImprovedTCPServerSocket(Socket mSocket)
{
m_Socket = mSocket;
}
public void Send(byte[] buffer, int offset, int size)
{
Socket mySocket;
lock (m_SocketLocker)
{
mySocket = m_Socket;
}
mySocket.Send(buffer, offset, size, SocketFlags.None);
}
protected int Receive(int requiredBytesToRead, byte[] receivedBytes, TimeSpan timeout, bool doReceive)
{
try
{
Socket mySocket;
lock (m_SocketLocker)
{
mySocket = m_Socket;
}
while (some condition){
currentBytesOffset += InternalReceive(mySocket, receivedBytes);
}
}
catch() {
.....
}
}
protected virtual int InternalReceive(Socket mySocket, byte[] receivedBytesת int currentBytesOffset)
{
currentBytesOffset += mySocket.Receive(receivedBytes, // collect given input.
currentBytesOffset, // offset.
bytesAvailableToRead, // number of bytes to read
flags,
out socketError);
return currentBytesOffset;
}
public virtual void Close()
{
m_Socket.Close();
}
}
,当发送/接收时,他正在锁定范围,如您所见:
ImprovedSslStream
现在为了实现https服务器,我创建了一个新的SslStream
类,它包裹ImprovedTCPServerSocket
并从SslStream
继承
我正在使用m_SslStream
我是否需要像m_Socket
那样对public class ImprovedSslStream : ImprovedTCPServerSocket
{
private X509Certificate m_Certificate;
private SslStream m_SslStream;
public ImprovedSslStream(Socket mSocket, X509Certificate serverCertificate) : base(mSocket)
{
m_Certificate = serverCertificate;
}
public void Send(byte[] buffer, int offset, int size)
{
//
sslStream.Write(.....)
}
protected override int GetCurrentByteOffset(Socket mySocket, byte[] receivedBytes, int currentBytesOffset,
int bytesAvailableToRead, bool doReceive)
{
m_SslStream=new SslStream(new NetworkStream(mySocket),false); //Do I need to lock it ?
try
{
m_SslStream.AuthenticateAsServer(m_Certificate,
false, SslProtocols.Tls, true);
currentBytesOffset+=m_SslStream.Read(receivedBytes, currentBytesOffset, bytesAvailableToRead);
}
catch (AuthenticationException e)
{
}
return currentBytesOffset;
}
}
执行相同的锁定,还是可以像这样使用它?
这是我的代码:
{{1}}
}