internal override void ReleaseAndDisposeAll() { lock (padLock) { foreach (LinkedManagedSocket socket in sockets) { try { if (socket.Connected) { socket.Shutdown(SocketShutdown.Both); } socket.Close(); Interlocked.Decrement(ref activeSocketCount); Interlocked.Decrement(ref socketCount); } catch (SocketException) { } catch (ObjectDisposedException) { } } nextSocket = null; } }
private void PullSocketFromRotation(LinkedManagedSocket socket) { lock (padLock) { if (nextSocket != null) { if (socket == nextSocket) { nextSocket = nextSocket.Next; spareSockets--; } else { LinkedManagedSocket pointer = nextSocket.Next; LinkedManagedSocket prevPointer = null; while (pointer != null && pointer != socket) { prevPointer = pointer; pointer = pointer.Next; } if (pointer == socket && prevPointer != null && pointer != null) { prevPointer.Next = pointer.Next; //skip over it! spareSockets--; } } } } }
private LinkedManagedSocket BuildSocket() { LinkedManagedSocket socket = new LinkedManagedSocket(this.Settings, this); socket.Connect(this.destination, this.Settings.ConnectTimeout); Interlocked.Increment(ref socketCount); return(socket); }
internal override void ReleaseSocket(ManagedSocket socket) { try { if (socket.LastError != SocketError.Success || SocketAgedOut(socket)) { //log.InfoFormat("releasing socket to {0} with status {1}", destination, socket.LastError); DisposeSocket(socket); } else { if (!socket.Idle) { lock (padLock) { socket.Idle = true; if (nextSocket == null) { nextSocket = (LinkedManagedSocket)socket; spareSockets++; } else { LinkedManagedSocket newNextSocket = (LinkedManagedSocket)socket; LinkedManagedSocket currentNextSocket = nextSocket; newNextSocket.Next = currentNextSocket; nextSocket = newNextSocket; spareSockets++; } ExitLimiter(); } Interlocked.Decrement(ref activeSocketCount); } } } catch (Exception ex) { if (log.IsErrorEnabled) { log.ErrorFormat("Exception releasing socket: {0}", ex); } } }
public void PoolFiller() { do { Thread.Sleep(10000); try { List <LinkedManagedSocket> disposeList = null; lock (padLock) { LinkedManagedSocket socketPointer = nextSocket; while (socketPointer != null) { if (SocketAgedOut(socketPointer)) { LinkedManagedSocket expiredSocket = socketPointer; socketPointer = socketPointer.Next; PullSocketFromRotation(expiredSocket); spareSockets--; if (disposeList == null) { disposeList = new List <LinkedManagedSocket>(); } disposeList.Add(expiredSocket); } else { socketPointer = socketPointer.Next; } } } if (disposeList != null) { disposeList.ForEach(expiredSocket => DisposeSocket(expiredSocket, false)); } } catch (Exception ex) { log.InfoFormat("poolfiller exception {0}", ex.Message); } } while (true); }
internal override ManagedSocket GetSocket() { if (!EnterLimiter()) { throw new SocketException((int)SocketError.TooManyOpenSockets); } List <LinkedManagedSocket> disposeList = null; try { lock (padLock) { while (nextSocket != null) { // async receive could set an error at any time. if (nextSocket.LastError == SocketError.Success) { var foundSocket = nextSocket; // here we the first non-errored socket off of the linked list. foundSocket.Idle = false; nextSocket = foundSocket.Next; spareSockets--; foundSocket.Next = null; Interlocked.Increment(ref activeSocketCount); return(foundSocket); } else { // not null, has error LinkedManagedSocket badSocket = nextSocket; log.DebugFormat("Socket not used in state {0}", badSocket.LastError); nextSocket = nextSocket.Next; // look at the next one spareSockets--; badSocket.Next = null; if (disposeList == null) { disposeList = new List <LinkedManagedSocket>(); } disposeList.Add(badSocket); } } } if (disposeList != null) { try { disposeList.ForEach(badSocket => DisposeSocket(badSocket, false)); } catch (Exception ex) { log.InfoFormat("exception disposing sockets {0}", ex.Message); } disposeList = null; } // else, make a new one. which.. probably should not be able to happen. var socket = BuildSocket(); lock (setLock) { sockets.Add(socket); } Interlocked.Increment(ref activeSocketCount); return(socket); } catch { ExitLimiter(); throw; } finally { if (disposeList != null) { disposeList.ForEach(badSocket => DisposeSocket(badSocket, false)); } } }
internal override ManagedSocket GetSocket() { LinkedManagedSocket socket = null; if (EnterLimiter()) { try { lock (padLock) { LinkedManagedSocket foundSocket = null; if (nextSocket != null) { while (foundSocket == null) { if (nextSocket == null) //might be upon second iteration of this loop { foundSocket = BuildSocket(); lock (setLock) { sockets.Add(foundSocket); } Interlocked.Increment(ref activeSocketCount); break; //return socket; } if (nextSocket.LastError == SocketError.Success) //async receive could set an error at any time. { foundSocket = nextSocket; } else //not null, has error { LinkedManagedSocket badSocket = nextSocket; nextSocket = nextSocket.Next; //look at the next one badSocket.Next = null; DisposeSocket(badSocket, false); } } //here we would've grabbed the first non-errored socket off of the linked list. foundSocket.Idle = false; nextSocket = foundSocket.Next; foundSocket.Next = null; Interlocked.Increment(ref activeSocketCount); socket = foundSocket; //return socket; } } //next socket was null. we no longer need a lock if (socket != null) //found a good one on the list or built one in the loop { return(socket); } //else, make a new one. which.. probably should not be able to happen. socket = BuildSocket(); lock (setLock) { sockets.Add(socket); } Interlocked.Increment(ref activeSocketCount); return(socket); } catch { ExitLimiter(); throw; } } throw new SocketException((int)SocketError.TooManyOpenSockets); }