Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1239 → Rev 1240

/TCPproxy/trunk/MainForm.cs
49,6 → 49,7
private int resendPort;
private TcpShowMode tcpShowMode = TcpShowMode.ByDirection;
private bool autoExpand = true;
private bool clearing = false;
 
private string defaultCaption;
 
608,6 → 609,8
 
protected override void Dispose(bool disposing)
{
clearing = true;
 
if(tcpListener != null) {
tcpListener.StopListening(); // stop listening
tcpListener.CancelAll(); // cancel all open connections
1062,12 → 1065,9
 
private void ClearAll()
{
// close all connetions
foreach(object tcp in treeNodes.Keys)
if(tcp is TcpConnection)
((TcpConnection)tcp).Cancel();
clearing = true;
 
// FIXME wait for all log messages from network part
tcpListener.CancelAll();
 
treeNodes.Clear();
1077,6 → 1077,8
 
messagesBox.Clear();
logMessages.Clear();
 
clearing = false;
}
 
private void Start(int listenPort, IPAddress resendHost, int resendPort)
1255,6 → 1257,8
#region network events handlers
private void TcpConnectionLog(object sender, TcpLogEventArgs e)
{
if(clearing) return;
 
lock(this)
{
TcpConnection tcp = sender as TcpConnection;
/TCPproxy/trunk/Network.cs
173,7 → 173,7
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(new IPEndPoint(IPAddress.Any, listenPort));
socket.Listen(100);
ThreadDebugger.Add(socket.BeginAccept(new AsyncCallback(OnClientConnect), null), "StartListening");
socket.BeginAccept(new AsyncCallback(OnClientConnect), null);
SendLog(null, LogLevel.Important, "Listen on " + socket.LocalEndPoint);
}
 
198,17 → 198,24
 
public void CancelAll()
{
// make a copy of connections list because it will be changed when connections will delete themself
ArrayList tcpConnectionsCopy;
 
lock(tcpConnections)
{
tcpConnectionsCopy = new ArrayList(tcpConnections);
}
 
// send cancel
foreach(TcpConnection tcp in tcpConnectionsCopy)
{
tcp.Cancel();
}
 
// wait for aborted threads
foreach(TcpConnection tcp in tcpConnectionsCopy)
{
tcp.WaitCancel();
}
}
 
protected virtual void OnClientConnect(IAsyncResult asyn)
216,7 → 223,7
try
{
Socket worker = socket.EndAccept(asyn);
ThreadDebugger.Add(socket.BeginAccept(new AsyncCallback(OnClientConnect), null), "OnClientConnect"); // wait for next client
socket.BeginAccept(new AsyncCallback(OnClientConnect), null); // wait for next client
 
TcpConnection tcp = new TcpConnection(string.Format("{0:0000}", tcpId++));
tcp.Close += new TcpEventHandler(TcpConnectionClosed);
389,6 → 396,12
worker.Cancel();
}
 
public void WaitCancel()
{
if(worker != null)
worker.WaitCancel();
}
 
protected TcpMessage Append(TcpMessageDirection direction, byte[] newBytes)
{
return Append(direction, newBytes, newBytes.Length);
436,6 → 449,12
OnUpdate(new TcpEventArgs());
}
 
private void CheckClosed()
{
if(localState == SocketState.Closed && remoteState == SocketState.Closed)
OnClose(new TcpEventArgs());
}
 
protected void SetLocalState(SocketState localState)
{
if(this.localState == SocketState.None && localState == SocketState.Connecting)
493,6 → 512,7
this.localState = localState;
if(this.localState == SocketState.Closed) httpParser.NewMessageArived();
OnUpdate(new TcpEventArgs());
CheckClosed();
}
 
protected void SetRemoteState(SocketState remoteState)
550,6 → 570,7
this.remoteState = remoteState;
if(this.remoteState == SocketState.Closed) httpParser.NewMessageArived();
OnUpdate(new TcpEventArgs());
CheckClosed();
}
 
public event TcpEventHandler Update;
731,7 → 752,9
 
private TcpConnection tcp;
private Socket localSocket;
private bool localSocketOpen = false;
private Socket remoteSocket;
private bool remoteSocketOpen = false;
private byte[] localDataBuffer;
private byte[] remoteDataBuffer;
private AsyncCallback receiveLocalMethod;
758,10 → 781,11
((IPEndPoint)localSocket.RemoteEndPoint).Address,
((IPEndPoint)localSocket.RemoteEndPoint).Port));
 
