network

package
v0.0.0-...-d436db8 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2020 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OpcodePing          = 0
	OpcodePong          = 1
	OpcodeBind          = 2
	OpcodeMsg           = 3
	OpcodeMsgAck        = 4
	OpcodeUnbind        = 5
	OpcodeBindAck       = 6
	OpcodeUnbindAck     = 7
	OpcodeKick          = 8 // 踢掉用户
	OpcodeMigrate       = 9 // 迁移用户到给定room
	OpcodeRebind        = 10
	OpcodeRebindAck     = 11
	OpcodeOfflineNotify = 12 // miop5离线补偿,走execcmd
	OpcodeUploadData    = 13 //上行数据
	OpcodeUploadDataAck = 14 //上行数据ack
	OpcodeHandshake     = 15 //握手协商
)
View Source
const (
	MIOP_MAX_DATA_LENGTH int32 = 1024 * 1024
	MiopBindAckMapMax    int   = 150000 //map最大值,大于此值,返回错误,记录日志
)
View Source
const (
	TcpNetwork             = "tcp"
	WebSocketNetwork       = "web"
	TLSNetwork             = "tls"
	WebSocketSecureNetwork = "wss"
)
View Source
const (
	MAX_UPLOAD_SIZE = 1024 * 1024
)

Variables

View Source
var Miop5PongStream = func() []byte {
	miopBuf := NewMiopBuffer()
	miopBuf.Version = 5
	miopBuf.OpCode = OpcodePong
	return miopBuf.Stream()
}()
View Source
var Miop6PongStream = func() []byte {
	miopBuf := NewMiopBuffer()
	miopBuf.Version = 6
	miopBuf.OpCode = OpcodePong
	return miopBuf.Stream()
}()
View Source
var MiopBindAckMap map[string][]byte
View Source
var MiopBindAckMapLock sync.RWMutex

Functions

func MiopBindAckStream

func MiopBindAckStream(appId, r string, version int16, opCode int16) ([]byte, error)

此处bind和unbind的ACK公用一个map,不过要做区分

func RegisterRpcTypeForValue

func RegisterRpcTypeForValue(value interface{})

注册 rpc 类型

Types

type HttpClient

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

func NewHttpClient

func NewHttpClient() *HttpClient

func (*HttpClient) Close

func (this *HttpClient) Close() error

func (*HttpClient) ConnectTo

func (this *HttpClient) ConnectTo(address string, timeout time.Duration) error

func (*HttpClient) Dial

func (this *HttpClient) Dial(localAddress string, remoteAddress string, timeout time.Duration) error

func (*HttpClient) Get

func (this *HttpClient) Get(uri string, readTimeout time.Duration, writeTimeout time.Duration) (*HttpResponse, error)

func (*HttpClient) Post

func (this *HttpClient) Post(uri string, posts map[string]string, readTimeout time.Duration, writeTimeout time.Duration) (*HttpResponse, error)

func (*HttpClient) Recv

func (this *HttpClient) Recv(readTimeout time.Duration) (*HttpResponse, error)

func (*HttpClient) Send

func (this *HttpClient) Send(request *HttpRequest, writeTimeout time.Duration) error

func (*HttpClient) SetDebug

func (this *HttpClient) SetDebug(debug bool)

type HttpConnection

type HttpConnection struct {
	*TcpConnection // 内嵌 *TcpConnection
	// contains filtered or unexported fields
}

func NewHttpConnection

func NewHttpConnection(tcpConnection *TcpConnection) *HttpConnection

func (*HttpConnection) FinishChunk

func (this *HttpConnection) FinishChunk() error

func (*HttpConnection) ReadRequest

func (this *HttpConnection) ReadRequest(timeout time.Duration) (*HttpRequest, error)

func (*HttpConnection) ReadResponse

func (this *HttpConnection) ReadResponse(timeout time.Duration) (*HttpResponse, error)

func (*HttpConnection) ReadToRequest

func (this *HttpConnection) ReadToRequest(request *HttpRequest, timeout time.Duration) (*HttpRequest, error)

func (*HttpConnection) SendChunk

func (this *HttpConnection) SendChunk(s string) error

func (*HttpConnection) WriteChunkedHeader

func (this *HttpConnection) WriteChunkedHeader(response *HttpResponse, timeout time.Duration) error

func (*HttpConnection) WriteRequest

func (this *HttpConnection) WriteRequest(request *HttpRequest, timeout time.Duration) error

func (*HttpConnection) WriteResponse

func (this *HttpConnection) WriteResponse(response *HttpResponse, timeout time.Duration) error

func (*HttpConnection) WriteResponseStream

func (this *HttpConnection) WriteResponseStream(stream []byte, timeout time.Duration) error

type HttpMessage

type HttpMessage interface {
	SetHeader(key string, value string)
	Header(key string) string
	Stream() []byte
	// contains filtered or unexported methods
}

type HttpRequest

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

func NewHttpRequest

func NewHttpRequest() *HttpRequest

func (*HttpRequest) Chas

func (this *HttpRequest) Chas(key string) bool

func (*HttpRequest) Cint

func (this *HttpRequest) Cint(key string) int

func (*HttpRequest) Cookies

func (this *HttpRequest) Cookies() map[string]string

func (*HttpRequest) Cstr

func (this *HttpRequest) Cstr(key string) string

func (*HttpRequest) GetRemoteIp

func (this *HttpRequest) GetRemoteIp() string

func (*HttpRequest) Gets

func (this *HttpRequest) Gets() map[string]string

func (*HttpRequest) Ghas

func (this *HttpRequest) Ghas(key string) bool

func (*HttpRequest) Gint

func (this *HttpRequest) Gint(key string) int

func (*HttpRequest) Gstr

func (this *HttpRequest) Gstr(key string) string

func (*HttpRequest) Header

func (this *HttpRequest) Header(key string) string

func (*HttpRequest) IsDelete

func (this *HttpRequest) IsDelete() bool

func (*HttpRequest) IsGet

func (this *HttpRequest) IsGet() bool

func (*HttpRequest) IsKeepAlive

func (this *HttpRequest) IsKeepAlive() bool

func (*HttpRequest) IsPost

func (this *HttpRequest) IsPost() bool

func (*HttpRequest) IsPut

func (this *HttpRequest) IsPut() bool

func (*HttpRequest) Method

func (this *HttpRequest) Method() string

func (*HttpRequest) PathInfo

func (this *HttpRequest) PathInfo() string

func (*HttpRequest) Phas

func (this *HttpRequest) Phas(key string) bool

func (*HttpRequest) Pint

func (this *HttpRequest) Pint(key string) int

func (*HttpRequest) Posts

func (this *HttpRequest) Posts() map[string]string

func (*HttpRequest) Pstr

func (this *HttpRequest) Pstr(key string) string

func (*HttpRequest) Rint

func (this *HttpRequest) Rint(key string) int

func (*HttpRequest) Rstr

func (this *HttpRequest) Rstr(key string) string

func (*HttpRequest) SetHeader

func (this *HttpRequest) SetHeader(key string, value string)

func (*HttpRequest) SetMethod

func (this *HttpRequest) SetMethod(method string)

func (*HttpRequest) SetPosts

func (this *HttpRequest) SetPosts(posts map[string]string)

func (*HttpRequest) SetPstr

func (this *HttpRequest) SetPstr(key, value string)

func (*HttpRequest) SetRemoteIp

func (this *HttpRequest) SetRemoteIp(ip string)

func (*HttpRequest) SetUri

func (this *HttpRequest) SetUri(uri string)

func (*HttpRequest) SetVar

func (this *HttpRequest) SetVar(key string, value interface{})

func (*HttpRequest) SetVersion

func (this *HttpRequest) SetVersion(version string)

func (*HttpRequest) Stream

func (this *HttpRequest) Stream() []byte

func (*HttpRequest) String

func (this *HttpRequest) String() string

func (*HttpRequest) Uri

func (this *HttpRequest) Uri() string

func (*HttpRequest) Var

func (this *HttpRequest) Var(key string) interface{}

func (*HttpRequest) Version

func (this *HttpRequest) Version() string

type HttpResponse

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

func NewHttpResponse

func NewHttpResponse() *HttpResponse

func (*HttpResponse) BodyStream

func (this *HttpResponse) BodyStream() []byte

func (*HttpResponse) BodyString

func (this *HttpResponse) BodyString() string

func (*HttpResponse) ChunkedStream

func (this *HttpResponse) ChunkedStream() []byte

func (*HttpResponse) Code

func (this *HttpResponse) Code() int

func (*HttpResponse) Header

func (this *HttpResponse) Header(key string) string

func (*HttpResponse) IsConnectionClose

func (this *HttpResponse) IsConnectionClose() bool

func (*HttpResponse) Putb

func (this *HttpResponse) Putb(stream []byte)

func (*HttpResponse) Puts

func (this *HttpResponse) Puts(content string)

func (*HttpResponse) SetBodyStream

func (this *HttpResponse) SetBodyStream(bodyStream []byte)

func (*HttpResponse) SetBodyString

func (this *HttpResponse) SetBodyString(bodyString string)

func (*HttpResponse) SetCode

func (this *HttpResponse) SetCode(code int)

func (*HttpResponse) SetHeader

func (this *HttpResponse) SetHeader(key string, value string)

func (*HttpResponse) SetHeaderWithChunkedMode

func (this *HttpResponse) SetHeaderWithChunkedMode()

func (*HttpResponse) SetHeaderWithContentLength

func (this *HttpResponse) SetHeaderWithContentLength(length string)

func (*HttpResponse) SetStatus

func (this *HttpResponse) SetStatus(code int, phrase string)

func (*HttpResponse) SetVersion

func (this *HttpResponse) SetVersion(version string)

func (*HttpResponse) Status

func (this *HttpResponse) Status() (int, string)

func (*HttpResponse) Stream

func (this *HttpResponse) Stream() []byte

func (*HttpResponse) String

func (this *HttpResponse) String() string

func (*HttpResponse) Version

func (this *HttpResponse) Version() string

type INetwork

type INetwork interface {
	net.Conn

	// network type: tcp, websocket or ...
	Type() string

	// read specified length from the network interface
	ReadStream(b []byte, n int) error

	// write bytes to the network interface
	WriteBytes(b []byte, t time.Duration) (int, error)

	// get remote ip
	RemoteIp() string
}

type MiopBuffer

type MiopBuffer struct {
	Version int16
	OpCode  int16
	PropLen int16
	DataLen int32
	Prop    map[string]string
	Data    []byte
}

*

  • 已定义的 Prop 下标有: *
  • t:300
  • u:hello@world
  • k:0 (kick, 缺省是1)
  • d:0 (durable, 缺省是1)
  • c:1 (缺省不带该key)
  • OpCode只在version=5时存在

func NewMiopBuffer

func NewMiopBuffer() *MiopBuffer

func (*MiopBuffer) HasProp

func (this *MiopBuffer) HasProp(key string) bool

func (*MiopBuffer) IsAckMessage

func (this *MiopBuffer) IsAckMessage() bool

判断是否是ack包

func (*MiopBuffer) IsEmpty

func (this *MiopBuffer) IsEmpty() bool

func (*MiopBuffer) IsKickMessage

func (this *MiopBuffer) IsKickMessage() bool

判断是否是kick包

func (*MiopBuffer) IsMessage

func (this *MiopBuffer) IsMessage() bool

func (*MiopBuffer) IsMigrateMessage

func (this *MiopBuffer) IsMigrateMessage() bool

判断是否是迁移包

func (*MiopBuffer) IsPongMessage

func (this *MiopBuffer) IsPongMessage() bool

判断是否是pong包

func (*MiopBuffer) ReadFrom

func (this *MiopBuffer) ReadFrom(tcpConnection *TcpConnection, timeout time.Duration) error

func (*MiopBuffer) ReadUpdateData

func (this *MiopBuffer) ReadUpdateData(offset int) (string, int, error)

获取上行数据的data字段,返回data,本次解析到的offset

func (*MiopBuffer) Reset

func (this *MiopBuffer) Reset() error

func (*MiopBuffer) Stream

