tcpserver

package module
v0.0.0-...-a897b4e Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2018 License: BSD-3-Clause Imports: 11 Imported by: 9

README

DISCONTINUED

Please visit: github.com/orkunkaraduman/go-accepter

Go TCP Server library

GoDoc

Go TCP Server library provides tcpserver package implements TCP server.

Go TCP Server library has TCPServer struct seems like Server struct of http package to create TCP servers. Also offers TextProtocol struct as a Handler to implement text based protocols.

Examples

For more examples, examples/

examples/go-tcpserver-echoline
package main

import (
	"fmt"
	"log"

	"github.com/orkunkaraduman/go-tcpserver"
)

func main() {
	prt := &tcpserver.TextProtocol{
		OnAccept: func(ctx *tcpserver.TextProtocolContext) {
			ctx.WriteLine("WELCOME")
		},
		OnQuit: func(ctx *tcpserver.TextProtocolContext) {
			ctx.WriteLine("QUIT")
		},
		OnReadLine: func(ctx *tcpserver.TextProtocolContext, line string) int {
			fmt.Println(line)
			if line == "QUIT" {
				ctx.Close()
				return 0
			}
			ctx.WriteLine(line)
			return 0
		},
		OnReadData: func(ctx *tcpserver.TextProtocolContext, data []byte) {

		},
	}
	srv := &tcpserver.TCPServer{
		Addr:    ":1234",
		Handler: prt,
	}
	log.Fatal(srv.ListenAndServe())
}

Documentation

Overview

Package tcpserver is discontinued. Please visit: github.com/orkunkaraduman/go-accepter Package tcpserver provides TCP server implementation.

Index

Constants

This section is empty.

Variables

View Source
var DefMaxLineSize = 1 * 1024

DefMaxLineSize specifies maximum line size with delimiter if TextProtocol.MaxLineSize is 0.

View Source
var (
	// ErrBufferLimitExceeded is returned when specified buffer limit
	// was exceeded.
	ErrBufferLimitExceeded = errors.New("buffer limit exceeded")
)

Functions

func ReadBytesLimit

func ReadBytesLimit(b *bufio.Reader, delim byte, lim int) (line []byte, err error)

ReadBytesLimit reads bytes as bufio.Reader.ReadBytes with limit.

Types

type Handler

type Handler interface {
	Serve(conn net.Conn, closeCh <-chan struct{})
}

A Handler responds to an TCP incoming connection.

type HandlerFunc

type HandlerFunc func(conn net.Conn, closeCh <-chan struct{})

The HandlerFunc type is an adapter to allow the use of ordinary functions as handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler that calls f.

func (HandlerFunc) Serve

func (f HandlerFunc) Serve(conn net.Conn, closeCh <-chan struct{})

Serve calls f(conn, closeCh)

type TCPServer

type TCPServer struct {
	// TCP address to listen on.
	Addr string

	// Handler to invoke.
	Handler Handler

	// TLSConfig optionally provides a TLS configuration.
	TLSConfig *tls.Config

	// ErrorLog specifies an optional logger for errors in Handler.
	ErrorLog *log.Logger
	// contains filtered or unexported fields
}

A TCPServer defines parameters for running an TCP server.

func (*TCPServer) Close

func (srv *TCPServer) Close() (err error)

Close immediately closes all active net.Listeners and any connections. For a graceful shutdown, use Shutdown.

Close returns any error returned from closing the Server's underlying Listener(s).

func (*TCPServer) ListenAndServe

func (srv *TCPServer) ListenAndServe() error

ListenAndServe listens on the TCP network address srv.Addr and then calls Serve to handle requests on incoming connections. ListenAndServe returns a nil error after Close or Shutdown method called.

func (*TCPServer) ListenAndServeTLS

func (srv *TCPServer) ListenAndServeTLS(certFile, keyFile string) error

ListenAndServeTLS listens on the TCP network address srv.Addr and then calls Serve to handle requests on incoming TLS connections.

Filenames containing a certificate and matching private key for the server must be provided if neither the Server's TLSConfig.Certificates nor TLSConfig.GetCertificate are populated. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate.

func (*TCPServer) Serve

func (srv *TCPServer) Serve(l net.Listener) (err error)

Serve accepts incoming connections on the Listener l, creating a new service goroutine for each. The service goroutines read requests and then call srv.Handler to reply to them. Serve returns a nil error after Close or Shutdown method called.

func (*TCPServer) ServeTLS

func (srv *TCPServer) ServeTLS(l net.Listener, certFile, keyFile string) (err error)

ServeTLS accepts incoming connections on the Listener l, creating a new service goroutine for each. The service goroutines read requests and then call srv.Handler to reply to them. ServeTLS returns a nil error after Close or Shutdown method called.

Additionally, files containing a certificate and matching private key for the server must be provided if neither the Server's TLSConfig.Certificates nor TLSConfig.GetCertificate are populated.. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate.

func (*TCPServer) Shutdown

func (srv *TCPServer) Shutdown(ctx context.Context) (err error)

Shutdown gracefully shuts down the server without interrupting any connections. Shutdown works by first closing all open listeners, then fills closeCh on Serve method of Handler, and then waiting indefinitely for connections to exit Serve method of Handler and then close. If the provided context expires before the shutdown is complete, Shutdown returns the context's error, otherwise it returns any error returned from closing the Server's underlying Listener(s).

When Shutdown is called, Serve, ListenAndServe, and ListenAndServeTLS immediately return nil. Make sure the program doesn't exit and waits instead for Shutdown to return.

type TextProtocol

type TextProtocol struct {
	// Accept callback. It will be called before reading line.
	OnAccept func(ctx *TextProtocolContext)

	// Quit callback. It will be called before closing.
	OnQuit func(ctx *TextProtocolContext)

	// ReadLine callback. If it returns greater then 0, context reads data from
	// connection n bytes. And after will be call OnReadData.
	OnReadLine func(ctx *TextProtocolContext, line string) (n int)

	// ReadData callback.
	OnReadData func(ctx *TextProtocolContext, buf []byte)

	// MaxLineSize specifies maximum line size with delimiter.
	MaxLineSize int

	// User data to use free.
	UserData interface{}
}

TextProtocol defines parameters for Handler of text based protocol.

func (*TextProtocol) Serve

func (prt *TextProtocol) Serve(conn net.Conn, closeCh <-chan struct{})

Serve implements Handler.Serve.

type TextProtocolContext

type TextProtocolContext struct {
	// Pointer of TextProtocol struct handled by this context.
	Prt *TextProtocol

	// Connection handled by this context.
	Conn net.Conn

	// User data to use free.
	UserData interface{}
	// contains filtered or unexported fields
}

TextProtocolContext defines parameters for text protocol context.

func (*TextProtocolContext) Close

func (ctx *TextProtocolContext) Close()

Close closes context.

func (*TextProtocolContext) WriteData

func (ctx *TextProtocolContext) WriteData(buf []byte) error

WriteData writes data to connection.

func (*TextProtocolContext) WriteLine

func (ctx *TextProtocolContext) WriteLine(line string) error

WriteLine writes a line to connection.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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