simpletcp

package module
v0.0.0-...-720616b Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2017 License: MIT Imports: 8 Imported by: 0

README

Simple TCP

Simple TCP 协议数据帧由 报文头 和 数据 两部分组成。

报文头由 固定头、版本号、数据类型、消息ID、报文长度、保留位 组成。

   +--------+--------+------------+---------+  <+
   | Fixed (16 bits) | Version(8) | Type(8) |   |
   +----------------------------------------+   |
   |         Message ID (32 bits)           |   |
   +----------------------------------------+   | Message
   |        Data Length (32 bits)           |   | Header
   +----------------------------------------+   |
   |          Reserved (32 bits)            |   |
   +----------------------------------------+  <+
   |                                        |
   |                Data                    |    Message
   |               ......                   |    Data
   |                                        |
   +----------------------------------------+

报文头长度为 16 字节,数据长度不确定。

Fixed Header: 两个固定的字节,填 'Ac' (0x41 0x63),如果服务端读取的这两个字节不是 'Ac',直接断开连接

Version: 目前版本是 1,填 0x01,Ping 消息版本为 0x00

Data Type: 表示数据格式,目前只支持 json/utf80x01

Message ID: 高位优先的 4 字节无符号整数,如 65538 填 0x00010002,客户端一次可以发送个报文,顺序是 1、2、3,服务端异步返回处理结果,可能返回的顺序是 2、3、1,返回时带上 Message ID,以便客户端确定是哪个报文的返回,以免混乱

Data Length: 真实数据长度,不包含报文头,高位优先的 4 字节无符号整数

Reserved: 保留位,便于其他扩展,留作他用(例如报文过长,可以截断报文,这 4 字节填上关联的 Message ID)

Documentation

Index

Constants

View Source
const (
	HeadLength       = 16
	VersionPing byte = 0x00
	Version1    byte = 0x01
)
View Source
const (
	BodyTypeJSON     byte = 0x01
	BodyTypeProtobuf byte = 0x02
	BodyTypeXML      byte = 0x03
	BodyTypePlain    byte = 0x04
)
View Source
const (
	QueueSize  = 4096
	Processors = 32
)
View Source
const MaxLength uint32 = 1<<16 - HeadLength

Variables

View Source
var (
	ErrFixedHead        = errors.New("fixed Head not 0x41 0x63")
	ErrFrameNil         = errors.New("frame is nil")
	ErrBodyLengthExceed = errors.New("data length exceed")
)
View Source
var Fixed = [2]byte{'A', 'c'}
View Source
var Reserved [4]byte = [4]byte{0, 0, 0, 0}

Functions

This section is empty.

Types

type Connect

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

func NewConnect

func NewConnect(conn *net.TCPConn, serv *Server) *Connect

func (*Connect) Run

func (c *Connect) Run()

type Frame

type Frame struct {
	Head []byte
	Body []byte
}

func NewFrame

func NewFrame() *Frame

func NewFrameDefault

func NewFrameDefault() *Frame

func (*Frame) BodyLength

func (f *Frame) BodyLength() uint32

func (*Frame) BodyType

func (f *Frame) BodyType() byte

func (*Frame) FixedHead

func (f *Frame) FixedHead() []byte

func (*Frame) MessageId

func (f *Frame) MessageId() uint32

func (*Frame) Reserved

func (f *Frame) Reserved() []byte

func (*Frame) SetBodyLength

func (f *Frame) SetBodyLength(bl uint32)

func (*Frame) SetBodyOnly

func (f *Frame) SetBodyOnly(body []byte)

func (*Frame) SetBodyWithLength

func (f *Frame) SetBodyWithLength(body []byte)

func (*Frame) SetMessageId

func (f *Frame) SetMessageId(messageId uint32)

func (*Frame) SetReserved

func (f *Frame) SetReserved(reserved uint32)

func (*Frame) SetVersion

func (f *Frame) SetVersion(version byte)

func (*Frame) String

func (f *Frame) String() string

func (*Frame) Version

func (f *Frame) Version() byte

type Server

type Server struct {
	Host string
	Port int

	Fixed     [2]byte // default 'Ac' (0x41 0x63)
	MaxLength uint32  // default 65536 (1<<16)

	Version  byte // default 1 (0x01)
	BodyType byte // default 1 (0x01, json)

	Handle      func([]byte) []byte // one of handlers must not nil
	HandleFrame func(*Frame) *Frame
	// contains filtered or unexported fields
}

func (*Server) Close

func (s *Server) Close() error

func (*Server) Start

func (s *Server) Start() (err error)

Directories

Path Synopsis
example
client command
server command

Jump to

Keyboard shortcuts

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