this.localSocket = localSocket;
this.tcp = tcp;
receiveLocalMethod = new AsyncCallback(OnLocalReceived);
receiveRemoteMethod = new AsyncCallback(OnRemoteReceived);
this.localSocket = localSocket;
this.localSocketOpen = true;
this.tcp = tcp;
receiveLocalMethod = new AsyncCallback(OnLocalReceived);
receiveRemoteMethod = new AsyncCallback(OnRemoteReceived);
 
tcp.SetLocalPoint((IPEndPoint)localSocket.RemoteEndPoint);
this.localSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
785,6 → 809,7
remoteSocket = new Socket(point.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
remoteSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
remoteSocket.Connect(point);
remoteSocketOpen = true;
tcp.SetRemoteState(SocketState.Connected);
tcp.SetRemotePoint((IPEndPoint)remoteSocket.RemoteEndPoint);
 
816,9 → 841,7
try
{
localDataBuffer = new byte[BUF_SIZE];
ThreadDebugger.Add(
localSocket.BeginReceive(localDataBuffer, 0, BUF_SIZE, SocketFlags.None, receiveLocalMethod, this),
"ContinueLocalReceive");
localSocket.BeginReceive(localDataBuffer, 0, BUF_SIZE, SocketFlags.None, receiveLocalMethod, this);
}
catch(ObjectDisposedException ex) // the socket is closed
{
837,9 → 860,7
try
{
remoteDataBuffer = new byte[BUF_SIZE];
ThreadDebugger.Add(
remoteSocket.BeginReceive(remoteDataBuffer, 0, BUF_SIZE, SocketFlags.None, receiveRemoteMethod, this),
"ContinueRemoteReceive");
remoteSocket.BeginReceive(remoteDataBuffer, 0, BUF_SIZE, SocketFlags.None, receiveRemoteMethod, this);
}
catch(ObjectDisposedException ex) // the socket is closed
{
893,6 → 914,8
 
private void OnLocalReceived(IAsyncResult asyn)
{
if(!localSocketOpen) return; // we have already closed the socket
 
try
{
int bytesReceived = 0;
1037,6 → 1060,8
 
private void OnRemoteReceived(IAsyncResult asyn)
{
if(!remoteSocketOpen) return; // we have already closed the socket
 
try
{
int bytesReceived = 0;
1201,6 → 1226,7
tcp.SendLog(LogLevel.Warning, ex);
}
tcp.SetLocalState(SocketState.Closed);
localSocketOpen = false;
 
try
{
1217,6 → 1243,7
tcp.SendLog(LogLevel.Warning, ex);
}
tcp.SetRemoteState(SocketState.Closed);
remoteSocketOpen = false;
 
// return
tcp.OnClose(new TcpEventArgs());
1226,6 → 1253,13
tcp.SendLog(LogLevel.Warning, ex);
}
}
 
public void WaitCancel()
{
// wait for the threads
localSendThread.Join();
remoteSendThread.Join();
}
}
}
 
2502,7 → 2536,6
public class ThreadDebugger
{
private static ArrayList threads = new ArrayList();
private static ArrayList asyncs = new ArrayList();
 
internal static void Add(Thread thread, string comment)
{
2510,25 → 2543,13
Console.WriteLine("ThreadDebugger: thread added {0}", comment);
}
 
internal static void Add(IAsyncResult async, string comment)
{
asyncs.Add(new AsyncItem(async, comment));
Console.WriteLine("ThreadDebugger: async added ", comment);
}
 
public static void PrintStatus()
{
Console.WriteLine("=== ThreadDebugger Status Begin ======");
Console.WriteLine("--- Threads --------------------------");
foreach(ThreadItem t in threads)
{
Console.WriteLine("{0} ({1}): {2}", t.thread.Name, t.comment, t.thread.IsAlive ? "alive" : "dead");
}
Console.WriteLine("--- Asyncs ---------------------------");
foreach(AsyncItem a in asyncs)
{
Console.WriteLine("{0}: {1}", a.comment, a.async.IsCompleted ? "alive" : "dead");
}
Console.WriteLine("=== ThreadDebugger Status End ========");
}
 
2543,17 → 2564,5
this.comment = comment;
}
}
 
private class AsyncItem
{
public IAsyncResult async;
public string comment;
 
public AsyncItem(IAsyncResult async, string comment)
{
this.async = async;
this.comment = comment;
}
}
}
}