internal ClientSocket GetClientSocket() { if (!this.IsAlive) { LocalLoggingService.Warning("{0} {1} {2} {3}", DistributedServerConfiguration.ModuleName, "ClientNode", "GetClientSocket", string.Format("节点 {0} 已经死亡", config.Name)); return(null); } locker.EnterUpgradeableReadLock(); ClientSocket socket = null; try { socket = idleSockets.FirstOrDefault(); if (socket != null) { socket.Acquire(); locker.EnterWriteLock(); try { idleSockets.Remove(socket); busySockets.Add(socket); } finally { locker.ExitWriteLock(); } } else { if (busySockets.Count < config.MaxConnections) { LocalLoggingService.Debug("{0} {1} {2} {3}", DistributedServerConfiguration.ModuleName, "ClientNode", "GetClientSocket", string.Format("节点 {0} 中没有空闲的socket,新创建一个socket并加入池", config.Name)); socket = CreateClientSocket(false, ReturnClientSocket, CloseClientSocket, CloseClientSocketAndNode); if (socket != null) { socket.Acquire(); locker.EnterWriteLock(); try { busySockets.Add(socket); } finally { locker.ExitWriteLock(); } } else { LocalLoggingService.Warning("{0} {1} {2} {3}", DistributedServerConfiguration.ModuleName, "ClientNode", "GetClientSocket", string.Format("节点 {0} 中GetClientSocket尝试创建一个新的socket失败", config.Name)); } } else { LocalLoggingService.Warning("{0} {1} {2} {3}", DistributedServerConfiguration.ModuleName, "ClientNode", "GetClientSocket", string.Format("节点 {0} 的socket池已满,无法获得socket ", config.Name)); } } } finally { locker.ExitUpgradeableReadLock(); } if (socket == null) { LocalLoggingService.Warning("{0} {1} {2} {3}", DistributedServerConfiguration.ModuleName, "ClientNode", "GetClientSocket", string.Format("节点 {0} 没有获取到socket ", config.Name)); } return(socket); }
private void ReturnClientSocket(ClientSocket socket) { socket.Release(); locker.EnterWriteLock(); try { busySockets.Remove(socket); if (idleSockets.Count + busySockets.Count < config.MaxConnections) { idleSockets.Add(socket); } else { LocalLoggingService.Warning("{0} {1} {2} {3}", DistributedServerConfiguration.ModuleName, "ClientNode", "ReturnClientSocket", string.Format("节点 {0} 的socket池已达到最大限制 {1},socket无须放回池", config.Name, config.MaxConnections)); socket.Destroy(); } } finally { locker.ExitWriteLock(); } }
private void CloseClientSocket(ClientSocket socket) { socket.Destroy(); LocalLoggingService.Warning("{0} {1} {2} {3}", DistributedServerConfiguration.ModuleName, "ClientNode", "CloseClientSocket", string.Format("关闭节点 {0} 中的一个socket", config.Name)); locker.EnterWriteLock(); try { busySockets.Remove(socket); } finally { locker.ExitWriteLock(); } }
private void CloseClientSocketAndNode(ClientSocket socket) { LocalLoggingService.Warning("{0} {1} {2} {3}", DistributedServerConfiguration.ModuleName, "ClientNode", "CloseClientSocketAndNode", string.Format("节点 {0} 已经无效", config.Name)); this.IsAlive = false; socket.Destroy(); nodeErrorCallbackAction(this); }
private ClientSocket CreateClientSocket(bool isDirectSocket, Action<ClientSocket> disposeAction, Action<ClientSocket> lowlevelErrorAction, Action<ClientSocket> highLevelErrorAction) { var ip = config.Address.Split(':')[0]; var port = Convert.ToInt16(config.Address.Split(':')[1]); var socket = new ClientSocket(disposeAction, lowlevelErrorAction, highLevelErrorAction, Convert.ToInt16(((TimeSpan)config.SendTimeout).TotalMilliseconds), Convert.ToInt16(((TimeSpan)config.ReceiveTimeout).TotalMilliseconds), ip, port); try { socket.Connect(Convert.ToInt16(((TimeSpan)config.ConnectTimeout).TotalMilliseconds)); return socket; } catch (Exception ex) { LocalLoggingService.Error("{0} {1} {2} {3}", DistributedServerConfiguration.ModuleName, "ClientNode", "Init", ex, string.Format("节点 {0} 连接socket出错", config.Name), ex.Message); if (!isDirectSocket) CloseClientSocketAndNode(socket); else socket.Destroy(); } return null; }