Commit 6ad5b3de authored by Jose Hugo Torres's avatar Jose Hugo Torres
Browse files

Gateway #14

parent 494da98c
......@@ -110,10 +110,10 @@ public class ServidorGatewayCHEC(string ipGateway, int ptoGateway, string ipPOSB
idProceso = Environment.ProcessId;
#endif
Log.Information("Gateway servidor socket en {ip} : {puerto}, proceso id {id}", IpGateway, PuertoGateway, idProceso);
Log.Information("Servidor GATEWAY CHEC socket en {ip} : {puerto}, proceso id {id}", IpGateway, PuertoGateway, idProceso);
if (_isDebug)
{
Log.Debug("Versión framework {version} # 13", Environment.Version);
Log.Debug("Versión framework {version} # 14", Environment.Version);
Log.Debug("Tcp Socket configuración:");
Log.Debug($" Blocking {tcpSocket.Blocking}");
Log.Debug($" ExclusiveAddressUse {tcpSocket.ExclusiveAddressUse}");
......@@ -129,23 +129,23 @@ public class ServidorGatewayCHEC(string ipGateway, int ptoGateway, string ipPOSB
try
{
//var tareaRecibePOSBC = Task.Run(() => _posbc.Recibe(_canalSalida));
ActivaPOSBC();
// Procesa conexiones al Gateway.
while (true)
{
Log.Information("Esperando conexión");
Socket clienteCHEC = tcpSocket.Accept();
Log.Information("Conexión remota ip {ip} en puerto {pto}",
Log.Information("Conexión remota CHEC ip {ip} en puerto {pto}",
IPAddress.Parse(((IPEndPoint)clienteCHEC.RemoteEndPoint).Address.ToString()),
((IPEndPoint)clienteCHEC.RemoteEndPoint).Port.ToString());
// Activa el POSBC
ActivaPOSBC();
var tareaAceptaEntradasCHEC = Task.Run(() => AceptaEntradasCHEC(clienteCHEC));
var tareaRemiteRespuestaPOSBC = Task.Run(() => ProcesaSalidaPOSBC(clienteCHEC, _posbc.Socket));
var tareaAceptaEntradasCHEC = Task.Run(() => AceptaEntradasCHEC(clienteCHEC));
}
}
finally
{
Log.Warning("Servidor GATEWAY CERRADO.");
tcpSocket.Close();
_posbc.CierraConexion();
}
......
......@@ -30,8 +30,14 @@ public class SocketServer
// Recibir el mensaje del cliente
byte[] buffer = new byte[4096];
int bytesRead = clientSocket.Receive(buffer);
string message = Encoding.UTF8.GetString(buffer, 0, bytesRead);
int longitudMsj = 0;
if (bytesRead > 4 ) {
longitudMsj = Util.LongitudCodificada(buffer);
}
string message = Encoding.UTF8.GetString(buffer, 4, 4 + longitudMsj);
Console.WriteLine($"Mensaje recibido: {message}");
string msjRespuesta = message + " desde POSBC";
buffer = Util.ConvierteEnBufferBytes(msjRespuesta);
// Enviar el mismo mensaje de vuelta al cliente
clientSocket.Send(buffer, bytesRead, SocketFlags.None);
......@@ -39,18 +45,18 @@ public class SocketServer
// Enviar mensajes aleatorios adicionales
Random random = new Random();
int additionalMessages = random.Next(0, 6); // Número aleatorio entre 0 y 5
int additionalMessages = random.Next(0, 3); // Número aleatorio entre 0 y 5
for (int i = 1; i <= additionalMessages; i++)
{
int delay = random.Next(100, 5001); // Tiempo de espera aleatorio entre 0.1 y 5 segundos
Task.Delay(delay).Wait(); // Espera asincrónica
string additionalMessage = $"mensaje aleatorio {i} después de {delay}ms";
string additionalMessage = $"mensaje POSBC aleatorio {i} después de {delay}ms";
byte[] additionalData = Util.ConvierteEnBufferBytes(additionalMessage);
clientSocket.Send(additionalData, SocketFlags.None);
Console.WriteLine($"Mensaje adicional enviado: {additionalMessage}");
Console.WriteLine(Util.DetalleMensajes(additionalData));
Console.WriteLine($"Mensaje adicional enviado: {additionalMessage} desde POSBC");
//Console.WriteLine(Util.DetalleMensajes(additionalData));
}
}
}
......
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