func (this *MiopBuffer) Stream() []byte

func (*MiopBuffer) StreamMiop

func (this *MiopBuffer) StreamMiop() []byte

编码miop0~4版本

func (*MiopBuffer) StreamMiop5

func (this *MiopBuffer) StreamMiop5() []byte

编码miop5版本

func (*MiopBuffer) WriteTo

func (this *MiopBuffer) WriteTo(tcpConnection *TcpConnection, timeout time.Duration) error

type RpcRequest

type RpcRequest struct {
	Func string
	Args map[string]interface{}
}

func NewRpcRequest

func NewRpcRequest() *RpcRequest

type RpcResponse

type RpcResponse struct {
	Result bool
	Reason string
	Code   int
	Args   map[string]interface{}
	Data   interface{}
}

func NewRpcResponse

func NewRpcResponse() *RpcResponse

type TLSConnection

type TLSConnection struct {
	*tls.Conn
	Buf *bufio.Reader
}

func ShakeTLS

func ShakeTLS(tcpConn *TcpConnection) (*TLSConnection, error)

func (*TLSConnection) IsHTTPProto

func (c *TLSConnection) IsHTTPProto() bool

func (*TLSConnection) Read

func (c *TLSConnection) Read(b []byte) (int, error)

func (*TLSConnection) ReadStream

func (c *TLSConnection) ReadStream(b []byte, n int) error

func (*TLSConnection) RemoteIp

func (c *TLSConnection) RemoteIp() string

func (*TLSConnection) Type

func (c *TLSConnection) Type() string

func (*TLSConnection) WriteBytes

func (c *TLSConnection) WriteBytes(b []byte, timeout time.Duration) (int, error)

type TcpConnection

type TcpConnection struct {
	*net.TCPConn
	// contains filtered or unexported fields
}

tcp 连接

func TcpConnect

func TcpConnect(localAddress string, remoteAddress string, timeout time.Duration) (*TcpConnection, error)

连接一个 tcp 地址

func (*TcpConnection) IsCrossDomain

func (this *TcpConnection) IsCrossDomain() bool

func (*TcpConnection) IsHttpProto

func (this *TcpConnection) IsHttpProto() bool

func (*TcpConnection) IsMiopV3

func (this *TcpConnection) IsMiopV3() bool

Miop协议版本3

func (*TcpConnection) IsMiopV5

func (this *TcpConnection) IsMiopV5() bool

Miop协议版本5

func (*TcpConnection) IsMiopV6

func (this *TcpConnection) IsMiopV6() bool

Miop协议版本6

func (*TcpConnection) IsTLSProto

func (this *TcpConnection) IsTLSProto() bool

TLS 协议

func (*TcpConnection) IsXimpProto

func (this *TcpConnection) IsXimpProto() bool

func (*TcpConnection) Read

func (this *TcpConnection) Read(p []byte) (n int, err error)

func (*TcpConnection) ReadProtoBuffer

func (this *TcpConnection) ReadProtoBuffer() error

func (*TcpConnection) ReadRpcRequest

func (this *TcpConnection) ReadRpcRequest(timeout time.Duration) (*RpcRequest, error)

func (*TcpConnection) ReadStream

func (this *TcpConnection) ReadStream(stream []byte, count int) error

func (*TcpConnection) RemoteIp

func (this *TcpConnection) RemoteIp() string

获取连接ip

func (*TcpConnection) RemotePort

func (this *TcpConnection) RemotePort() string

获取连接port

func (*TcpConnection) Rpc

func (this *TcpConnection) Rpc(request *RpcRequest, readTimeout time.Duration, writeTimeout time.Duration) (*RpcResponse, error)

func (*TcpConnection) Type

func (this *TcpConnection) Type() string

func (*TcpConnection) WriteBytes

func (this *TcpConnection) WriteBytes(p []byte, timeout time.Duration) (int, error)

func (*TcpConnection) WriteRpcResponse

func (this *TcpConnection) WriteRpcResponse(response *RpcResponse, timeout time.Duration) error

type TcpListener

type TcpListener struct {
	*net.TCPListener
}

