Interface Network<T>

Network defines the interaction layer between the server and the network. It is responsible for parsing incoming requests and serializing outgoing responses.

The network interface is extensible to any network protocol, such as TCP, UDP, or HTTP.

Network should define basic methods to handle incoming requests and send outgoing responses.

interface Network<T> {
    handler?: NetworkHandler;
    networkType: SupportedNetworkType;
    serializer: Serializer<T>;
    close(): void;
    listen(callback?: (() => void)): void;
    off(event: string, listener: (() => void)): void;
    on(event: string, listener: (() => void)): void;
}

Type Parameters

  • T

Implemented by

Properties

handler?: NetworkHandler

The handler is responsible for processing incoming requests and sending outgoing responses.

The type of network being used

serializer: Serializer<T>

The serializer is responsible for encoding and decoding packets. This is the piece of logic responsible for communicating directly with the wire format for whatever network protocol is used by the network interface.

Methods

  • Begin listening for incoming requests.

    The listen method should be able to effectively bootstrap the network interface without accepting additional parameters. This is to support polymorphic startups across a variety of network interfaces without needing to know the specifics of each.

    For example, some network interfaces may bind an array of ports, while others may bind to a single port and address. The listen method should be able to simply call without needing to know the specifics of the network interface.

    Parameters

    • Optionalcallback: (() => void)

      The callback to call when the network interface is ready to receive requests

        • (): void
        • Returns void

    Returns void

  • Removes the listener from the underlying network interface.

    Parameters

    • event: string

      The event to unsubscribe from

    • listener: (() => void)

      The listener to remove

        • (): void
        • Returns void

    Returns void

  • Passes the listener to the underlying network interface which may be an event emitter.

    Parameters

    • event: string

      The event to subscribe to

    • listener: (() => void)

      The listener to call when the event is emitted

        • (): void
        • Returns void

    Returns void