diff --git a/src/Ether.Network/Client/NetClient.cs b/src/Ether.Network/Client/NetClient.cs index de59404..98212b5 100644 --- a/src/Ether.Network/Client/NetClient.cs +++ b/src/Ether.Network/Client/NetClient.cs @@ -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(); @@ -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)) diff --git a/src/Ether.Network/Server/NetServer.cs b/src/Ether.Network/Server/NetServer.cs index 883b059..25d72a4 100644 --- a/src/Ether.Network/Server/NetServer.cs +++ b/src/Ether.Network/Server/NetServer.cs @@ -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(); @@ -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(); diff --git a/src/Ether.Network/Utils/NetUtils.cs b/src/Ether.Network/Utils/NetUtils.cs index c87ed3f..bd0ac53 100644 --- a/src/Ether.Network/Utils/NetUtils.cs +++ b/src/Ether.Network/Utils/NetUtils.cs @@ -61,10 +61,9 @@ public static byte[] GetPacketBuffer(byte[] bufferSource, int offset, int size) /// Creates a new instance. /// /// User token - /// Buffer size /// Completed operation action /// - public static SocketAsyncEventArgs CreateSocketAsync(object userToken, int bufferSize, EventHandler completedAction) + public static SocketAsyncEventArgs CreateSocketAsync(object userToken, EventHandler completedAction) { var socketAsync = new SocketAsyncEventArgs { @@ -73,6 +72,20 @@ public static SocketAsyncEventArgs CreateSocketAsync(object userToken, int buffe socketAsync.Completed += completedAction; + return socketAsync; + } + + /// + /// Creates a new instance. + /// + /// User token + /// Completed operation action + /// Buffer size + /// + public static SocketAsyncEventArgs CreateSocketAsync(object userToken, EventHandler completedAction, int bufferSize) + { + SocketAsyncEventArgs socketAsync = CreateSocketAsync(userToken, completedAction); + if (bufferSize > 0) socketAsync.SetBuffer(new byte[bufferSize], 0, bufferSize);