goetty

package module
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2022 License: Apache-2.0 Imports: 17 Imported by: 122

README

goetty

Goetty is a framework to help you build socket application.

Example

server

package example

import (
	"log"

	"github.com/fagongzi/goetty"
	"github.com/fagongzi/goetty/codec/simple"
)

// EchoServer echo server
type EchoServer struct {
	addr string
	app  goetty.NetApplication
}

// NewEchoServer create new server
func NewEchoServer(addr string) *EchoServer {
	svr := &EchoServer{}
	encoder, decoder := simple.NewStringCodec()
	app, err := goetty.NewTCPApplication(addr, svr.handle,
		goetty.WithAppSessionOptions(goetty.WithCodec(encoder, decoder)))
	if err != nil {
		log.Panicf("start server failed with %+v", err)
	}

	return &EchoServer{
		addr: addr,
		app:  app,
	}
}

// Start start
func (s *EchoServer) Start() error {
	return s.Start()
}

// Stop stop
func (s *EchoServer) Stop() error {
	return s.Stop()
}

func (s *EchoServer) handle(session goetty.IOSession, msg interface{}, received uint64) error {
	log.Printf("received %+v from %s, already received %d msgs",
		msg,
		session.RemoteAddr(),
		received)
	return session.WriteAndFlush(msg)
}

client

package example

import (
	"log"
	"time"

	"github.com/fagongzi/goetty"
	"github.com/fagongzi/goetty/codec/simple"
)

// EchoClient echo client
type EchoClient struct {
	serverAddr string
	conn       goetty.IOSession
}

// NewEchoClient new client
func NewEchoClient(serverAddr string) (*EchoClient, error) {
	c := &EchoClient{
		serverAddr: serverAddr,
	}

	encoder, decoder := simple.NewStringCodec()
	c.conn = goetty.NewIOSession(goetty.WithCodec(encoder, decoder))
	_, err := c.conn.Connect(serverAddr, time.Second*3)
	return c, err
}

// SendMsg send msg to server
func (c *EchoClient) SendMsg(msg string) error {
	return c.conn.WriteAndFlush(msg)
}

// ReadLoop read loop
func (c *EchoClient) ReadLoop() error {
	// start loop to read msg from server
	for {
		msg, err := c.conn.Read() // if you want set a read deadline, you can use 'WithTimeout option'
		if err != nil {
			log.Printf("read failed with %+v", err)
			return err
		}

		log.Printf("received %+v", msg)
	}
}

Documentation

Index

Constants

View Source
const (
	// DefaultSessionBucketSize default bucket size of session map
	DefaultSessionBucketSize = uint64(64)
	// DefaultReadBuf read buf size
	DefaultReadBuf = 256
	// DefaultWriteBuf write buf size
	DefaultWriteBuf = 256
)

Variables

View Source
var (
	// ErrIllegalState illegal state error
	ErrIllegalState = errors.New("illegal state")
	// ErrDisableConnect disable to connect
	ErrDisableConnect = errors.New("io session is disable to connect")
)

Functions

This section is empty.

Types

type AppOption added in v1.8.0

type AppOption func(*appOptions)

AppOption application option

func WithAppErrorMsgFactory added in v1.8.0

func WithAppErrorMsgFactory(value func(IOSession, interface{}, error) interface{}) AppOption

WithAppErrorMsgFactory set function to process error, closed the client session if this field not set

func WithAppLogger added in v1.11.2

func WithAppLogger(logger *zap.Logger) AppOption

WithAppLogger set logger for application

func WithAppSessionAware added in v1.10.0

func WithAppSessionAware(aware IOSessionAware) AppOption

WithAppSessionBucketSize set the app session aware

func WithAppSessionBucketSize added in v1.8.0

func WithAppSessionBucketSize(value uint64) AppOption

WithAppSessionBucketSize set the number of maps to store session

func WithAppSessionOptions added in v1.8.0

func WithAppSessionOptions(value ...Option) AppOption

WithAppSessionOptions set the number of maps to store session

type IOSession

type IOSession interface {
	// ID sessino id
	ID() uint64
	// Connect connect to address, only used at client-side
	Connect(addr string, timeout time.Duration) (bool, error)
	// Close close the session
	Close() error
	// Connected returns true if connection is ok
	Connected() bool
	// Read read packet from connection
	Read() (interface{}, error)
	// Write write packet to connection out buffer
	Write(msg interface{}) error
	// WriteAndFlush write packet to connection out buffer and flush the out buffer
	WriteAndFlush(msg interface{}) error
	// Flush flush the out buffer
	Flush() error
	// InBuf connection read buffer
	InBuf() *buf.ByteBuf
	// OutBuf connection out buffer
	OutBuf() *buf.ByteBuf
	// SetAttr set attr
	SetAttr(key string, value interface{})
	// GetAttr read attr
	GetAttr(key string) interface{}
	// RemoteAddr returns remote address, include ip and port
	RemoteAddr() string
	// RemoteIP returns remote address, only ip
	RemoteIP() string
	// RawConn returns the raw connection
	RawConn() (net.Conn, error)
}

IOSession session

func NewIOSession added in v1.8.0

func NewIOSession(opts ...Option) IOSession

NewIOSession create a new io session

type IOSessionAware added in v1.10.0

type IOSessionAware interface {
	// Created session created
	Created(IOSession)
	//Closed session closed
	Closed(IOSession)
}

IOSessionAware io session aware

type NetApplication added in v1.8.0

type NetApplication interface {
	// Start start the transport server
	Start() error
	// Stop stop the transport server
	Stop() error
	// GetSession get session
	GetSession(uint64) (IOSession, error)
	// Broadcast broadcast msg to all sessions
	Broadcast(msg interface{}) error
}

NetApplication is a network based application

func NewApplication added in v1.8.0

func NewApplication(address string, handleFunc func(IOSession, interface{}, uint64) error, opts ...AppOption) (NetApplication, error)

NewApplication returns a application

func NewApplicationWithListener added in v1.14.0

func NewApplicationWithListener(listener net.Listener, handleFunc func(IOSession, interface{}, uint64) error, opts ...AppOption) (NetApplication, error)

NewApplicationWithListener returns a net application with listener

type Option added in v1.0.0

type Option func(*options)

Option transport option

func WithBufSize added in v1.8.0

func WithBufSize(read, write int) Option

WithBufSize set read/write buf size

func WithCodec added in v1.8.0

func WithCodec(encoder codec.Encoder, decoder codec.Decoder) Option

WithCodec set codec

func WithConnOptionFunc added in v1.8.0

func WithConnOptionFunc(connOptionFunc func(net.Conn)) Option

WithConnOptionFunc set conn options func

func WithEnableAsyncWrite added in v1.8.0

func WithEnableAsyncWrite(asyncFlushBatch int64) Option

WithEnableAsyncWrite enable async write

func WithLogger added in v1.8.0

func WithLogger(value *zap.Logger) Option

WithLogger set logger

func WithReleaseMsgFunc added in v1.8.0

func WithReleaseMsgFunc(value func(interface{})) Option

WithReleaseMsgFunc set the number of maps to store session

func WithTimeout added in v1.8.0

func WithTimeout(read, write time.Duration) Option

WithTimeout set read/write timeout

Directories

Path Synopsis
raw

Jump to

Keyboard shortcuts

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