Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1240 → Rev 1239

/TCPproxy/trunk/MainForm.cs
49,7 → 49,6
private int resendPort;
private TcpShowMode tcpShowMode = TcpShowMode.ByDirection;
private bool autoExpand = true;
private bool clearing = false;
 
private string defaultCaption;
 
609,8 → 608,6
 
protected override void Dispose(bool disposing)
{
clearing = true;
 
if(tcpListener != null) {
tcpListener.StopListening(); // stop listening
tcpListener.CancelAll(); // cancel all open connections
1065,9 → 1062,12
 
private void ClearAll()
{
clearing = true;
// close all connetions
foreach(object tcp in treeNodes.Keys)
if(tcp is TcpConnection)
((TcpConnection)tcp).Cancel();
 
tcpListener.CancelAll();
// FIXME wait for all log messages from network part
 
treeNodes.Clear();
1077,8 → 1077,6
 
messagesBox.Clear();
logMessages.Clear();
 
clearing = false;
}
 
private void Start(int listenPort, IPAddress resendHost, int resendPort)
1257,8 → 1255,6
#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);
socket.BeginAccept(new AsyncCallback(OnClientConnect), null);
ThreadDebugger.Add(socket.BeginAccept(new AsyncCallback(OnClientConnect), null), "StartListening");
SendLog(null, LogLevel.Important, "Listen on " + socket.LocalEndPoint);
}
 
198,24 → 198,17
 
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)
223,7 → 216,7
try
{
Socket worker = socket.EndAccept(asyn);
socket.BeginAccept(new AsyncCallback(OnClientConnect), null); // wait for next client
ThreadDebugger.Add(socket.BeginAccept(new AsyncCallback(OnClientConnect), null), "OnClientConnect"); // wait for next client
 
TcpConnection tcp = new TcpConnection(string.Format("{0:0000}", tcpId++));
tcp.Close += new TcpEventHandler(TcpConnectionClosed);
396,12 → 389,6
worker.Cancel();
}
 
public void WaitCancel()
{
if(worker != null)
worker.WaitCancel();
}
 
protected TcpMessage Append(TcpMessageDirection direction, byte[] newBytes)
{
return Append(direction, newBytes, newBytes.Length);
449,12 → 436,6
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)
512,7 → 493,6
this.localState = localState;
if(this.localState == SocketState.Closed) httpParser.NewMessageArived();
OnUpdate(new TcpEventArgs());
CheckClosed();
}
 
protected void SetRemoteState(SocketState remoteState)
570,7 → 550,6
this.remoteState = remoteState;
if(this.remoteState == SocketState.Closed) httpParser.NewMessageArived();
OnUpdate(new TcpEventArgs());
CheckClosed();
}
 
public event TcpEventHandler Update;
752,9 → 731,7
 
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;
781,11 → 758,10
((IPEndPoint)localSocket.RemoteEndPoint).Address,
((IPEndPoint)localSocket.RemoteEndPoint).Port));
 
this.localSocket = localSocket;
this.localSocketOpen = true;
this.tcp = tcp;
receiveLocalMethod = new AsyncCallback(OnLocalReceived);
receiveRemoteMethod = new AsyncCallback(OnRemoteReceived);
this.localSocket = localSocket;
this.tcp = tcp;
receiveLocalMethod = new AsyncCallback(OnLocalReceived);
receiveRemoteMethod = new AsyncCallback(OnRemoteReceived);
 
tcp.SetLocalPoint((IPEndPoint)localSocket.RemoteEndPoint);
this.localSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
809,7 → 785,6
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);
 
841,7 → 816,9
try
{
localDataBuffer = new byte[BUF_SIZE];
localSocket.BeginReceive(localDataBuffer, 0, BUF_SIZE, SocketFlags.None, receiveLocalMethod, this);
ThreadDebugger.Add(
localSocket.BeginReceive(localDataBuffer, 0, BUF_SIZE, SocketFlags.None, receiveLocalMethod, this),
"ContinueLocalReceive");
}
catch(ObjectDisposedException ex) // the socket is closed
{
860,7 → 837,9
try
{
remoteDataBuffer = new byte[BUF_SIZE];
remoteSocket.BeginReceive(remoteDataBuffer, 0, BUF_SIZE, SocketFlags.None, receiveRemoteMethod, this);
ThreadDebugger.Add(
remoteSocket.BeginReceive(remoteDataBuffer, 0, BUF_SIZE, SocketFlags.None, receiveRemoteMethod, this),
"ContinueRemoteReceive");
}
catch(ObjectDisposedException ex) // the socket is closed
{
914,8 → 893,6
 
private void OnLocalReceived(IAsyncResult asyn)
{
if(!localSocketOpen) return; // we have already closed the socket
 
try
{
int bytesReceived = 0;
1060,8 → 1037,6
 
private void OnRemoteReceived(IAsyncResult asyn)
{
if(!remoteSocketOpen) return; // we have already closed the socket
 
try
{
int bytesReceived = 0;
1226,7 → 1201,6
tcp.SendLog(LogLevel.Warning, ex);
}
tcp.SetLocalState(SocketState.Closed);
localSocketOpen = false;
 
try
{
1243,7 → 1217,6
tcp.SendLog(LogLevel.Warning, ex);
}
tcp.SetRemoteState(SocketState.Closed);
remoteSocketOpen = false;
 
// return
tcp.OnClose(new TcpEventArgs());
1253,13 → 1226,6
tcp.SendLog(LogLevel.Warning, ex);
}
}
 
public void WaitCancel()
{
// wait for the threads
localSendThread.Join();
remoteSendThread.Join();
}
}
}
 
2536,6 → 2502,7
public class ThreadDebugger
{
private static ArrayList threads = new ArrayList();
private static ArrayList asyncs = new ArrayList();
 
internal static void Add(Thread thread, string comment)
{
2543,13 → 2510,25
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 ========");
}
 
2564,5 → 2543,17
this.comment = comment;
}
}
 
private class AsyncItem
{
public IAsyncResult async;
public string comment;
 
public AsyncItem(IAsyncResult async, string comment)
{
this.async = async;
this.comment = comment;
}
}
}
}