Skip to content
a net framework
Go
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example dev: modify api Mar 13, 2018
protocol/redis fix: redis protocol Jan 16, 2019
.gitignore dev: add redis protocol support Jun 21, 2017
LICENSE add example Jun 18, 2016
README.md update readme and example Jun 18, 2016
buf.go
codec.go
codec_length_field.go dev: add maxBodySize and recover read loop Oct 8, 2018
codec_raw.go dev: add middleware and sync middleware Mar 15, 2018
codec_string.go
codec_sync.go dev: add timeout for sync and fix readtimeout bug Mar 16, 2018
conn_pool.go
connector.go
conv.go just test Nov 1, 2017
hack.go
help_test.go dev: add uint16 method for bytebuf Jan 14, 2019
id.go just test Nov 1, 2017
mem_pool.go just test Nov 1, 2017
mem_pool_test.go
middleware.go dev: add timeout for sync and fix readtimeout bug Mar 16, 2018
middleware_sync.go fix: sync protocol not syncing if loss net packket Apr 11, 2018
middleware_sync_test.go dev: add timeout for sync and fix readtimeout bug Mar 16, 2018
option.go dev: add middleware and sync middleware Mar 15, 2018
pool.go just test Nov 1, 2017
queue.go dev: add middleware and sync middleware Mar 15, 2018
server.go dev: add statck info if read panic Mar 29, 2019
server_test.go dev: add middleware and sync middleware Mar 15, 2018
session.go fix: read timeout bug Apr 24, 2019
sync_queue.go dev: add middleware and sync middleware Mar 15, 2018
timewheel.go just test Nov 1, 2017
timewheel_test.go dev: change timer wheel implemention Jul 5, 2017
uuid.go dev: add mem pool Sep 8, 2017

README.md

goetty

Goetty is a framework to help you build socket application.

Example

codec

package example

import (
    "github.com/fagongzi/goetty"
)

type StringDecoder struct {
}

func (decoder StringDecoder) Decode(in *goetty.ByteBuf) (bool, interface{}, error) {
    _, data, err := in.ReadMarkedBytes()

    if err != nil {
        return true, "", err
    }

    return true, string(data), nil
}

type StringEncoder struct {
}

func (self StringEncoder) Encode(data interface{}, out *goetty.ByteBuf) error {
    msg, _ := data.(string)
    bytes := []byte(msg)
    out.WriteInt(len(bytes))
    out.Write(bytes)
    return nil
}

server

package example

import (
    "fmt"
    "github.com/fagongzi/goetty"
)

type EchoServer struct {
    addr   string
    server *goetty.Server
}

func NewEchoServer(addr string) *EchoServer {
    return &EchoServer{
        addr:   addr,
        server: goetty.NewServer(addr, goetty.NewIntLengthFieldBasedDecoder(&StringDecoder{}), &StringEncoder{}, goetty.NewInt64IdGenerator()),
    }
}

func (self *EchoServer) Serve() error {
    return self.server.Serve(self.doConnection)
}

func (self *EchoServer) doConnection(session goetty.IOSession) error {
    defer session.Close() // close the connection

    fmt.Printf("A new connection from <%s>", session.RemoteAddr())

    // start loop for read msg from this connection
    for {
        msg, err := session.Read() // if you want set a read deadline, you can use 'session.ReadTimeout(timeout)'
        if err != nil {
            return err
        }

        fmt.Printf("receive a msg<%s> from <%s>", msg, session.RemoteAddr())

        // echo msg back
        session.Write(msg)
    }

    return nil
}

client

package example

import (
    "fmt"
    "github.com/fagongzi/goetty"
    "time"
)

type EchoClient struct {
    serverAddr string
    conn       *goetty.Connector
}

func NewEchoClient(serverAddr string) (*EchoClient, error) {
    cnf := &goetty.Conf{
        Addr: serverAddr,
        TimeoutConnectToServer: time.Second * 3,
    }

    c := &EchoClient{
        serverAddr: serverAddr,
        conn:       goetty.NewConnector(cnf, goetty.NewIntLengthFieldBasedDecoder(&StringDecoder{}), &StringEncoder{}),
    }

    // if you want to send heartbeat to server, you can set conf as below, otherwise not set

    // create a timewheel to calc timeout
    tw := goetty.NewHashedTimeWheel(time.Second, 60, 3)
    tw.Start()

    cnf.TimeoutWrite = time.Second * 3
    cnf.TimeWheel = tw
    cnf.WriteTimeoutFn = c.writeHeartbeat

    _, err := c.conn.Connect()

    return c, err
}

func (self *EchoClient) writeHeartbeat(serverAddr string, conn *goetty.Connector) {
    self.SendMsg("this is a heartbeat msg")
}

func (self *EchoClient) SendMsg(msg string) error {
    return self.conn.Write(msg)
}

func (self *EchoClient) ReadLoop() error {
    // start loop to read msg from server
    for {
        msg, err := self.conn.Read() // if you want set a read deadline, you can use 'connector.ReadTimeout(timeout)'
        if err != nil {
            fmt.Printf("read msg from server<%s> failure", self.serverAddr)
            return err
        }

        fmt.Printf("receive a msg<%s> from <%s>", msg, self.serverAddr)
    }

    return nil
}
You can’t perform that action at this time.