private int newSocketQueue(Socket socket) { IPEndPoint ipEndPoint = new unionType { Value = socket.RemoteEndPoint }.IPEndPoint; if (ipEndPoint.AddressFamily == AddressFamily.InterNetworkV6) { ipv6Hash ipv6 = ipEndPoint.Address; if (ipv6.Ip != null && socketQueues[getQueueIndex((uint)ipv6.HashCode)].NewSocket(socket, ref ipv6)) { return(1); } } else { #pragma warning disable 618 int ipv4 = (int)(uint)(ulong)ipEndPoint.Address.Address; #pragma warning restore 618 if (socketQueues[getQueueIndex((uint)ipv4)].NewSocket(socket, ipv4)) { return(1); } } fastCSharp.threading.disposeTimer.Default.addSocketClose(socket); return(0); }
private void newSocket(Socket socket, ref ipv6Hash ipv6) { if (Certificate == null) { (typePool <socket> .Pop() ?? http.socket.NewSocket()).Start(this, socket, ref ipv6); } else { (typePool <sslStream> .Pop() ?? sslStream.NewStream()).Start(this, socket, ref ipv6); } }
/// <summary> /// 客户端请求处理 /// </summary> /// <param name="socket"></param> /// <param name="ipv6"></param> /// <returns></returns> public bool NewSocket(Socket socket, ref ipv6Hash ipv6) { int index; Monitor.Enter(nodeLock); if (ipv6Queue != null && ipv6Queue.TryGetValue(ipv6, out index)) { if (nodes[index].NewActiveCount(maxActiveSocketCount)) { Monitor.Exit(nodeLock); server.newSocket(socket, ref ipv6); return(true); } if (nodes[index].Count < maxSocketCount) { try { nodes[index].NewQueue(socket, ref this); } finally { Monitor.Exit(nodeLock); } if (server.Certificate == null) { ++socketBase.queueCount; } else { ++socketBase.sslQueueCount; } return(true); } Monitor.Exit(nodeLock); } else { try { if (ipv6Queue == null) { ipv6Queue = dictionary.Create <ipv6Hash, int>(); } index = getSocketQueueIndex(); nodes[index].Set(socket); ipv6Queue.Add(ipv6, index); } finally { Monitor.Exit(nodeLock); } server.newSocket(socket, ref ipv6); return(true); } return(false); }
/// <summary> /// 开始处理新的请求 /// </summary> /// <param name="server">HTTP服务</param> /// <param name="socket">套接字</param> /// <param name="ip"></param> internal void Start(server server, Socket socket, ref ipv6Hash ip) { Ipv6 = ip; Ipv4 = 0; this.server = server; servers = server.Servers; Socket = socket; try { SslStream = new SslStream(networkStream = new NetworkStream(socket, true), false); SslStream.BeginAuthenticateAsServer(server.Certificate, false, server.Protocol, false, authenticateCallback, this); return; } catch (Exception error) { log.Default.Add(error, null, false); } headerError(); }
/// <summary> /// 请求处理结束 /// </summary> /// <param name="ipv6"></param> /// <returns></returns> public Socket Next(ref ipv6Hash ipv6) { int index; Monitor.Enter(nodeLock); if (ipv6Queue != null && ipv6Queue.TryGetValue(ipv6, out index)) { Socket socket; int freeIndex = nodes[index].Free(nodes); switch (freeIndex + 2) { case -2 + 2: Monitor.Exit(nodeLock); return(null); case -1 + 2: socket = nodes[index].FreeSocket(); Monitor.Exit(nodeLock); return(socket); case 0 + 2: try { ipv6Queue.Remove(ipv6); nodeIndexs.Add(index); } finally { Monitor.Exit(nodeLock); } return(null); default: socket = nodes[freeIndex].FreeSocket(); try { nodeIndexs.Add(freeIndex); } finally { Monitor.Exit(nodeLock); } return(socket); } } Monitor.Exit(nodeLock); return(null); }
internal Socket SocketEnd(ref ipv6Hash ipv6) { Interlocked.Increment(ref freeClientCount); return(socketQueues == null ? null : socketQueues[getQueueIndex((uint)ipv6.HashCode)].Next(ref ipv6)); }
/// <summary> /// 请求处理结束 /// </summary> /// <param name="ipv6">客户端IP</param> internal static new void SocketEnd(ipv6Hash ipv6) { client socket = clientQueue.End(ipv6); if (socket.Server != null) sslStream.Start(socket.Server, socket.Socket, ipv6, socket.Certificate); }
/// <summary> /// 请求处理结束 /// </summary> /// <param name="ipv6">客户端IP</param> internal static void SocketEnd(ipv6Hash ipv6) { client socket = clientQueue.End(ipv6); if (socket.Server != null) http.socket.Start(socket.Server, socket.Socket, ipv6); }