Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
tcp-controller
Advanced tools
A simple TCP controller framework. With TCP Controller you can manage your TCP server more easily.
The TCP Controller is a framework to create your own TCP client/server using stream and message stream based support to send and receive data.
To use this library you need to create a class that will be the TCP controller and then annotate it with @TcpController([address, port]). The entire architecture was based on the REST API standard and ported to the transport layer of the TCP/IP model.
@TcpController([address, port])
@TcpMessageStream()
@TcpMessage()
@TcpError()
@TcpListening()
@TcpClientDisconnected()
@TcpClientConnected()
@TcpCriteria([id])
@TcpCriteriaId([id])
@TcpBuffer()
@TcpSocket()
@TcpInjectSocket()
You must run the following terminal command.
npm install tcp-controller --save
The first thing to do to create your gateway and interact with a server that supports flow-oriented TCP/IP or message flow is to create a class and annotate it with @TcpController
, then add at least one method of message processing annotated by @TcpMessage
. After creating the class, you need to call createServer
to start the lifecycle of your gateway if it is a server or clientConnect
if the gateway is a TCP/IP client.
In TCP Controller, the life cycle of a TCP connection is regulated by 3 decorators, they are: @TcpClientConnected
, @TcpClientDisconnected
, @TcpError
. Whenever the connection is successfully established, the method decorated with @TcpClientConnected
is invoked, just as when the connection is closed the method decorated with @TcpClientDisconnected
is invoked. In TCP Controller there are 2 types of exceptions, those caused by network failures and those caused by application code failures. The method decorated with @TcpError
catches TCP connection errors. There is currently no equivalent for exceptions caused by application crashes.
To send a reply for each incoming message you can use the send
function which is declared in the TcpGateway class. Create your Tcp Controller class and inherit from the TcpGateway class. You can call the send
function more than once in an incoming message.
For a more detailed overview see the example of integration below using a Gateway to receive messages. Here is a TCP server sample:
import { Socket } from "net";
import {
createServer,
NativeSocket,
TcpGateway,
TcpBuffer,
TcpClientConnected,
TcpClientDisconnected,
TcpController,
TcpCriteria,
TcpCriteriaId,
TcpError,
TcpListening,
TcpMessage,
TcpMessageStream,
TcpSocket
} from "tcp-controller";
@TcpController("0.0.0.0", 1337)
@TcpMessageStream()
class TcpServer extends TcpGateway {
@TcpClientConnected()
onConnect(@TcpSocket() sock: NativeSocket) {
console.log("[Server] Connected client from " + sock.remoteAddress);
this.send(sock, Buffer.from("Server to client!"));
this.send(sock, Buffer.from("5erver to client!"));
}
@TcpClientDisconnected()
onDisconnect(hadError: boolean) {
console.log("[Server] Disconnected client with error? " + hadError);
}
@TcpError()
onError(error: Error) {
console.log("[Server] Error happened: " + error.message);
}
@TcpListening()
onListening() {
console.log("[Server] Server up.");
}
@TcpCriteria("message")
@TcpMessage()
onMessage(@TcpBuffer() buffer: Buffer, @TcpSocket() sock: NativeSocket) {
console.log("[Server] Message received from " + sock.remoteAddress + ".\nMessage: " + buffer.toString());
}
@TcpCriteriaId("message")
isMessage(@TcpBuffer() buffer: Buffer) {
return buffer.toString().includes("Client to server!");
}
}
createServer(TcpServer);
And here is a Client TCP Gateway:
import { Socket } from "net";
import {
clientConnect,
NativeSocket,
TcpGateway,
TcpBuffer,
TcpClientConnected,
TcpClientDisconnected,
TcpController,
TcpCriteria,
TcpCriteriaId,
TcpError,
TcpMessage,
TcpMessageStream,
TcpSocket
} from "tcp-controller";
@TcpController("0.0.0.0", 1337)
@TcpMessageStream()
class TcpClient extends TcpGateway {
@TcpClientConnected()
onConnect(@TcpSocket() sock: NativeSocket) {
console.log("[Client] Connected to server " + sock.remoteAddress);
this.send(sock, Buffer.from("Client to server!"));
this.send(sock, Buffer.from("Client to 5erver!"));
}
@TcpClientDisconnected()
onDisconnect(hadError: boolean) {
console.log("[Client] Disconnected with error? " + hadError);
}
@TcpError()
onError(error: Error) {
console.log("[Client] Error happened: " + error.message);
}
@TcpCriteria("message")
@TcpMessage()
onMessage(@TcpBuffer() buffer: Buffer, @TcpSocket() sock: NativeSocket) {
console.log("[Client] Message received from " + sock.remoteAddress + ".\nMessage: " + buffer.toString());
}
@TcpCriteriaId("message")
isMessage(@TcpBuffer() buffer: Buffer) {
return buffer.toString().includes("Server to client!");
}
}
clientConnect(TcpClient);
Muryllo Pimenta de Oliveira – muryllo.pimenta@upe.br
Distributed under MIT license. See LICENSE
for more informations.
git checkout -b feature/fooBar
)git commit -am 'Add some fooBar'
)git push origin feature/fooBar
)FAQs
A simple TCP controller framework. With TCP Controller you can manage your TCP server more easily.
We found that tcp-controller demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.