tcp

package
v1.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2024 License: Apache-2.0 Imports: 8 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TCPClient

type TCPClient struct {
	// tcp server targets
	ServerIP   string
	ServerPort uint

	ReceiveHandler func(data []byte)
	ErrorHandler   func(err error, socketCloseFunc func())

	ReadBufferSize      uint
	ReaderYieldDuration time.Duration

	ReadDeadLineDuration  time.Duration
	WriteDeadLineDuration time.Duration
	// contains filtered or unexported fields
}

TCPClient defines tcp client connection struct

ServerIP = tcp server ip address ServerPort = tcp server port ReceiveHandler = func handler to be triggered when data is received from tcp server ErrorHandler = func handler to be triggered when error is received from tcp server (while performing reader service) ReadBufferSize = default: 1024, defines the read buffer byte size, if > 65535, defaults to 1024 ReaderYieldDuration = default: 25ms, defines the amount of time yielded during each reader service loop cycle, if > 1000ms, defaults to 25ms ReadDeadLineDuration = default: 1000ms, defines the amount of time given to read action before timeout, valid range is 250ms - 5000ms WriteDeadLineDuration = default: 0, duration value used to control write timeouts, this value is added to current time during write timeout set action

func (*TCPClient) Close

func (c *TCPClient) Close()

Close will close the tcp connection for the current TCPClient struct object

func (*TCPClient) Dial

func (c *TCPClient) Dial() error

Dial will dial tcp client to the remote tcp server host ip and port defined within the TCPClient struct, if Dial failed, an error is returned, if Dial succeeded, nil is returned

func (*TCPClient) Read

func (c *TCPClient) Read() (data []byte, timeout bool, err error)

Read will read data into tcp client from TCP Server host, Read will block until read deadlined timeout, then loop continues to read again

func (*TCPClient) StartReader

func (c *TCPClient) StartReader() error

StartReader will start the tcp client reader service, it will continuously read data from tcp server

func (*TCPClient) StopReader

func (c *TCPClient) StopReader()

StopReader will end the tcp client reader service

func (*TCPClient) Write

func (c *TCPClient) Write(data []byte) error

Write will write data into tcp connection stream, and deliver to the TCP Server host currently connected

type TCPServer

type TCPServer struct {
	Port uint

	ListenerAcceptHandler func(clientIP string)
	ListenerErrorHandler  func(err error)

	ClientReceiveHandler func(clientIP string, data []byte, writeToClientFunc func(writeData []byte, clientIP string) error)
	ClientErrorHandler   func(clientIP string, err error)

	ReadBufferSize        uint
	ListenerYieldDuration time.Duration
	ReaderYieldDuration   time.Duration

	ReadDeadlineDuration  time.Duration
	WriteDeadLineDuration time.Duration
	// contains filtered or unexported fields
}

TCPServer defines a concurrent tcp server for handling inbound client requests, and sending back responses

Port = this tcp server port number to listen on ListenerErrorHandler = func to trigger when listener caused error event, this also signifies the end of tcp server listener serving mode ClientReceiveHandler = func to trigger when this tcp server receives data from client, the writeToClientFunc is provided for sending data back to the connected client ClientErrorHandler = func to trigger when this tcp server detected tcp client error during data receive event ReadBufferSize = default 1024, cannot be greater than 65535, defines the byte length of read buffer ListenerYieldDuration = default 0, no yield, valid range is 0 - 250ms, the amount of time to yield to cpu during listener accept loop cycle ReaderYieldDuration = default 25ms, the amount of time to yield to cpu process during each cycle of Read loop ReadDeadLineDuration = default 1000ms, the amount of time to wait for read action before timeout, valid range is 250ms - 5000ms WriteDeadLineDuration = the amount of time to wait for write action before timeout, if 0, then no timeout

func (*TCPServer) Close

func (s *TCPServer) Close()

Close will shut down TCP Server, and clean up resources allocated

func (*TCPServer) DisconnectClient

func (s *TCPServer) DisconnectClient(clientIP string)

DisconnectClient will close client connection and end the client reader service

func (*TCPServer) GetConnectedClients

func (s *TCPServer) GetConnectedClients() (clientsList []string)

GetConnectedClients returns list of connected tcp clients

func (*TCPServer) Serve

func (s *TCPServer) Serve() (err error)

Serve will startup TCP Server and begin accepting TCP Client connections, and initiates connection processing

func (*TCPServer) WriteToClient

func (s *TCPServer) WriteToClient(writeData []byte, clientIP string) error

WriteToClient accepts byte slice and clientIP target, for writing data to the target client

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL