tcpserver

package module
v0.0.0-...-690f39b Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2024 License: MIT Imports: 8 Imported by: 0

README

TCP Server Abstraction Library

Overview

This project is a simple TCP Server Abstraction Library created to provide a straightforward and customizable way to create TCP servers in Golang. It abstracts away the complex parts of managing TCP servers and provides a clear interface that software developers can use easily. Moreover, it efficiently limits the number of goroutines in use, ensuring optimum performance.

Features

  • Concurrent management of multiple TCP connections
  • Customizable TCP connection handlers
  • Limiting the number of goroutines

Installing the Library

To install this library you can use the well-known go get to download it to your GOPATH:

go get github.com/jtarchie/tcpserver

Usage

The library consists of several functionalities. Here are some starting points:

  1. Creating a new server:
ctx := context.TODO()
server, err := tcp.NewServer(ctx, 8080, 1) // listen on port 8080, with 1 limited goroutine
if err != nil {
    log.Fatalf("error starting server: %v", err)
}
  1. Handling incoming connections with a custom handler:
type CustomHandler struct{}
func (*CustomHandler) OnConnection(_ context.Context, rw io.ReadWriter) error {
    _, err := rw.Write([]byte("Hello"))
    return err
}
server.Listen(ctx, &CustomHandler{})
  1. Closing connections:
err := server.Close()
if err != nil {
    log.Print("error closing server: $v", err)
}
  1. Client operations:

This was build to have an easy way to test clients.

response, err := tcp.Write(8080, "message")
if err != nil {
	log.Fatalf("error writing to server: %v", err)
}
fmt.Printf("response from server: %s\n", response)

Contributing

If you wish to contribute to this project, create an issue to discuss.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Write

func Write(port int, message string) (string, error)

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(port int) (*Client, error)

func (*Client) Close

func (c *Client) Close() error

func (*Client) ReadlineString

func (c *Client) ReadlineString() (string, error)

func (*Client) WriteString

func (c *Client) WriteString(message string) error

type Handler

type Handler interface {
	OnConnection(context.Context, io.ReadWriter) error
}

type Server

type Server struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer(
	ctx context.Context,
	port uint,
	poolSize uint,
) (*Server, error)

func (*Server) Close

func (s *Server) Close() error

func (*Server) Listen

func (s *Server) Listen(ctx context.Context, handler Handler) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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