tcp 监听器

func TcpListen

func TcpListen(address string) (*TcpListener, error)

监听一个 tcp 地址

func (*TcpListener) Accept

func (this *TcpListener) Accept() (*TcpConnection, error)

type WebSocketConnection

type WebSocketConnection struct {
	*websocket.Conn
	ReadBuffer []byte
	// contains filtered or unexported fields
}

func ShakeWebSocket

func ShakeWebSocket(tcp *TcpConnection, req *http.Request) (*WebSocketConnection, error)

func (*WebSocketConnection) Read

func (w *WebSocketConnection) Read(b []byte) (n int, err error)

func (*WebSocketConnection) ReadStream

func (w *WebSocketConnection) ReadStream(stream []byte, count int) error

func (*WebSocketConnection) RemoteIp

func (w *WebSocketConnection) RemoteIp() string

func (*WebSocketConnection) SetDeadline

func (w *WebSocketConnection) SetDeadline(t time.Time) error

func (*WebSocketConnection) Type

func (w *WebSocketConnection) Type() string

func (*WebSocketConnection) Write

func (w *WebSocketConnection) Write(b []byte) (n int, err error)

func (*WebSocketConnection) WriteBytes

func (w *WebSocketConnection) WriteBytes(b []byte, t time.Duration) (int, error)

type WebSocketSecureConnection

type WebSocketSecureConnection struct {
	*websocket.Conn
	ReadBuffer []byte
	// contains filtered or unexported fields
}

func ShakeWebSocketSecure

func ShakeWebSocketSecure(tls *TLSConnection, req *http.Request) (*WebSocketSecureConnection, error)

func (*WebSocketSecureConnection) Read

func (w *WebSocketSecureConnection) Read(b []byte) (n int, err error)

func (*WebSocketSecureConnection) ReadStream

func (w *WebSocketSecureConnection) ReadStream(stream []byte, count int) error

func (*WebSocketSecureConnection) RemoteIp

func (w *WebSocketSecureConnection) RemoteIp() string

func (*WebSocketSecureConnection) SetDeadline

func (w *WebSocketSecureConnection) SetDeadline(t time.Time) error

func (*WebSocketSecureConnection) Type

func (*WebSocketSecureConnection) Write

func (w *WebSocketSecureConnection) Write(b []byte) (n int, err error)

func (*WebSocketSecureConnection) WriteBytes

func (w *WebSocketSecureConnection) WriteBytes(b []byte, t time.Duration) (int, error)

type XimpBuffer

type XimpBuffer struct {
	IsHeartbeat bool    // 是否是心跳包
	IsDecrypt   bool    // 是否已经解密,false时是加密,
	IsClient    bool    // 是否是客户端发上来的包
	HasHeader   bool    // 是否有magic或者flag
	Version     uint16  // 协议版本, 4个bit
	CVersion    uint16  // 客户端版本,12个bit
	Appid       uint16  // 2字节
	Reserved    [6]byte // 6个字节的保留字段
	DataStream  []byte  // proto数据
	// 业务相关
	TimeStamp int64  // 用于chatroom发送消息时的延迟统计
	TraceId   string // 客户端发上来的跟踪ID
}

客户端与服务端的第一次回包

func NewXimpBuffer

func NewXimpBuffer() *XimpBuffer

func (*XimpBuffer) Decrypt

func (this *XimpBuffer) Decrypt(rkey []byte) error

func (*XimpBuffer) Encode

func (this *XimpBuffer) Encode() ([]byte, error)

编码

func (*XimpBuffer) Encrypt

func (this *XimpBuffer) Encrypt(rkey []byte) error

func (*XimpBuffer) ReadFrom

func (this *XimpBuffer) ReadFrom(isClient bool, conn INetwork, timeout time.Duration) error

读取协议的所有内容 isClient 表示要读取的包是否是客户端传过来的包

func (*XimpBuffer) String

func (this *XimpBuffer) String() string

func (*XimpBuffer) WriteTo

func (this *XimpBuffer) WriteTo(tcpConnection *TcpConnection, timeout time.Duration) error

Jump to

Keyboard shortcuts

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