Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Brayan Sarmiento
API-Gateway-CHEC
Commits
b11f0788
Commit
b11f0788
authored
Jun 24, 2024
by
Jose Hugo Torres
Browse files
TCPListener2
parent
41a876af
Changes
5
Hide whitespace changes
Inline
Side-by-side
CHECPrueba/Program.cs
View file @
b11f0788
...
@@ -7,7 +7,7 @@ using Serilog;
...
@@ -7,7 +7,7 @@ using Serilog;
public
class
CHECPrueba
public
class
CHECPrueba
{
{
private
const
string
GatewayServer
=
"127.0.0.1"
;
private
const
string
GatewayServer
=
"127.0.0.1"
;
private
const
int
GatewayPort
=
669
7
;
private
const
int
GatewayPort
=
669
0
;
public
static
async
Task
Main
(
string
[]
args
)
public
static
async
Task
Main
(
string
[]
args
)
{
{
...
@@ -70,12 +70,9 @@ public class CHECPrueba
...
@@ -70,12 +70,9 @@ public class CHECPrueba
int
bytesRead
=
await
stream
.
ReadAsync
(
lengthBuffer
,
0
,
4
);
int
bytesRead
=
await
stream
.
ReadAsync
(
lengthBuffer
,
0
,
4
);
if
(
bytesRead
==
0
)
break
;
// La conexión se ha cerrado
if
(
bytesRead
==
0
)
break
;
// La conexión se ha cerrado
if
(
BitConverter
.
IsLittleEndian
)
{
Array
.
Reverse
(
lengthBuffer
);
}
int
messageLength
=
BitConverter
.
ToInt32
(
lengthBuffer
,
0
);
int
messageLength
=
Util
.
LongitudCodificada
(
lengthBuffer
);
Log
.
Information
(
"Mensajde Gateway longitud {longitud}"
,
messageLength
);
byte
[]
messageBuffer
=
new
byte
[
messageLength
];
byte
[]
messageBuffer
=
new
byte
[
messageLength
];
bytesRead
=
await
stream
.
ReadAsync
(
messageBuffer
,
0
,
messageLength
);
bytesRead
=
await
stream
.
ReadAsync
(
messageBuffer
,
0
,
messageLength
);
if
(
bytesRead
==
0
)
continue
;
// La conexión se ha cerrado
if
(
bytesRead
==
0
)
continue
;
// La conexión se ha cerrado
...
...
POSBCPrueba/Program.cs
View file @
b11f0788
...
@@ -6,7 +6,7 @@ using Serilog;
...
@@ -6,7 +6,7 @@ using Serilog;
public
class
PosbcServer
public
class
PosbcServer
{
{
private
const
int
Port
=
669
8
;
private
const
int
Port
=
669
7
;
private
static
int
messageCounter
=
1
;
private
static
int
messageCounter
=
1
;
public
static
async
Task
Main
()
public
static
async
Task
Main
()
...
@@ -52,12 +52,8 @@ public class PosbcServer
...
@@ -52,12 +52,8 @@ public class PosbcServer
int
bytesRead
=
await
stream
.
ReadAsync
(
lengthBuffer
,
0
,
4
);
int
bytesRead
=
await
stream
.
ReadAsync
(
lengthBuffer
,
0
,
4
);
if
(
bytesRead
==
0
)
continue
;
// Si no se lee nada, continuar esperando
if
(
bytesRead
==
0
)
continue
;
// Si no se lee nada, continuar esperando
if
(
BitConverter
.
IsLittleEndian
)
int
messageLength
=
Util
.
LongitudCodificada
(
lengthBuffer
);
{
Log
.
Information
(
"Mensaje de Gateway longitud {longitud}"
,
messageLength
);
Array
.
Reverse
(
lengthBuffer
);
}
int
messageLength
=
BitConverter
.
ToInt32
(
lengthBuffer
,
0
);
byte
[]
messageBuffer
=
new
byte
[
messageLength
];
byte
[]
messageBuffer
=
new
byte
[
messageLength
];
bytesRead
=
await
stream
.
ReadAsync
(
messageBuffer
,
0
,
messageLength
);
bytesRead
=
await
stream
.
ReadAsync
(
messageBuffer
,
0
,
messageLength
);
if
(
bytesRead
==
0
)
continue
;
// Si no se lee nada, continuar esperando
if
(
bytesRead
==
0
)
continue
;
// Si no se lee nada, continuar esperando
...
...
gatewayGK/Program.cs
View file @
b11f0788
...
@@ -18,14 +18,14 @@ namespace GatewaySCO
...
@@ -18,14 +18,14 @@ namespace GatewaySCO
/// </summary>
/// </summary>
public
class
ProgramGatewaySCO
public
class
ProgramGatewaySCO
{
{
public
static
void
Hola
(
string
[]
args
)
public
static
async
Task
Main
(
string
[]
args
)
{
{
Console
.
WriteLine
(
"*** Gateway SCO - Servidor: procesa peticiones de Toshiba CHEC ***"
);
Console
.
WriteLine
(
"*** Gateway SCO - Servidor: procesa peticiones de Toshiba CHEC ***"
);
ProgramGatewaySCO
program
=
new
();
ProgramGatewaySCO
program
=
new
();
program
.
LeeActivaConfiguracion
(
args
);
program
.
LeeActivaConfiguracion
(
args
);
TcpGatewayB
gateway
=
new
TcpGatewayB
();
TcpGatewayB
gateway
=
new
TcpGatewayB
();
//
await gateway.Start();
await
gateway
.
Start
();
}
}
...
...
gatewayGK/Servidor/TcpGatewayA.cs
deleted
100644 → 0
View file @
41a876af
using
System
;
using
System.Net
;
using
System.Net.Sockets
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
Servidor.TcpGatewayA
;
/******************************************************************************
SERVIDOR CIERRA LA CONEXIÓN CHEC LUEGO DEL PRIMER MENSAJE, ESPERANDO QUE
CHEC SE CONECTE NUEVAMENTE.
*/
public
class
TcpGateway
{
private
const
int
GatewayPort
=
6697
;
private
const
string
PosbcServer
=
"127.0.0.1"
;
private
const
int
PosbcPort
=
6698
;
public
static
async
Task
Main
()
{
TcpListener
gatewayListener
=
new
TcpListener
(
IPAddress
.
Any
,
GatewayPort
);
gatewayListener
.
Start
();
Console
.
WriteLine
(
$"Gateway escuchando en el puerto
{
GatewayPort
}
..."
);
while
(
true
)
{
TcpClient
checClient
=
await
gatewayListener
.
AcceptTcpClientAsync
();
Console
.
WriteLine
(
"CHEC conectado."
);
_
=
Task
.
Run
(()
=>
HandleChecClientAsync
(
checClient
));
}
}
private
static
async
Task
HandleChecClientAsync
(
TcpClient
checClient
)
{
using
(
checClient
)
using
(
NetworkStream
checStream
=
checClient
.
GetStream
())
using
(
TcpClient
posbcClient
=
new
TcpClient
())
{
await
posbcClient
.
ConnectAsync
(
PosbcServer
,
PosbcPort
);
Console
.
WriteLine
(
"Conectado a POSBC."
);
using
(
NetworkStream
posbcStream
=
posbcClient
.
GetStream
())
{
Task
receiveFromPosbcTask
=
ReceiveFromPosbcAndSendToChecAsync
(
posbcStream
,
checStream
);
while
(
true
)
{
// Leer mensaje desde CHEC
byte
[]
lengthBuffer
=
new
byte
[
4
];
int
bytesRead
=
await
checStream
.
ReadAsync
(
lengthBuffer
,
0
,
4
);
if
(
bytesRead
==
0
)
break
;
// La conexión se ha cerrado
if
(
BitConverter
.
IsLittleEndian
)
{
Array
.
Reverse
(
lengthBuffer
);
}
int
messageLength
=
BitConverter
.
ToInt32
(
lengthBuffer
,
0
);
byte
[]
messageBuffer
=
new
byte
[
messageLength
];
bytesRead
=
await
checStream
.
ReadAsync
(
messageBuffer
,
0
,
messageLength
);
if
(
bytesRead
==
0
)
break
;
// La conexión se ha cerrado
// Remitir mensaje a POSBC
await
posbcStream
.
WriteAsync
(
lengthBuffer
,
0
,
4
);
await
posbcStream
.
WriteAsync
(
messageBuffer
,
0
,
messageLength
);
Console
.
WriteLine
(
$"Mensaje remitido a POSBC:
{
Encoding
.
UTF8
.
GetString
(
messageBuffer
)}
"
);
}
await
receiveFromPosbcTask
;
}
}
}
private
static
async
Task
ReceiveFromPosbcAndSendToChecAsync
(
NetworkStream
posbcStream
,
NetworkStream
checStream
)
{
byte
[]
buffer
=
new
byte
[
4096
];
while
(
true
)
{
// Leer longitud del mensaje desde POSBC
byte
[]
lengthBuffer
=
new
byte
[
4
];
int
bytesRead
=
await
posbcStream
.
ReadAsync
(
lengthBuffer
,
0
,
4
);
if
(
bytesRead
==
0
)
break
;
// La conexión se ha cerrado
if
(
BitConverter
.
IsLittleEndian
)
{
Array
.
Reverse
(
lengthBuffer
);
}
int
messageLength
=
BitConverter
.
ToInt32
(
lengthBuffer
,
0
);
byte
[]
messageBuffer
=
new
byte
[
messageLength
];
bytesRead
=
await
posbcStream
.
ReadAsync
(
messageBuffer
,
0
,
messageLength
);
if
(
bytesRead
==
0
)
break
;
// La conexión se ha cerrado
// Remitir mensaje a CHEC
await
checStream
.
WriteAsync
(
lengthBuffer
,
0
,
4
);
await
checStream
.
WriteAsync
(
messageBuffer
,
0
,
messageLength
);
Console
.
WriteLine
(
$"Mensaje remitido a CHEC:
{
Encoding
.
UTF8
.
GetString
(
messageBuffer
)}
"
);
}
}
}
gatewayGK/Servidor/TcpGatewayB.cs
View file @
b11f0788
...
@@ -9,21 +9,17 @@ namespace Servidor.TcpGatewayB;
...
@@ -9,21 +9,17 @@ namespace Servidor.TcpGatewayB;
public
class
TcpGatewayB
public
class
TcpGatewayB
{
{
private
const
int
GatewayPort
=
669
8
;
private
const
int
GatewayPort
=
669
0
;
private
const
string
PosbcServer
=
"127.0.0.1"
;
private
const
string
PosbcServer
=
"127.0.0.1"
;
private
const
int
PosbcPort
=
6697
;
private
const
int
PosbcPort
=
6697
;
public
async
Task
Start
()
public
async
Task
Start
()
{
{
Log
.
Logger
=
new
LoggerConfiguration
()
.
WriteTo
.
Console
()
.
CreateLogger
();
try
try
{
{
TcpListener
gatewayListener
=
new
TcpListener
(
IPAddress
.
Any
,
GatewayPort
);
TcpListener
gatewayListener
=
new
TcpListener
(
IPAddress
.
Any
,
GatewayPort
);
gatewayListener
.
Start
();
gatewayListener
.
Start
();
Log
.
Information
(
$"Gateway escuchando en el puerto
{
GatewayPort
}
..."
);
Log
.
Information
(
$"Gateway
B#1
escuchando en el puerto
{
GatewayPort
}
..."
);
while
(
true
)
while
(
true
)
{
{
...
@@ -63,12 +59,8 @@ public class TcpGatewayB
...
@@ -63,12 +59,8 @@ public class TcpGatewayB
int
bytesRead
=
await
checStream
.
ReadAsync
(
lengthBuffer
,
0
,
4
);
int
bytesRead
=
await
checStream
.
ReadAsync
(
lengthBuffer
,
0
,
4
);
if
(
bytesRead
==
0
)
continue
;
// Si no se lee nada, continuar esperando
if
(
bytesRead
==
0
)
continue
;
// Si no se lee nada, continuar esperando
if
(
BitConverter
.
IsLittleEndian
)
int
messageLength
=
Util
.
LongitudCodificada
(
lengthBuffer
);
{
Log
.
Debug
(
"Mensaje de CHEC longitud {longitud}"
,
messageLength
);
Array
.
Reverse
(
lengthBuffer
);
}
int
messageLength
=
BitConverter
.
ToInt32
(
lengthBuffer
,
0
);
byte
[]
messageBuffer
=
new
byte
[
messageLength
];
byte
[]
messageBuffer
=
new
byte
[
messageLength
];
bytesRead
=
await
checStream
.
ReadAsync
(
messageBuffer
,
0
,
messageLength
);
bytesRead
=
await
checStream
.
ReadAsync
(
messageBuffer
,
0
,
messageLength
);
if
(
bytesRead
==
0
)
continue
;
// Si no se lee nada, continuar esperando
if
(
bytesRead
==
0
)
continue
;
// Si no se lee nada, continuar esperando
...
@@ -105,12 +97,8 @@ public class TcpGatewayB
...
@@ -105,12 +97,8 @@ public class TcpGatewayB
int
bytesRead
=
await
posbcStream
.
ReadAsync
(
lengthBuffer
,
0
,
4
);
int
bytesRead
=
await
posbcStream
.
ReadAsync
(
lengthBuffer
,
0
,
4
);
if
(
bytesRead
==
0
)
continue
;
// Si no se lee nada, continuar esperando
if
(
bytesRead
==
0
)
continue
;
// Si no se lee nada, continuar esperando
if
(
BitConverter
.
IsLittleEndian
)
int
messageLength
=
Util
.
LongitudCodificada
(
lengthBuffer
);
{
Log
.
Debug
(
"Mensajde de POSBC longitud {longitud}"
,
messageLength
);
Array
.
Reverse
(
lengthBuffer
);
}
int
messageLength
=
BitConverter
.
ToInt32
(
lengthBuffer
,
0
);
byte
[]
messageBuffer
=
new
byte
[
messageLength
];
byte
[]
messageBuffer
=
new
byte
[
messageLength
];
bytesRead
=
await
posbcStream
.
ReadAsync
(
messageBuffer
,
0
,
messageLength
);
bytesRead
=
await
posbcStream
.
ReadAsync
(
messageBuffer
,
0
,
messageLength
);
if
(
bytesRead
==
0
)
continue
;
// Si no se lee nada, continuar esperando
if
(
bytesRead
==
0
)
continue
;
// Si no se lee nada, continuar esperando
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment