Kleine AIS Korrekturen

This commit is contained in:
Daniel Schick 2018-06-23 10:51:52 +00:00
parent e54b6661cc
commit 4a2feba85a
3 changed files with 18 additions and 11 deletions

View File

@ -40,6 +40,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>
<OutputPath>bin\Debug\</OutputPath> <OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>

View File

@ -50,7 +50,7 @@ namespace LS100PortProxy
} }
private class ConnectionInfo private class ConnectionInfo
{ {
public Socket Socket; public Socket Socket;
public Thread Thread; public Thread Thread;
public Queue<Chunk> chunks = new Queue<Chunk>(); public Queue<Chunk> chunks = new Queue<Chunk>();
@ -112,16 +112,16 @@ namespace LS100PortProxy
{ // Accept a connection { // Accept a connection
try try
{ {
Socket socket = _serverSocket.Accept(); Socket socket = _serverSocket.Accept();
_log.InfoFormat("new connection from {0}", socket.RemoteEndPoint.ToString());
ConnectionInfo connection = new ConnectionInfo(); ConnectionInfo connection = new ConnectionInfo();
connection.Socket = socket; // Create the thread for the receives. connection.Socket = socket; // Create the thread for the receives.
connection.Thread = new Thread(ProcessConnection); connection.Thread = new Thread(ProcessConnection);
connection.Thread.IsBackground = true; connection.Thread.IsBackground = true;
connection.Thread.Start(connection); connection.Thread.Start(connection);
// Store the socket // Store the socket
lock (_connections) lock (_connections)
_connections.Add(connection); _connections.Add(connection);
_log.InfoFormat("new connection from {0}, total connections: {1}", socket.RemoteEndPoint.ToString(), _connections.Count);
} }
catch (SocketException) { /* tu was */ } catch (SocketException) { /* tu was */ }
Thread.Sleep(250); Thread.Sleep(250);
@ -138,7 +138,7 @@ namespace LS100PortProxy
{ {
// write all chunks in incoming chunk queue to this socket // write all chunks in incoming chunk queue to this socket
if (connection.Socket.Poll(1000, SelectMode.SelectWrite)) if (connection.Socket.Poll(500, SelectMode.SelectWrite))
{ {
while (connection.chunks.Count > 0) while (connection.chunks.Count > 0)
{ {
@ -146,6 +146,7 @@ namespace LS100PortProxy
lock (_connections) lock (_connections)
{ {
aChunk = connection.chunks.Dequeue(); aChunk = connection.chunks.Dequeue();
//Trace.WriteLine(string.Format("{0} : {1}", connection.Socket.RemoteEndPoint, connection.chunks.Count));
} }
int bytesToSend = aChunk.length; int bytesToSend = aChunk.length;
byte[] buffer = aChunk.data; byte[] buffer = aChunk.data;
@ -155,12 +156,13 @@ namespace LS100PortProxy
//string[] lines = testOutput.Split('\r'); //string[] lines = testOutput.Split('\r');
//foreach(string line in lines) //foreach(string line in lines)
// _log.Debug(line); // _log.Debug(line);
int actuallySentBytes = connection.Socket.Send(aChunk.data, aChunk.length, SocketFlags.None); int actuallySentBytes = connection.Socket.Send(buffer, bytesToSend, SocketFlags.None);
//Trace.WriteLine(string.Format("{0}/{1}", actuallySentBytes, bytesToSend));
bytesToSend -= actuallySentBytes; bytesToSend -= actuallySentBytes;
if (bytesToSend > 0) if (bytesToSend > 0)
{ {
byte[] newBuffer = new byte[bytesToSend]; byte[] newBuffer = new byte[bytesToSend];
Array.Copy(buffer, aChunk.length - bytesToSend, newBuffer, 0, bytesToSend); Array.Copy(buffer, actuallySentBytes, newBuffer, 0, bytesToSend);
buffer = newBuffer; buffer = newBuffer;
} }
} }
@ -188,15 +190,14 @@ namespace LS100PortProxy
{ Console.WriteLine("Exception: " + exc); } { Console.WriteLine("Exception: " + exc); }
finally finally
{ {
_log.InfoFormat("removing connection to {0}", connection.Socket.RemoteEndPoint);
connection.Socket.Close(); connection.Socket.Close();
lock (_connections) lock (_connections)
_connections.Remove(connection); _connections.Remove(connection);
_log.InfoFormat("connection closed, total connections: {0}", _connections.Count);
GC.Collect(); GC.Collect();
} }
} }
internal void StartClient() internal void StartClient()
{ {
this._clientThread = new Thread(new ThreadStart(this.ClientConnection)); this._clientThread = new Thread(new ThreadStart(this.ClientConnection));
@ -253,9 +254,10 @@ namespace LS100PortProxy
if(j > chunk.length) // es wurden CR eingefügt if(j > chunk.length) // es wurden CR eingefügt
{ {
chunk.data = null;
chunk.data = target; chunk.data = target;
chunk.length = j; chunk.length = j;
_log.DebugFormat("Chunk replaced, new length {0}", j); _log.DebugFormat("Chunk data replaced, new length {0}", j);
} }
if (chunk.length > 0 && (this._connections.Count > 0)) if (chunk.length > 0 && (this._connections.Count > 0))
@ -267,10 +269,14 @@ namespace LS100PortProxy
{ {
foreach (ConnectionInfo connectionInfo in _connections) foreach (ConnectionInfo connectionInfo in _connections)
{ {
connectionInfo.chunks.Enqueue(chunk.DeepClone()); connectionInfo.chunks.Enqueue(chunk.DeepClone());
} }
} }
chunk.data = null;
} }
chunk = null;
} }
} }
else else

Binary file not shown.