/// <summary> /// On web context. /// </summary> /// <param name="sender">The server that sent the context.</param> /// <param name="context">The current connection context.</param> private void Server_OnWebContext(object sender, WebContext context) { Nequeo.Net.Data.ConnectionContext conn = null; lock (_lockObject) { // As data comes from the client send it to the load balance server // this client is connected to. conn = _clients[context.ConnectionID]; } // If the client has been found. if (conn != null) { Nequeo.Net.NetClient client = conn.Client; // Read all the bytes in the buffer. int count = (int)context.WebRequest.Input.Length; byte[] buffer = new byte[count]; context.WebRequest.Read(buffer, 0, count); if (buffer != null) { // Write all the bytes to the balance server through the client. client.Send(buffer); } buffer = null; } }
/// <summary> /// On client diconnected. /// </summary> /// <param name="context">The client context.</param> private async void ClientDisconnectedEx(Nequeo.Net.Provider.ISingleContextBase context) { await Nequeo.Threading.AsyncOperationResult <bool> . RunTask(() => { lock (_lockObject) { try { // Get the client. Nequeo.Net.Data.ConnectionContext conn = _clients[context.ConnectionID]; // Close the connection. Nequeo.Net.NetClient client = conn.Client; client.Dispose(); // Remove the client. _clients.Remove(context.ConnectionID); // Decrement the server count. Find the current load balance server // this client is connected to. IEnumerable <Data.contextServer> balanceServers = _loadServers.servers.Where(u => u.name.ToLower() == conn.LoadBalanceServer.ToLower()); // If the server has been found. if (balanceServers.Count() > 0) { // Get the first server. Data.contextServer balanceServer = balanceServers.First(); // Get the current client count. int currentCount = balanceServer.count; // If the current count is not zero // then decrement to count else do // not go less than zero. if (currentCount > 0) { balanceServer.count = currentCount - 1; } } } catch { } } }); }