core

module
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2020 License: GPL-3.0

README

core

core for go services

Release Go Report Card

tcp epoll server


import (
	"fmt"
	"io"

	"github.com/deweppro/core/pkg/server"
	tcpepoll "github.com/deweppro/core/pkg/server/tcp-epoll"
	"github.com/deweppro/go-app/pkg/app"
)

func main() {
	app.New("config.yaml").Modules(newtest).Run()
}

type test struct {
	s *tcpepoll.Server
}

func newtest() *test {
	return &test{
		s: tcpepoll.New(),
	}
}

func (t *test) Up() error {
	addr, err := server.RandomPort("0.0.0.0")
	if err != nil {
		return err
	}

	t.s.SetAddr(addr)
	t.s.SetHandler(t.handler)

	return t.s.Up()
}

func (t *test) Down() error {
	return t.s.Down()
}

func (t *test) handler(r []byte, w io.Writer) error {
	fmt.Println(string(r))

	if _, err := w.Write([]byte("Bye bye!")); err != nil {
		// close connection for all errors with write to log
		return err
	}

	return io.EOF
}

Jump to

Keyboard shortcuts

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