Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add abstraction for Socket and SocketAsyncEventArgs #85

Open
wants to merge 5 commits into
base: v1.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions Common/BufferManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@ public class BufferManager
int m_currentIndex;
int m_bufferSize;

/// <summary>
/// Get the buffer
/// </summary>
public byte[] Buffer
{
get
{
return m_buffer;
}
}

/// <summary>
/// Get the buffer
/// </summary>
public int BufferSize
{
get
{
return m_bufferSize;
}
}

/// <summary>
/// Initializes a new instance of the <see cref="BufferManager"/> class.
/// </summary>
Expand All @@ -44,35 +66,35 @@ public void InitBuffer()
/// <summary>
/// Assigns a buffer from the buffer pool to the specified SocketAsyncEventArgs object
/// </summary>
/// <returns>true if the buffer was successfully set, else false</returns>
public bool SetBuffer(SocketAsyncEventArgs args)
/// <returns>A Tuple where Item1 is true if the buffer should be set, else false.
/// If Item1 is true then Item2 has the new offset, else 0.
/// </returns>
public Tuple<bool, int> SetBuffer()
{

int offset;
if (m_freeIndexPool.Count > 0)
{
args.SetBuffer(m_buffer, m_freeIndexPool.Pop(), m_bufferSize);
offset = m_freeIndexPool.Pop();
}
else
{
if ((m_numBytes - m_bufferSize) < m_currentIndex)
{
return false;
return new Tuple<bool, int>(false, 0);
}
args.SetBuffer(m_buffer, m_currentIndex, m_bufferSize);
offset = m_currentIndex;
m_currentIndex += m_bufferSize;
}
return true;
return new Tuple<bool, int>(true, offset);
}

/// <summary>
/// Removes the buffer from a SocketAsyncEventArg object. This frees the buffer back to the
/// Removes the buffer from a SocketAsyncEventArg object. This frees the buffer back to the
/// buffer pool
/// </summary>
public void FreeBuffer(SocketAsyncEventArgs args)
public void FreeBuffer(int offset)
{
m_freeIndexPool.Push(args.Offset);
args.SetBuffer(null, 0, 0);
m_freeIndexPool.Push(offset);
}

}
}
1 change: 0 additions & 1 deletion Common/SuperSocket.Common.Net35.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
<Compile Include="SearchMarkState.cs" />
<Compile Include="SendingQueue.cs" />
<Compile Include="SmartPool.cs" />
<Compile Include="SocketEx.cs" />
<Compile Include="StringExtension.cs" />
<Compile Include="StringExtension.NET35.cs" />
<Compile Include="TheadPoolEx.cs" />
Expand Down
1 change: 0 additions & 1 deletion Common/SuperSocket.Common.Net40.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
<Compile Include="SearchMarkState.cs" />
<Compile Include="SendingQueue.cs" />
<Compile Include="SmartPool.cs" />
<Compile Include="SocketEx.cs" />
<Compile Include="StringExtension.cs" />
<Compile Include="StringExtension.NET4.cs" />
<Compile Include="TheadPoolEx.cs" />
Expand Down
1 change: 0 additions & 1 deletion Common/SuperSocket.Common.Net45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
<Compile Include="SearchMarkState.cs" />
<Compile Include="SendingQueue.cs" />
<Compile Include="SmartPool.cs" />
<Compile Include="SocketEx.cs" />
<Compile Include="StringExtension.cs" />
<Compile Include="StringExtension.NET4.cs" />
<Compile Include="TheadPoolEx.cs" />
Expand Down
29 changes: 17 additions & 12 deletions Protocols/WebSocket/WebSocketServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketBase.Protocol;
using SuperSocket.SocketBase.Sockets;
using SuperSocket.WebSocket.Command;
using SuperSocket.WebSocket.Config;
using SuperSocket.WebSocket.Protocol;
Expand Down Expand Up @@ -54,8 +55,9 @@ public class WebSocketServer : WebSocketServer<WebSocketSession>
/// Initializes a new instance of the <see cref="WebSocketServer"/> class.
/// </summary>
/// <param name="subProtocols">The sub protocols.</param>
public WebSocketServer(IEnumerable<ISubProtocol<WebSocketSession>> subProtocols)
: base(subProtocols)
/// <param name="socketFactory"></param>
public WebSocketServer(IEnumerable<ISubProtocol<WebSocketSession>> subProtocols, ISocketFactory socketFactory = null)
: base(subProtocols, socketFactory)
{

}
Expand All @@ -64,17 +66,18 @@ public WebSocketServer(IEnumerable<ISubProtocol<WebSocketSession>> subProtocols)
/// Initializes a new instance of the <see cref="WebSocketServer"/> class.
/// </summary>
/// <param name="subProtocol">The sub protocol.</param>
public WebSocketServer(ISubProtocol<WebSocketSession> subProtocol)
: base(subProtocol)
/// <param name="socketFactory"></param>
public WebSocketServer(ISubProtocol<WebSocketSession> subProtocol, ISocketFactory socketFactory = null)
: base(subProtocol, socketFactory)
{

}

/// <summary>
/// Initializes a new instance of the <see cref="WebSocketServer"/> class.
/// </summary>
public WebSocketServer()
: base(new List<ISubProtocol<WebSocketSession>>())
public WebSocketServer(ISocketFactory socketFactory = null)
: base(new List<ISubProtocol<WebSocketSession>>(), socketFactory)
{

}
Expand Down Expand Up @@ -105,8 +108,9 @@ protected IBinaryDataConverter BinaryDataConverter
/// Initializes a new instance of the <see cref="WebSocketServer&lt;TWebSocketSession&gt;"/> class.
/// </summary>
/// <param name="subProtocols">The sub protocols.</param>
public WebSocketServer(IEnumerable<ISubProtocol<TWebSocketSession>> subProtocols)
: this()
/// <param name="socketFactory"></param>
public WebSocketServer(IEnumerable<ISubProtocol<TWebSocketSession>> subProtocols, ISocketFactory socketFactory = null)
: this(socketFactory)
{
if (!subProtocols.Any())
return;
Expand All @@ -124,17 +128,18 @@ public WebSocketServer(IEnumerable<ISubProtocol<TWebSocketSession>> subProtocols
/// Initializes a new instance of the <see cref="WebSocketServer&lt;TWebSocketSession&gt;"/> class.
/// </summary>
/// <param name="subProtocol">The sub protocol.</param>
public WebSocketServer(ISubProtocol<TWebSocketSession> subProtocol)
: this(new List<ISubProtocol<TWebSocketSession>> { subProtocol })
/// <param name="socketFactory"></param>
public WebSocketServer(ISubProtocol<TWebSocketSession> subProtocol, ISocketFactory socketFactory = null)
: this(new List<ISubProtocol<TWebSocketSession>> { subProtocol }, socketFactory)
{

}

/// <summary>
/// Initializes a new instance of the <see cref="WebSocketServer&lt;TWebSocketSession&gt;"/> class.
/// </summary>
public WebSocketServer()
: base(new WebSocketProtocol())
public WebSocketServer(ISocketFactory socketFactory = null)
: base(new WebSocketProtocol(), socketFactory)
{

}
Expand Down
16 changes: 10 additions & 6 deletions SocketBase/AppServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketBase.Protocol;
using SuperSocket.SocketBase.Security;
using SuperSocket.SocketBase.Sockets;

namespace SuperSocket.SocketBase
{
Expand All @@ -36,8 +37,9 @@ public AppServer()
/// Initializes a new instance of the <see cref="AppServer"/> class.
/// </summary>
/// <param name="receiveFilterFactory">The Receive filter factory.</param>
public AppServer(IReceiveFilterFactory<StringRequestInfo> receiveFilterFactory)
: base(receiveFilterFactory)
/// <param name="socketFactory">The socket factory.</param>
public AppServer(IReceiveFilterFactory<StringRequestInfo> receiveFilterFactory, ISocketFactory socketFactory = null)
: base(receiveFilterFactory, socketFactory)
{

}
Expand All @@ -63,8 +65,9 @@ public AppServer()
/// Initializes a new instance of the <see cref="AppServer&lt;TAppSession&gt;"/> class.
/// </summary>
/// <param name="receiveFilterFactory">The Receive filter factory.</param>
public AppServer(IReceiveFilterFactory<StringRequestInfo> receiveFilterFactory)
: base(receiveFilterFactory)
/// <param name="socketFactory">The socket factory.</param>
public AppServer(IReceiveFilterFactory<StringRequestInfo> receiveFilterFactory, ISocketFactory socketFactory)
: base(receiveFilterFactory, socketFactory)
{

}
Expand Down Expand Up @@ -98,8 +101,9 @@ public AppServer()
/// Initializes a new instance of the <see cref="AppServer&lt;TAppSession, TRequestInfo&gt;"/> class.
/// </summary>
/// <param name="protocol">The protocol.</param>
protected AppServer(IReceiveFilterFactory<TRequestInfo> protocol)
: base(protocol)
/// <param name="socketFactory">The socket factory.</param>
protected AppServer(IReceiveFilterFactory<TRequestInfo> protocol, ISocketFactory socketFactory = null)
: base(protocol, socketFactory)
{

}
Expand Down
13 changes: 12 additions & 1 deletion SocketBase/AppServerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using SuperSocket.SocketBase.Protocol;
using SuperSocket.SocketBase.Provider;
using SuperSocket.SocketBase.Security;
using SuperSocket.SocketBase.Sockets;

namespace SuperSocket.SocketBase
{
Expand Down Expand Up @@ -82,6 +83,14 @@ object IAppServer.ReceiveFilterFactory
get { return this.ReceiveFilterFactory; }
}

/// <summary>
/// Gets or sets the socket factory.
/// </summary>
/// <value>
/// The socket factory.
/// </value>
public virtual ISocketFactory SocketFactory { get; protected set; }

private List<ICommandLoader<ICommand<TAppSession, TRequestInfo>>> m_CommandLoaders = new List<ICommandLoader<ICommand<TAppSession, TRequestInfo>>>();

private Dictionary<string, CommandInfo<ICommand<TAppSession, TRequestInfo>>> m_CommandContainer;
Expand Down Expand Up @@ -175,9 +184,11 @@ public AppServerBase()
/// Initializes a new instance of the <see cref="AppServerBase&lt;TAppSession, TRequestInfo&gt;"/> class.
/// </summary>
/// <param name="receiveFilterFactory">The Receive filter factory.</param>
public AppServerBase(IReceiveFilterFactory<TRequestInfo> receiveFilterFactory)
/// <param name="socketFactory"></param>
public AppServerBase(IReceiveFilterFactory<TRequestInfo> receiveFilterFactory, ISocketFactory socketFactory = null)
{
this.ReceiveFilterFactory = receiveFilterFactory;
this.SocketFactory = socketFactory ?? new PassthroughSocketFactory();
}

/// <summary>
Expand Down
6 changes: 6 additions & 0 deletions SocketBase/IAppServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using SuperSocket.SocketBase.Logging;
using SuperSocket.SocketBase.Protocol;
using SuperSocket.SocketBase.Provider;
using SuperSocket.SocketBase.Sockets;

namespace SuperSocket.SocketBase
{
Expand Down Expand Up @@ -42,6 +43,11 @@ public interface IAppServer : IWorkItem, ILoggerProvider
/// </summary>
object ReceiveFilterFactory { get; }

/// <summary>
/// Gets the socket factory.
/// </summary>
ISocketFactory SocketFactory { get; }

/// <summary>
/// Gets the certificate of current server.
/// </summary>
Expand Down
5 changes: 2 additions & 3 deletions SocketBase/ISocketSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
using System.IO;
using System.Net;
using System.Security.Authentication;
using System.Net.Sockets;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Sockets;

namespace SuperSocket.SocketBase
{
Expand Down Expand Up @@ -104,7 +104,7 @@ public interface ISocketSession : ISessionBase
/// <summary>
/// Gets the client socket.
/// </summary>
Socket Client { get; }
ISocket Client { get; }

/// <summary>
/// Gets the local listening endpoint.
Expand All @@ -129,7 +129,6 @@ public interface ISocketSession : ISessionBase
/// </summary>
IAppSession AppSession { get; }


/// <summary>
/// Gets the original receive buffer offset.
/// </summary>
Expand Down
Loading