public ServerAgent(Uri uri, Gateway gateway, int maxConnections = 100) { Uri = uri; UriKey = uri.ToString(); ServerName = uri.ToString(); ServerID = GetServerID(); Host = uri.Host; Port = uri.Port; Available = false; MaxConnections = maxConnections; Gateway = gateway; requestAgentsPool = new ConcurrentQueue <TcpClientAgent>(); mWaitQueue = new ConcurrentQueue <TaskCompletionSource <TcpClientAgent> >(); for (int i = 0; i < 10; i++) { var item = new TcpClientAgent(Host, Port); requestAgentsPool.Enqueue(item); Agents.Add(item); } mConnections = 10; mCount = 10; Statistics.Server = ServerName; this.Available = false; mQueueWaitMaxLength = gateway.AgentRequestQueueSize; string protocol = Uri.Scheme.ToLower(); WebSocket = (protocol == "ws" || protocol == "wss"); mWSClient = GetWSClient(); AddressHeader = Encoding.ASCII.GetBytes("Logic-Server: " + ServerName + "\r\n"); }
internal Task <TcpClientAgent> PopClient() { TcpClientAgent tcpClientAgent; TaskCompletionSource <TcpClientAgent> result = new TaskCompletionSource <TcpClientAgent>(); if (requestAgentsPool.TryDequeue(out tcpClientAgent)) { result.SetResult(tcpClientAgent); } else { if (mConnections > MaxConnections) { if (mWaitLength < mQueueWaitMaxLength) { System.Threading.Interlocked.Increment(ref mWaitLength); mWaitQueue.Enqueue(result); } else { result.SetResult(null); } } else { System.Threading.Interlocked.Increment(ref mConnections); tcpClientAgent = new TcpClientAgent(Host, Port); lock (Agents) Agents.Add(tcpClientAgent); result.SetResult(tcpClientAgent); } } return(result.Task); }
public RequestAgent(TcpClientAgent clientAgent, ServerAgent serverAgent, HttpRequest request, HttpResponse response, UrlRouteServerGroup.UrlServerInfo urlServerInfo, Routes.UrlRoute urlRoute) { mTransferEncoding = false; mRequestLength = 0; Code = 0; Server = serverAgent; Request = request; Response = response; mClientAgent = clientAgent; mClientAgent.Client.ClientError = OnSocketError; mClientAgent.Client.DataReceive = OnReveive; mBuffer = mClientAgent.Buffer; Status = RequestStatus.None; UrlServerInfo = urlServerInfo; UrlRoute = urlRoute; mStartTime = TimeWatch.GetElapsedMilliseconds(); mRequestID = request.ID; //System.Threading.Interlocked.Increment(ref RequestCount); //mHistoryRequests[mRequestID] = this; if (UrlRoute.TimeOut > 0) { TimerOutValue = mStartTime + UrlRoute.TimeOut; UrlRoute.Gateway.TimeoutFactory.Add(this); } mProxyStream = new PipeStream(UrlRoute.Gateway.ProxyBufferPool); }
public ServerAgent(Uri uri, Gateway gateway, int maxConnections = 100) { Uri = uri; UriKey = uri.ToString(); ServerName = uri.ToString(); ServerID = GetServerID(); Host = uri.Host; Port = uri.Port; Available = false; MaxConnections = maxConnections; Gateway = gateway; requestAgentsPool = new ConcurrentQueue <TcpClientAgent>(); mWaitQueue = new ConcurrentQueue <TaskCompletionSource <TcpClientAgent> >(); for (int i = 0; i < 10; i++) { var item = new TcpClientAgent(Host, Port); requestAgentsPool.Enqueue(item); Agents.Add(item); } mConnections = 10; mCount = 10; Statistics.Server = ServerName; this.Available = false; mQueueWaitMaxLength = gateway.AgentRequestQueueSize; }
internal void Push(TcpClientAgent agent) { if (mDisposed) { agent.Client.DisConnect(); } else { requestAgentsPool.Push(agent); System.Threading.Interlocked.Increment(ref mCount); } }
public RequestAgent(TcpClientAgent clientAgent, ServerAgent serverAgent, HttpRequest request, HttpResponse response) { mTransferEncoding = false; mRequestLength = 0; Code = 0; Server = serverAgent; Request = request; Response = response; mClientAgent = clientAgent; mClientAgent.Client.ClientError = OnSocketError; mClientAgent.Client.DataReceive = OnReveive; mBuffer = mClientAgent.Buffer; Status = RequestStatus.None; }
internal TcpClientAgent PopClient() { if (!requestAgentsPool.TryPop(out TcpClientAgent result)) { var count = System.Threading.Interlocked.Increment(ref mConnections); if (count > MaxConnections) { System.Threading.Interlocked.Decrement(ref mConnections); return(null); } result = new TcpClientAgent(Host, Port); } else { System.Threading.Interlocked.Decrement(ref mCount); } return(result); }
public RequestAgent(TcpClientAgent clientAgent, ServerAgent serverAgent, HttpRequest request, HttpResponse response, UrlRouteServerGroup.UrlServerInfo urlServerInfo, Routes.UrlRoute urlRoute) { mTransferEncoding = false; mRequestLength = 0; Code = 0; Server = serverAgent; Request = request; Response = response; mClientAgent = clientAgent; mClientAgent.Client.ClientError = OnSocketError; mClientAgent.Client.DataReceive = OnReveive; mBuffer = mClientAgent.Buffer; Status = RequestStatus.None; UrlServerInfo = urlServerInfo; UrlRoute = urlRoute; mStartTime = TimeWatch.GetElapsedMilliseconds(); mRequestID = System.Threading.Interlocked.Increment(ref mRequestIDSqueue); //System.Threading.Interlocked.Increment(ref RequestCount); //mHistoryRequests[mRequestID] = this; }
internal void Push(TcpClientAgent agent) { if (mDisposed) { agent.Client.DisConnect(); } else { if (mWaitQueue.TryDequeue(out TaskCompletionSource <TcpClientAgent> result)) { System.Threading.Interlocked.Decrement(ref mWaitLength); //result.SetResult(agent); Task.Run(() => result.SetResult(agent)); } else { agent.Status = TcpClientAgentStatus.Free; requestAgentsPool.Enqueue(agent); } } }
public void Execute(HttpRequest request, HttpResponse response) { TcpClientAgent clientAgent = PopClient(); if (clientAgent == null) { string error = $"Unable to reach {Host}:{Port} HTTP request, exceeding maximum number of connections"; if (Gateway.HttpServer.EnableLog(LogType.Error)) { Gateway.HttpServer.Log(LogType.Error, error); } BadGateway result = new BadGateway(error); Events.EventResponseErrorArgs erea = new Events.EventResponseErrorArgs(request, response, result, BadGateway.SERVER_MAX_OF_CONNECTIONS); Gateway.OnResponseError(erea); } else { RequestAgent agent = new RequestAgent(clientAgent, this, request, response); agent.Completed = OnCompleted; agent.Execute(); } }
internal void Push(TcpClientAgent agent) { lock (mLockPool) { if (mDisposed) { agent.Client.DisConnect(); } else { if (mClientWaitQueue.Count > 0) { var item = mClientWaitQueue.Dequeue(); item.SetResult(agent); //Task.Run(() => item.SetResult(agent)); } else { requestAgentsPool.Enqueue(agent); } } } }
internal Task <TcpClientAgent> PopClient() { TcpClientAgent tcpClientAgent; lock (mLockPool) { TaskCompletionSource <TcpClientAgent> result = new TaskCompletionSource <TcpClientAgent>(); if (requestAgentsPool.Count > 0) { tcpClientAgent = requestAgentsPool.Dequeue(); result.SetResult(tcpClientAgent); } else { mConnections++; if (mConnections > MaxConnections) { mConnections--; if (mClientWaitQueue.Count < mQueueWaitMaxLength) { mClientWaitQueue.Enqueue(result); } else { result.SetResult(null); } } else { tcpClientAgent = new TcpClientAgent(Host, Port); result.SetResult(tcpClientAgent); } } return(result.Task); } }
public RequestAgent( TcpClientAgent clientAgent, ServerAgent serverAgent, HttpRequest request, HttpResponse response ) { mTransferEncoding = false; mRequestLength = 0; Code = 0; Server = serverAgent; Request = request; Response = response; mClientAgent = clientAgent; mClientAgent.Client.ClientError = OnSocketError; mClientAgent.Client.DataReceive = OnReveive; var _pStream = mClientAgent.Client.Stream as PipeStream; if (null != _pStream) { bool _init = true; var _act = _pStream.FlashCompleted; _pStream.FlashCompleted = buf => { if (_init) { _init = false; var _f = typeof(AsyncTcpClient).GetField( "mSendEventArgs", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance ); if (_f != null) { var _fVal = _f.GetValue(mClientAgent.Client) as BeetleX.Buffers.SocketAsyncEventArgsX; if (null != _fVal) { _fVal.Completed += SendEventArgs_Completed; } } _f = typeof(AsyncTcpClient).GetField( "mReceiveEventArgs", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance ); if (_f != null) { var _fVal = _f.GetValue(mClientAgent.Client) as BeetleX.Buffers.SocketAsyncEventArgsX; if (null != _fVal) { _fVal.Completed += ReceiveEventArgs_Completed; } } } request.Server.Log( BeetleX.EventArgs.LogType.Info, $"Begin FlashCompleted" ); _act(buf); request.Server.Log( BeetleX.EventArgs.LogType.Info, $"End FlashCompleted" ); }; } mBuffer = mClientAgent.Buffer; Status = RequestStatus.None; }