Commit 5f1abf15 authored by Jose Hugo Torres's avatar Jose Hugo Torres
Browse files

Lectura controlada

Controla los bytes leidos.
parent ac503a47
......@@ -19,7 +19,7 @@ public class TcpGateway
{
TcpListener gatewayListener = new TcpListener(IPAddress.Any, GatewayPort);
gatewayListener.Start();
Log.Information($"Gateway B#1 escuchando en el puerto {GatewayPort}...");
Log.Information($"Gateway B#2 escuchando en el puerto {GatewayPort}...");
while (true)
{
......@@ -56,14 +56,25 @@ public class TcpGateway
{
// Leer mensaje desde CHEC
byte[] lengthBuffer = new byte[4];
int bytesRead = await checStream.ReadAsync(lengthBuffer, 0, 4);
if (bytesRead == 0) continue; // Si no se lee nada, continuar esperando
int bytesRead = await ReadExactlyAsync(checStream, lengthBuffer, 4);
//int bytesRead = await checStream.ReadAsync(lengthBuffer, 0, 4);
if (bytesRead == 0)
{
Log.Debug("CHEC cabecera bytesRead = 0");
continue;
} // Si no se lee nada, continuar esperando
int messageLength = Util.LongitudCodificada(lengthBuffer);
Log.Debug("Mensaje de CHEC longitud {longitud}", messageLength);
byte[] messageBuffer = new byte[messageLength];
bytesRead = await checStream.ReadAsync(messageBuffer, 0, messageLength);
if (bytesRead == 0) continue; // Si no se lee nada, continuar esperando
// Leer el mensaje completo basado en la longitud esperada
bytesRead = await ReadExactlyAsync(checStream, messageBuffer, messageLength);
//bytesRead = await checStream.ReadAsync(messageBuffer, 0, messageLength);
if (bytesRead == 0)
{
Log.Debug("CHEC cuerpo bytesRead = 0");
continue;
} // Si no se lee nada, continuar esperando
// Remitir mensaje a POSBC
byte[] bytesMsj = Util.ConcatenaArreglosBytes(lengthBuffer, messageBuffer);
......@@ -92,14 +103,25 @@ public class TcpGateway
{
// Leer longitud del mensaje desde POSBC
byte[] lengthBuffer = new byte[4];
int bytesRead = await posbcStream.ReadAsync(lengthBuffer, 0, 4);
if (bytesRead == 0) continue; // Si no se lee nada, continuar esperando
int bytesRead = await ReadExactlyAsync(posbcStream, lengthBuffer, 4);
//int bytesRead = await posbcStream.ReadAsync(lengthBuffer, 0, 4);
if (bytesRead == 0)
{
Log.Debug("POSBC cabecera bytesRead = 0");
continue;
} // Si no se lee nada, continuar esperando
int messageLength = Util.LongitudCodificada(lengthBuffer);
Log.Debug("POSBC bytes cabecera leidos {bytes}, longitud en cabecera {longitud}", bytesRead, messageLength);
byte[] messageBuffer = new byte[messageLength];
bytesRead = await posbcStream.ReadAsync(messageBuffer, 0, messageLength);
Log.Debug("POSBC bytes cuerpo ledidos {bytes}", bytesRead);
bytesRead = await ReadExactlyAsync(posbcStream, messageBuffer, messageLength);
//bytesRead = await posbcStream.ReadAsync(messageBuffer, 0, messageLength);
Log.Debug("POSBC bytes cuerpo leidos {bytes}", bytesRead);
if (bytesRead == 0)
{
Log.Debug("POSBC cuerpo bytesRead = 0");
continue;
} // Si no se lee nada, continuar esperando
if (bytesRead == 0) continue; // Si no se lee nada, continuar esperando
// Remitir mensaje a CHEC
......@@ -117,4 +139,19 @@ public class TcpGateway
}
}
}
private async Task<int> ReadExactlyAsync(NetworkStream stream, byte[] buffer, int length)
{
int totalBytesRead = 0;
while (totalBytesRead < length)
{
int bytesRead = await stream.ReadAsync(buffer, totalBytesRead, length - totalBytesRead);
if (bytesRead == 0)
{
return 0; // La conexión se ha cerrado
}
totalBytesRead += bytesRead;
}
return totalBytesRead;
}
}
Supports Markdown
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