Commit 6f64f91a authored by Honfika's avatar Honfika

Connection count changed events

parent 12206ab2
...@@ -13,8 +13,12 @@ ...@@ -13,8 +13,12 @@
<ColumnDefinition Width="3" /> <ColumnDefinition Width="3" />
<ColumnDefinition /> <ColumnDefinition />
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<GridSplitter Grid.Column="1" HorizontalAlignment="Stretch" /> <Grid.RowDefinitions>
<ListView Grid.Column="0" HorizontalAlignment="Stretch" ItemsSource="{Binding Sessions}" SelectedItem="{Binding SelectedSession}" <RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<GridSplitter Grid.Column="1" Grid.Row="0" HorizontalAlignment="Stretch" />
<ListView Grid.Column="0" Grid.Row="0" HorizontalAlignment="Stretch" ItemsSource="{Binding Sessions}" SelectedItem="{Binding SelectedSession}"
KeyDown="ListViewSessions_OnKeyDown"> KeyDown="ListViewSessions_OnKeyDown">
<ListView.View> <ListView.View>
<GridView> <GridView>
...@@ -29,7 +33,7 @@ ...@@ -29,7 +33,7 @@
</GridView> </GridView>
</ListView.View> </ListView.View>
</ListView> </ListView>
<TabControl Grid.Column="2"> <TabControl Grid.Column="2" Grid.Row="0">
<TabItem Header="Session"> <TabItem Header="Session">
<Grid Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <Grid Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.RowDefinitions> <Grid.RowDefinitions>
...@@ -41,5 +45,11 @@ ...@@ -41,5 +45,11 @@
</Grid> </Grid>
</TabItem> </TabItem>
</TabControl> </TabControl>
<StackPanel Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="3" Orientation="Horizontal">
<TextBlock Text="ClientConnectionCount:" />
<TextBlock Text="{Binding ClientConnectionCount}" Margin="10,0,20,0" />
<TextBlock Text="ServerConnectionCount:" />
<TextBlock Text="{Binding ServerConnectionCount}" Margin="10,0,20,0" />
</StackPanel>
</Grid> </Grid>
</Window> </Window>
...@@ -44,6 +44,24 @@ namespace Titanium.Web.Proxy.Examples.Wpf ...@@ -44,6 +44,24 @@ namespace Titanium.Web.Proxy.Examples.Wpf
} }
} }
public static readonly DependencyProperty ClientConnectionCountProperty = DependencyProperty.Register(
nameof(ClientConnectionCount), typeof(int), typeof(MainWindow), new PropertyMetadata(default(int)));
public int ClientConnectionCount
{
get { return (int)GetValue(ClientConnectionCountProperty); }
set { SetValue(ClientConnectionCountProperty, value); }
}
public static readonly DependencyProperty ServerConnectionCountProperty = DependencyProperty.Register(
nameof(ServerConnectionCount), typeof(int), typeof(MainWindow), new PropertyMetadata(default(int)));
public int ServerConnectionCount
{
get { return (int)GetValue(ServerConnectionCountProperty); }
set { SetValue(ServerConnectionCountProperty, value); }
}
private readonly Dictionary<SessionEventArgs, SessionListItem> sessionDictionary = new Dictionary<SessionEventArgs, SessionListItem>(); private readonly Dictionary<SessionEventArgs, SessionListItem> sessionDictionary = new Dictionary<SessionEventArgs, SessionListItem>();
private SessionListItem selectedSession; private SessionListItem selectedSession;
...@@ -59,6 +77,8 @@ namespace Titanium.Web.Proxy.Examples.Wpf ...@@ -59,6 +77,8 @@ namespace Titanium.Web.Proxy.Examples.Wpf
proxyServer.BeforeResponse += ProxyServer_BeforeResponse; proxyServer.BeforeResponse += ProxyServer_BeforeResponse;
proxyServer.TunnelConnectRequest += ProxyServer_TunnelConnectRequest; proxyServer.TunnelConnectRequest += ProxyServer_TunnelConnectRequest;
proxyServer.TunnelConnectResponse += ProxyServer_TunnelConnectResponse; proxyServer.TunnelConnectResponse += ProxyServer_TunnelConnectResponse;
proxyServer.ClientConnectionCountChanged += delegate { Dispatcher.Invoke(() => { ClientConnectionCount = proxyServer.ClientConnectionCount; }); };
proxyServer.ServerConnectionCountChanged += delegate { Dispatcher.Invoke(() => { ServerConnectionCount = proxyServer.ServerConnectionCount; }); };
proxyServer.Start(); proxyServer.Start();
proxyServer.SetAsSystemProxy(explicitEndPoint, ProxyProtocolType.AllHttp); proxyServer.SetAsSystemProxy(explicitEndPoint, ProxyProtocolType.AllHttp);
......
...@@ -124,7 +124,7 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -124,7 +124,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
throw; throw;
} }
Interlocked.Increment(ref server.serverConnectionCount); server.UpdateServerConnectionCount(true);
return new TcpConnection return new TcpConnection
{ {
......
...@@ -67,7 +67,7 @@ namespace Titanium.Web.Proxy ...@@ -67,7 +67,7 @@ namespace Titanium.Web.Proxy
/// <summary> /// <summary>
/// Backing field for corresponding public property /// Backing field for corresponding public property
/// </summary> /// </summary>
internal int serverConnectionCount; private int serverConnectionCount;
/// <summary> /// <summary>
/// A object that creates tcp connection to server /// A object that creates tcp connection to server
...@@ -199,6 +199,16 @@ namespace Titanium.Web.Proxy ...@@ -199,6 +199,16 @@ namespace Titanium.Web.Proxy
/// </summary> /// </summary>
public event Func<object, TunnelConnectSessionEventArgs, Task> TunnelConnectResponse; public event Func<object, TunnelConnectSessionEventArgs, Task> TunnelConnectResponse;
/// <summary>
/// Occurs when client connection count changed.
/// </summary>
public event EventHandler ClientConnectionCountChanged;
/// <summary>
/// Occurs when server connection count changed.
/// </summary>
public event EventHandler ServerConnectionCountChanged;
/// <summary> /// <summary>
/// External proxy for Http /// External proxy for Http
/// </summary> /// </summary>
...@@ -752,7 +762,7 @@ namespace Titanium.Web.Proxy ...@@ -752,7 +762,7 @@ namespace Titanium.Web.Proxy
private async Task HandleClient(TcpClient tcpClient, ProxyEndPoint endPoint) private async Task HandleClient(TcpClient tcpClient, ProxyEndPoint endPoint)
{ {
Interlocked.Increment(ref clientConnectionCount); UpdateClientConnectionCount(true);
tcpClient.ReceiveTimeout = ConnectionTimeOutSeconds * 1000; tcpClient.ReceiveTimeout = ConnectionTimeOutSeconds * 1000;
tcpClient.SendTimeout = ConnectionTimeOutSeconds * 1000; tcpClient.SendTimeout = ConnectionTimeOutSeconds * 1000;
...@@ -770,7 +780,7 @@ namespace Titanium.Web.Proxy ...@@ -770,7 +780,7 @@ namespace Titanium.Web.Proxy
} }
finally finally
{ {
Interlocked.Decrement(ref clientConnectionCount); UpdateClientConnectionCount(false);
try try
{ {
...@@ -799,5 +809,33 @@ namespace Titanium.Web.Proxy ...@@ -799,5 +809,33 @@ namespace Titanium.Web.Proxy
endPoint.Listener.Stop(); endPoint.Listener.Stop();
endPoint.Listener.Server.Dispose(); endPoint.Listener.Server.Dispose();
} }
internal void UpdateClientConnectionCount(bool increment)
{
if (increment)
{
Interlocked.Increment(ref clientConnectionCount);
}
else
{
Interlocked.Decrement(ref clientConnectionCount);
}
ClientConnectionCountChanged?.Invoke(this, EventArgs.Empty);
}
internal void UpdateServerConnectionCount(bool increment)
{
if (increment)
{
Interlocked.Increment(ref serverConnectionCount);
}
else
{
Interlocked.Decrement(ref serverConnectionCount);
}
ServerConnectionCountChanged?.Invoke(this, EventArgs.Empty);
}
} }
} }
...@@ -168,7 +168,7 @@ namespace Titanium.Web.Proxy ...@@ -168,7 +168,7 @@ namespace Titanium.Web.Proxy
{ {
await TcpHelper.SendRaw(clientStream, connection, await TcpHelper.SendRaw(clientStream, connection,
(buffer, offset, count) => { connectArgs.OnDataSent(buffer, offset, count); }, (buffer, offset, count) => { connectArgs.OnDataReceived(buffer, offset, count); }); (buffer, offset, count) => { connectArgs.OnDataSent(buffer, offset, count); }, (buffer, offset, count) => { connectArgs.OnDataReceived(buffer, offset, count); });
Interlocked.Decrement(ref serverConnectionCount); UpdateServerConnectionCount(false);
} }
return; return;
...@@ -344,7 +344,7 @@ namespace Titanium.Web.Proxy ...@@ -344,7 +344,7 @@ namespace Titanium.Web.Proxy
else if (!connection.HostName.Equals(args.WebSession.Request.RequestUri.Host, StringComparison.OrdinalIgnoreCase)) else if (!connection.HostName.Equals(args.WebSession.Request.RequestUri.Host, StringComparison.OrdinalIgnoreCase))
{ {
connection.Dispose(); connection.Dispose();
Interlocked.Decrement(ref serverConnectionCount); UpdateServerConnectionCount(false);
connection = await GetServerConnection(args); connection = await GetServerConnection(args);
} }
......
...@@ -227,7 +227,7 @@ namespace Titanium.Web.Proxy ...@@ -227,7 +227,7 @@ namespace Titanium.Web.Proxy
if (serverConnection != null) if (serverConnection != null)
{ {
serverConnection.Dispose(); serverConnection.Dispose();
Interlocked.Decrement(ref serverConnectionCount); UpdateServerConnectionCount(false);
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment