nst

package module
v0.0.0-...-8a90b12 Latest Latest
Warning

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

Go to latest
Published: May 31, 2024 License: LGPL-3.0 Imports: 8 Imported by: 7

README

Package nst

一个TCP的服务和客户端实现,包含发送和接收数据的一系列方法

如何使用

服务端的使用

准备一个logs包中的Logs,作为服务端运行时可以发送错误日志的地方。

准备一个符合nst.ConnExecer接口的执行器,这个执行器将接收*nst.ConnExec所传来的客户端数据。

使用nst.NewServer()方法建立服务器,方法将返回Server。如果需要启用TSL加密,则可以继续用Server.ToTLS()方法配置密钥文件。最后用*Server.Start()打开监听。

可使用ConnExec.GetData()接收数据,也可以使用ConnExec.SendData()发送数据,直到服务端或客户端使用了ConnExec.SendClose()。

最终使用*Server.Close()关闭服务器。

客户端的使用

使用NewClient()或NewClientL()创建一个客户端,都是*Client。

使用Client.OpenProgress()或Client.OpenConnect()拿出一个可以使用的连接,也就是*CConnect。

使用CConnect.SendAndReturn()向服务端发送数据并接收服务器端的返回数据,直到服务器端关闭连接或客户端执行CConnect.Close()。

最后使用*Client.Close()关闭整个客户端。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesToUint16

func BytesToUint16(buf []byte) uint16

[]byte转uint16

func BytesToUint32

func BytesToUint32(buf []byte) uint32

[]byte转uint32

func BytesToUint64

func BytesToUint64(buf []byte) uint64

[]byte转uint64

func BytesToUint8

func BytesToUint8(buf []byte) uint8

[]byte转uint8

func Uint16ToBytes

func Uint16ToBytes(i uint16) []byte

Uint16转[]byte

func Uint32ToBytes

func Uint32ToBytes(i uint32) []byte

Uint32转[]byte

func Uint64ToBytes

func Uint64ToBytes(i uint64) []byte

Uint64转[]byte

func Uint8ToBytes

func Uint8ToBytes(i uint8) []byte

Uint8转[]byte

Types

type CConnect

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

the Client connection

func (*CConnect) Close

func (cc *CConnect) Close() (err error)

关闭已经发起的连接

func (*CConnect) SendAndReturn

func (cc *CConnect) SendAndReturn(data []byte) (returndata []byte, err error)

发送一个数据体,并接受返回数据体

type Client

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

func NewClient

func NewClient(addr string, max_count int, iftls bool) (c *Client, err error)

新建一个客户端

func NewClientL

func NewClientL(addr string, max_count int, iftls bool) (c *Client, err error)

新建一个长连接客户端

func (*Client) Close

func (c *Client) Close() (err error)

关闭客户端

func (*Client) OpenConnect

func (c *Client) OpenConnect() (cc *CConnect, err error)

开始处理,也就是真正去做这个连接

func (*Client) OpenProgress

func (c *Client) OpenProgress() (cc *CConnect, err error)

开始处理,也就是真正去做这个连接,与OpenConnect一样

type ConnExec

type ConnExec struct {
	Transmission *Transmission
	// contains filtered or unexported fields
}

func NewConnExec

func NewConnExec(trans *Transmission) (connExec *ConnExec)

func (*ConnExec) GetData

func (c *ConnExec) GetData() (data []byte, err error)

func (*ConnExec) SendClose

func (c *ConnExec) SendClose() (err error)

func (*ConnExec) SendData

func (c *ConnExec) SendData(data []byte) (err error)

func (*ConnExec) SetLong

func (c *ConnExec) SetLong()

func (*ConnExec) SetShort

func (c *ConnExec) SetShort()

type ConnExecer

type ConnExecer interface {
	NSTexec(ce *ConnExec) (SendStat, error)
}

Server's connect execution interface

type SendStat

type SendStat uint8
const (
	SEND_STAT_NO          SendStat = iota // the stat have nothing
	SEND_STAT_OK                          // it's all ok
	SEND_STAT_NOT_OK                      // it's not ok
	SEND_STAT_CONN_LONG                   // this is long connect
	SEND_STAT_CONN_SHORT                  // this is shot connect
	SEND_STAT_CHECK_DATA                  // check the server or connect
	SEND_STAT_NORMAL_DATA                 // normal data
	SEND_STAT_DATA_GOON                   // data goon transmission
	SEND_STAT_DATA_CLOSE                  // data transmission close
	SEND_STAT_CONN_CLOSE                  // connect close

)

type Server

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

A tcp server

func NewServer

func NewServer(execer ConnExecer, port string, elogs logs.Logser) (s *Server, err error)

Create a new server for tcp

func (*Server) Close

func (s *Server) Close()

func (*Server) Start

func (s *Server) Start()

Start the server

func (*Server) ToTLS

func (s *Server) ToTLS(pem, key string) (err error)

Let the Server to tls encryption

type Transmission

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

发送接收数据结构。发送数据的方法必须使用对应类型的接收方法进行接收

func NewTransmission

func NewTransmission(conn net.Conn) *Transmission

新建发送接收

func NewTransmissionTLS

func NewTransmissionTLS(conn *tls.Conn) *Transmission

func (*Transmission) Close

func (t *Transmission) Close() (err error)

关闭连接

func (*Transmission) GetBuf

func (t *Transmission) GetBuf() int

查看缓冲大小

func (*Transmission) GetBytes

func (t *Transmission) GetBytes(len uint64) (returnByte []byte, err error)

接收字节切片(没有字节长度信息)

func (*Transmission) GetData

func (t *Transmission) GetData() (bytes []byte, errs error)

接收一串数据流(包括字节流的长度),特定接收SendData()方法发送的数据

func (*Transmission) GetLen

func (t *Transmission) GetLen() (len uint64, errs error)

接收一个长度属性,也就是接收uint64

func (*Transmission) GetStat

func (t *Transmission) GetStat() (status uint8, errs error)

接收状态,也就是接收uint8

func (*Transmission) GetStruct

func (t *Transmission) GetStruct(stru interface{}) (errs error)

接收一个结构体(会在方法内部从gob转出),只能接受SendStruct()方法发送的信息

func (*Transmission) GetVer

func (t *Transmission) GetVer() (version uint32, errs error)

接收一个流程版本号,也就是接收一个uint32

func (*Transmission) RemoteAddr

func (t *Transmission) RemoteAddr() (addr net.Addr)

func (*Transmission) SendBytes

func (t *Transmission) SendBytes(bytes []byte) (err error)

发送字节切片(没有字节长度信息)

func (*Transmission) SendData

func (t *Transmission) SendData(bytes []byte) (errs error)

发送一串数据流(包括字节流的长度),必须用GetData()方法接收

func (*Transmission) SendLen

func (t *Transmission) SendLen(len uint64) (errs error)

发送一个长度属性,也就是发送uint64

func (*Transmission) SendStat

func (t *Transmission) SendStat(status uint8) (errs error)

发送状态,也就是发送uint8

func (*Transmission) SendStruct

func (t *Transmission) SendStruct(stru interface{}) (errs error)

发送一个结构体(会在方法内部转换成gob),只能用GetStruct()方法接受

func (*Transmission) SendVer

func (t *Transmission) SendVer(version uint32) (errs error)

发送一个流程版本号,也可以发送流程编号,就是发送一个uint32

func (*Transmission) SetBuf

func (t *Transmission) SetBuf(buf int)

设置缓冲大小

Jump to

Keyboard shortcuts

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