go-tcpserver: github.com/orkunkaraduman/go-tcpserver Index | Files

package tcpserver

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

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

Index

Package Files

handler.go tcpserver.go textprotocol.go utils.go

Variables

var DefMaxLineSize = 1 * 1024

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

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

func ReadBytesLimit Uses

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

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

type Handler Uses

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

A Handler responds to an TCP incoming connection.

type HandlerFunc Uses

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 Uses

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

Serve calls f(conn, closeCh)

type TCPServer Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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

Serve implements Handler.Serve.

type TextProtocolContext Uses

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 Uses

func (ctx *TextProtocolContext) Close()

Close closes context.

func (*TextProtocolContext) WriteData Uses

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

WriteData writes data to connection.

func (*TextProtocolContext) WriteLine Uses

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

WriteLine writes a line to connection.

Package tcpserver imports 11 packages (graph). Updated 2019-01-13. Refresh now. Tools for package owners.