BSD 3-Clause "New" or "Revised" License

项目介绍

cheng-zhongliang/event 是一个托管在 GitHub 上的开源项目,主要使用 Go 编写,采用 BSD 3-Clause "New" or "Revised" License 许可证,目前获得 119 个 Star、5 次 Fork。

仓库全名
cheng-zhongliang/event
开源许可证
BSD 3-Clause "New" or "Revised" License
Star 数
119
Fork 数
5

README 摘要

src="logo.png" width="350" height="175" border="0" alt="event"> event is a network I/O event notification library for Go. It uses epoll and kqueue to poll I/O events that is fast and low memory usage. It works in a similar manner as libevent. The goal of event is to provide a BASIC tool for building high performance network applications. Note: All development is done on a Raspberry Pi 4B. Features Supports…

完整内容见 GitHub 原始 README 或本页 。

常见问题

event 用什么语言开发?

主要使用 Go 编写,采用 BSD 3-Clause "New" or "Revised" License 许可证,目前有 119 个 Star。

event 可以免费商用吗?

仓库采用 BSD 3-Clause "New" or "Revised" License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 event 的源码?

可以直接克隆仓库:git clone https://github.com/cheng-zhongliang/event.git,默认分支为 master;安装与运行步骤见仓库 README。

event 最近还在维护吗?

仓库最近一次更新为 2024年12月27日,当前没有未关闭的 Issue;Star 数为 119,可作为社区活跃度的参考。

关于

Simple I/O event notification library wirtten in Golang

描述取自 GitHub 官方 API,中文内容由 GitRead 中文社区 编辑团队整理。

简要分析

热度分
133
价值分
73
活跃状态
活跃

README

event

Stars

event is a network I/O event notification library for Go. It uses epoll and kqueue to poll I/O events that is fast and low memory usage. It works in a similar manner as libevent.

The goal of event is to provide a BASIC tool for building high performance network applications.

Note: All development is done on a Raspberry Pi 4B.

Features

  • Supports Read/Write/Timeout events
  • Flexible timer event and ticker event
  • Supports event priority
  • Simple API
  • Low memory usage

Getting Started

Installing

To start using event, just run go get:

$ go get -u github.com/cheng-zhongliang/event

Event

  • EvRead fires when the fd is readable.
  • EvWrite fires when the fd is writable.
  • EvTimeout fires when the timeout expires.

When the event is triggered, the callback function will be called.

Read/Write/Timeout

These events can be used in combination.

base := event.NewBase()
ev := event.New(base, fd, event.EvRead|event.Timeout, callback, arg)
ev.Attach(time.Second)

When the fd is readable or timeout expires, this event will be triggered.

Option

The event is one-shot by default. If you want to persist, you can set the EvPersist option.

ev := event.New(base, fd, event.EvRead|event.EvPersist, callback, arg)

Timer

The timer is a one-shot event that will be triggered after the timeout expires.

base := event.NewBase()
ev := event.NewTimer(base, callback, arg)
ev.Attach(time.Second)

Ticker

The ticker is a repeating event that will be triggered every time the timeout expires.

base := event.NewBase()
ev := event.NewTicker(base, callback, arg)
ev.Attach(time.Second)

Priority

When events are triggered together, high priority events will be dispatched first.

ev := event.New(base, fd, event.EvRead, callback, arg)
ev.SetPriority(event.HP)

Usage

Example echo server that binds to port 1246:

package main

import (
	"syscall"

	"github.com/cheng-zhongliang/event"
)

func main() {
	base, err := event.NewBase()
	if err != nil {
		panic(err)
	}
	fd := socket()
	ev := event.New(base, fd, event.EvRead|event.EvPersist, accept, base)
	if err := ev.Attach(0); err != nil {
		panic(err)
	}
	if err := base.Dispatch(); err != nil && err != syscall.EBADF {
		panic(err)
	}
	syscall.Close(fd)
}

func socket() int {
	addr := syscall.SockaddrInet4{Port: 1246, Addr: [4]byte{0, 0, 0, 0}}
	fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
	if err != nil {
		panic(err)
	}
	if err := syscall.Bind(fd, &addr); err != nil {
		panic(err)
	}
	err = syscall.Listen(fd, syscall.SOMAXCONN)
	if err != nil {
		panic(err)
	}
	return fd
}

func accept(fd int, events uint32, arg interface{}) {
	base := arg.(*event.EventBase)
	clientFd, _, err := syscall.Accept(fd)
	if err != nil {
		panic(err)
	}
	ev := event.New(base, clientFd, event.EvRead|event.EvPersist, echo, nil)
	if err := ev.Attach(0); err != nil {
		panic(err)
	}
}

func echo(fd int, events uint32, arg interface{}) {
	buf := make([]byte, 0xFFF)
	n, err := syscall.Read(fd, buf)
	if err != nil {
		panic(err)
	}
	if _, err := syscall.Write(fd, buf[:n]); err != nil {
		panic(err)
	}
}

Connect to the echo server:

$ telnet localhost 1246

评论

暂无评论