evnet

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2022 License: Apache-2.0 Imports: 9 Imported by: 0

README

⚡ Introduction

Evnet is event-driven net framework with high performance deriving from gnet and evio. It supports epoll syscall in Linux only now.

✨ Features

  • Supporting an event-driven mechanism: epoll on Linux.
  • Supporting multiple protocols/IPC mechanism: TCP, UDP and Unix Domain Socket.
  • Efficient, reusable and elastic memory buffer and zero copy.
  • Supporting multiple load-balancing algorithms: Round-Robin, Source-Addr-Hash and Least-Connections

🎬 Getting started

go get github.com/bruce2233/evnet
type MyHandler struct {
	BuiltinEventHandler
}

func (mh MyHandler) OnConn(c Conn) {
	p, _ := c.Next(-1)
	println(string(p))
}

func (mh MyHandler) OnClose(c Conn) {
	println("On Close Trigger")
}

//main.go
func TestMainRec(t *testing.T) {
	readHandler := MyHandler{BuiltinEventHandler{}}
	Run(readHandler, "tcp://127.0.0.1:9000")
}

Documentation

Overview

Copyright (c) 2022 Bruce Zhang

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	ErrEvents = unix.EPOLLERR | unix.EPOLLHUP | unix.EPOLLRDHUP
	// OutEvents combines EPOLLOUT event and some exceptional events.
	OutEvents = ErrEvents | unix.EPOLLOUT
	// InEvents combines EPOLLIN/EPOLLPRI events and some exceptional events.
	InEvents = ErrEvents | unix.EPOLLIN | unix.EPOLLPRI
)

Variables

View Source
var SubReactorsNum = 2

Functions

func AcceptSocket

func AcceptSocket(fd int) (int, net.Addr, error)

func Run

func Run(eventHandler EventHandler, protoAddr string, optList ...Option)

Types

type BuiltinEventHandler

type BuiltinEventHandler struct {
}

func (BuiltinEventHandler) OnClose

func (builtinEventHandler BuiltinEventHandler) OnClose(c Conn)

func (BuiltinEventHandler) OnConn

func (builtinEventHandler BuiltinEventHandler) OnConn(c Conn)

type Conn

type Conn interface {
	//working
	io.Reader
	io.Writer
	Socket

	Next(n int) ([]byte, error)

	// LocalAddr is the connection's local socket address.
	LocalAddr() (addr net.Addr)

	// RemoteAddr is the connection's remote peer address.
	RemoteAddr() (addr net.Addr)
}

func NewConn

func NewConn(fd int, la net.Addr, ra net.Addr) (Conn, error)

type EventHandler

type EventHandler interface {
	//working
	OnConn(c Conn)
	OnClose(c Conn)
}

type Listener

type Listener struct {
	Fd int
	// contains filtered or unexported fields
}

type MainReactor

type MainReactor struct {
	// contains filtered or unexported fields
}

func (*MainReactor) Init

func (mr *MainReactor) Init(proto, addr string) error

Create poller and listener for mainReactor

func (*MainReactor) Listen

func (mainReactor *MainReactor) Listen(proto, addr string) (fd int, netAddr net.Addr, err error)

The proto paramater MUST be "tcp" The addr parameter

func (*MainReactor) Loop

func (mainReactor *MainReactor) Loop()

func (*MainReactor) SetEventHandler

func (mr *MainReactor) SetEventHandler(eh EventHandler)

type Option

type Option func(opts *Options)

func WithLogPath

func WithLogPath(logPath string) Option

type Options

type Options struct {
	LogPath string
}

type PollAttachment

type PollAttachment struct {
	Fd    int
	Event unix.EpollEvent
}

type Poller

type Poller struct {
	Fd int
}

func OpenPoller

func OpenPoller() (poller *Poller, err error)

create a new poller

func (*Poller) AddPollRead

func (poller *Poller) AddPollRead(pafd int) error

func (*Poller) Delete

func (p *Poller) Delete(fd int) error

Delete removes the given file-descriptor from the poller.

func (*Poller) Polling

func (poller *Poller) Polling() []unix.EpollEvent

block

type SubReactor

type SubReactor struct {
	// contains filtered or unexported fields
}

func (*SubReactor) Loop

func (sr *SubReactor) Loop()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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