Skip to content

Commit

Permalink
Merge pull request #76 from Eastrall/refactoring
Browse files Browse the repository at this point in the history
CreateSocketAsync refactoring
  • Loading branch information
Filipe GP authored Feb 14, 2018
2 parents 6468ae5 + ca28ec9 commit 3ff5140
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/Ether.Network/Client/NetClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ protected NetClient(string host, int port, int bufferSize)
{
this.Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this._ipEndPoint = NetUtils.CreateIpEndPoint(host, port);
this._socketSendArgs = NetUtils.CreateSocketAsync(this.Socket, -1, this.IO_Completed);
this._socketReceiveArgs = NetUtils.CreateSocketAsync(this, bufferSize, this.IO_Completed);
this._socketSendArgs = NetUtils.CreateSocketAsync(this.Socket, this.IO_Completed);
this._socketReceiveArgs = NetUtils.CreateSocketAsync(this, this.IO_Completed, bufferSize);
this._autoConnectEvent = new AutoResetEvent(false);
this._autoSendEvent = new AutoResetEvent(false);
this._sendingQueue = new BlockingCollection<byte[]>();
Expand All @@ -71,7 +71,7 @@ public void Connect()
if (this.IsConnected)
throw new InvalidOperationException("Client is already connected to remote.");

var connectSocket = NetUtils.CreateSocketAsync(this.Socket, -1, this.IO_Completed);
var connectSocket = NetUtils.CreateSocketAsync(this.Socket, this.IO_Completed);
connectSocket.RemoteEndPoint = this._ipEndPoint;

if (this.Socket.ConnectAsync(connectSocket))
Expand Down
7 changes: 3 additions & 4 deletions src/Ether.Network/Server/NetServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ public void Start()

for (var i = 0; i < this.Configuration.MaximumNumberOfConnections; i++)
{
this._readPool.Push(NetUtils.CreateSocketAsync(null, this.Configuration.BufferSize, this.IO_Completed));
this._writePool.Push(NetUtils.CreateSocketAsync(null, this.Configuration.BufferSize,
this.IO_Completed));
this._readPool.Push(NetUtils.CreateSocketAsync(null, this.IO_Completed, this.Configuration.BufferSize));
this._writePool.Push(NetUtils.CreateSocketAsync(null, this.IO_Completed, this.Configuration.BufferSize));
}

this.Initialize();
Expand All @@ -94,7 +93,7 @@ public void Start()
this.Socket.Bind(new IPEndPoint(address, this.Configuration.Port));
this.Socket.Listen(this.Configuration.Backlog);
this.IsRunning = true;
this.StartAccept(NetUtils.CreateSocketAsync(null, -1, this.IO_Completed));
this.StartAccept(NetUtils.CreateSocketAsync(null, this.IO_Completed));

if (this.Configuration.Blocking)
this._manualResetEvent.WaitOne();
Expand Down
17 changes: 15 additions & 2 deletions src/Ether.Network/Utils/NetUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ public static byte[] GetPacketBuffer(byte[] bufferSource, int offset, int size)
/// Creates a new <see cref="SocketAsyncEventArgs"/> instance.
/// </summary>
/// <param name="userToken">User token</param>
/// <param name="bufferSize">Buffer size</param>
/// <param name="completedAction">Completed operation action</param>
/// <returns></returns>
public static SocketAsyncEventArgs CreateSocketAsync(object userToken, int bufferSize, EventHandler<SocketAsyncEventArgs> completedAction)
public static SocketAsyncEventArgs CreateSocketAsync(object userToken, EventHandler<SocketAsyncEventArgs> completedAction)
{
var socketAsync = new SocketAsyncEventArgs
{
Expand All @@ -73,6 +72,20 @@ public static SocketAsyncEventArgs CreateSocketAsync(object userToken, int buffe

socketAsync.Completed += completedAction;

return socketAsync;
}

/// <summary>
/// Creates a new <see cref="SocketAsyncEventArgs"/> instance.
/// </summary>
/// <param name="userToken">User token</param>
/// <param name="completedAction">Completed operation action</param>
/// <param name="bufferSize">Buffer size</param>
/// <returns></returns>
public static SocketAsyncEventArgs CreateSocketAsync(object userToken, EventHandler<SocketAsyncEventArgs> completedAction, int bufferSize)
{
SocketAsyncEventArgs socketAsync = CreateSocketAsync(userToken, completedAction);

if (bufferSize > 0)
socketAsync.SetBuffer(new byte[bufferSize], 0, bufferSize);

Expand Down

0 comments on commit 3ff5140

Please sign in to comment.