pi_pi_net

package module
v0.0.0-...-54e2ccf Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

README

pi-pi-net 👋

🎉

pi-pi-net 是一个在linux环境下封装epoll的网络库,可以基于此库非常方便的实现Reactor网络模型,或者web,rpc,websocket等网络框架的基础框架

详细文档点这里

Develop detailed documentation

服务端使用

👊 服务端使用有两种方式,方式一

首先创建context :  ctx := pi_pi_net.NewContext()
然后直接读取  :  go readBlock(ctx)
最后运行即可:   ctx.Run("tcp","127.0.0.1:10000")


完整代码如下

func readBlock(ctx *pi_pi_net.Context){
    i := 0
    for messageServer := range ctx.ReadBlock() {
        fmt.Println("[read data]",messageServer.ReadString())
        messageServer.WriteString(fmt.Sprintf("hello client%v",i))
        i ++
        messageServer.Close()
    }
}

func main(){
    ctx := pi_pi_net.NewContext()
    fmt.Println("run")
    go readBlock(ctx)
    if err := ctx.Run("tcp","127.0.0.1:10000");err != nil {
        fmt.Println("run err",err)
        return
    }
}

👊 服务端使用有两种方式,方式二


1 首先监听地址  ctx,err := pi_pi_net.NewContext().Listen("tcp","127.0.0.1:10000")
2 异步读取  :  go read(ctx)
3 获取连接然后运行: ctx.Accept()

func main(){
	ctx,err := pi_pi_net.NewContext().Listen("tcp","127.0.0.1:10000")
	if err != nil {
		fmt.Println("[listen err]",err)
		return
	}
	go read(ctx)
	fmt.Println("run listen")
	if err := ctx.Accept();err != nil {
		fmt.Println("[accept err]",err)
	}
}

func read(ctx *pi_pi_net.Context){
	obj := ctx.ReadServerConnect()
	fmt.Println("[read data]",obj.ReadString())
	if n,err := obj.WriteString("hello client");err != nil {
		fmt.Println("write err",err)
	}else {
		fmt.Println("write n is ",n)
	}
	obj.Close()
}

👊 客户端使用

1 客户端连接服务端 	cli,err := Dail("tcp","127.0.0.1:10000")
2 读取数据 data,err := cli.ReadString()
3 写入数据  cli.WriteString("hello world")


	cli,err := Dail("tcp","127.0.0.1:10000")
	if err != nil {
		t.Error("dail err",err)
		return
	}
	_,err = cli.WriteString("hello world")
	if err != nil {
		t.Error("cli writeString err",err)
		return
	}
	//t.Log("write len is ",n)
	data,err := cli.ReadString()
	if err != nil {
		t.Error("read err",err)
		return
	}
	t.Log("data is",data)

👊 客户端可以api

Read()   :读取byte数组数据,阻塞
ReadString()  : 读取字符串,阻塞
Write(data []byte)  :写入数据
WriteString(data string) :写入数据
Close()   :关闭连接

🔨 服务端api

ReadServerConnect()  :单次读取
ReadBlock()          :阻塞读取,返回chan
ReadByte()           :按照byte数组读取
ReadString()         :按照字符串读取
Write(data []byte)    :按照byte数组写入
WriteString(data string)  : 按照字符串写入
WriteByteAndClose(data []byte)  : 写入byte数组之后关闭连接
WriteStringAndClose(data string) :写入字符串后关闭连接
Close()  :关闭连接

🔋 PR

我当然希望你能够有更好的想法或贡献。

🚥 我可能需要更多的灵感和建议、如果您有更好的想法,请联系我 🙌🙌

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByteToString

func ByteToString(bytes []byte) string

func ReadFull

func ReadFull(l int, buf []byte) (n int, err error)

func StringToByte

func StringToByte(s string) []byte

Types

type Client

type Client struct {
	Conn net.Conn
	Addr string
	Data chan struct {
		Body []byte
		Err  error
	}
	// contains filtered or unexported fields
}

func Dail

func Dail(network, addr string) (*Client, error)

func (*Client) Close

func (c *Client) Close() error

func (*Client) Read

func (c *Client) Read() (body []byte, err error)

客户端读取 返回值为[]byte client read and return []byte

func (*Client) ReadString

func (c *Client) ReadString() (body string, err error)

客户端读取 返回值为string client read and return string

func (*Client) Write

func (c *Client) Write(data []byte) (n int, err error)

客户端写入 client write

func (*Client) WriteString

func (c *Client) WriteString(data string) (n int, err error)

type Context

type Context struct {
	Body chan *ServerConnect

	El          *EventList
	Epoll       int
	SetNonblock bool

	ConnList map[int]struct{}
	Lock     *sync.RWMutex
	// contains filtered or unexported fields
}

func NewContext

func NewContext() *Context

默认使用

func (*Context) Accept

func (ctx *Context) Accept() error

func (*Context) Listen

func (ctx *Context) Listen(network, address string) (*Context, error)

func (*Context) ReadBlock

func (ctx *Context) ReadBlock() chan *ServerConnect

阻塞读取 server block read

func (*Context) ReadServerConnect

func (ctx *Context) ReadServerConnect() *ServerConnect

服务端读取 server read data

func (*Context) Run

func (ctx *Context) Run(network, address string) error

type EventList

type EventList struct {
	Events []unix.EpollEvent
	// contains filtered or unexported fields
}

type ServerConnect

type ServerConnect struct {
	Conn  int
	Data  []byte
	Error error
	Ctx   *Context
}

func (*ServerConnect) Close

func (conn *ServerConnect) Close() error

断开连接 close connect

func (*ServerConnect) ReadByte

func (conn *ServerConnect) ReadByte() []byte

读取数据 read []byte

func (*ServerConnect) ReadString

func (conn *ServerConnect) ReadString() string

func (*ServerConnect) Write

func (conn *ServerConnect) Write(data []byte) (n int, err error)

服务端写入 []byte server write []byte

func (*ServerConnect) WriteByteAndClose

func (conn *ServerConnect) WriteByteAndClose(data []byte) (writeErr, closeErr error)

服务端写入[]byte并且断开连接 server write []byte and close conn

func (*ServerConnect) WriteString

func (conn *ServerConnect) WriteString(data string) (n int, err error)

服务端写入 string server write string

func (*ServerConnect) WriteStringAndClose

func (conn *ServerConnect) WriteStringAndClose(data string) (writeErr, closeErr error)

服务端写入string并且断开连接 server write string and close conn

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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