netutil

package
Version: v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2017 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// NETWORK_ENDIAN is the network Endian of connections
	NETWORK_ENDIAN = binary.LittleEndian
)

Functions

func ConnectTCP

func ConnectTCP(host string, port int) (net.Conn, error)

ConnectTCP connects to host:port in TCP

func IsConnectionError

func IsConnectionError(_err interface{}) bool

IsConnectionError check if the error is a connection error (close)

func PackFloat32

func PackFloat32(order binary.ByteOrder, b []byte, f float32)

PackFloat32 packs float32 in specified byte order

func PutFloat32

func PutFloat32(b []byte, f float32)

func ServeTCP

func ServeTCP(listenAddr string, delegate TCPServerDelegate) error

ServeTCP serves on specified address as TCP server

func ServeTCPForever

func ServeTCPForever(listenAddr string, delegate TCPServerDelegate)

ServeTCPForever serves on specified address as TCP server, for ever ...

func UnpackFloat32

func UnpackFloat32(order binary.ByteOrder, b []byte) (f float32)

UnpackFloat32 unpacks float32 in specified byte order

Types

type BufferedConnection

type BufferedConnection struct {
	Connection
	// contains filtered or unexported fields
}

BufferedConnection provides buffered write to connections

func NewBufferedConnection

func NewBufferedConnection(conn Connection) *BufferedConnection

NewBufferedWriteConnection creates a new connection with buffered write based on underlying connection

func (*BufferedConnection) Flush

func (brc *BufferedConnection) Flush() error

func (*BufferedConnection) Read

func (brc *BufferedConnection) Read(p []byte) (int, error)

Read

func (*BufferedConnection) Write

func (brc *BufferedConnection) Write(p []byte) (int, error)

type Connection

type Connection interface {
	net.Conn // Connection is more than net.Conn
	Flush() error
}

Connection interface for connections to servers

type GobMsgPacker

type GobMsgPacker struct{}

GobMsgPacker packs and unpacks message in golang's Gob format

func (GobMsgPacker) PackMsg

func (mp GobMsgPacker) PackMsg(msg interface{}, buf []byte) ([]byte, error)

PackMsg packs a message to bytes of gob format

func (GobMsgPacker) UnpackMsg

func (mp GobMsgPacker) UnpackMsg(data []byte, msg interface{}) error

UnpackMsg unpacks bytes of gob format to message

type JSONMsgPacker

type JSONMsgPacker struct{}

JsoniterMsgPacker packs and unpacks messages in JSON format

func (JSONMsgPacker) PackMsg

func (mp JSONMsgPacker) PackMsg(msg interface{}, buf []byte) ([]byte, error)

PackMsg packs message to bytes of JSON format

func (JSONMsgPacker) UnpackMsg

func (mp JSONMsgPacker) UnpackMsg(data []byte, msg interface{}) error

UnpackMsg unpacks bytes of JSON format to message

type JsoniterMsgPacker

type JsoniterMsgPacker struct{}

JsoniterMsgPacker packs and unpacks messages in JSON format

func (JsoniterMsgPacker) PackMsg

func (mp JsoniterMsgPacker) PackMsg(msg interface{}, buf []byte) ([]byte, error)

PackMsg packs message to bytes of JSON format

func (JsoniterMsgPacker) UnpackMsg

func (mp JsoniterMsgPacker) UnpackMsg(data []byte, msg interface{}) error

UnpackMsg unpacks bytes of JSON format to message

type MessagePackMsgPacker

type MessagePackMsgPacker struct{}

MessagePackMsgPacker packs and unpacks message in MessagePack format

func (MessagePackMsgPacker) PackMsg

func (mp MessagePackMsgPacker) PackMsg(msg interface{}, buf []byte) ([]byte, error)

PackMsg packs message to bytes in MessagePack format

func (MessagePackMsgPacker) UnpackMsg

func (mp MessagePackMsgPacker) UnpackMsg(data []byte, msg interface{}) error

UnpackMsg unpacksbytes in MessagePack format to message

type MsgPacker

type MsgPacker interface {
	PackMsg(msg interface{}, buf []byte) ([]byte, error)
	UnpackMsg(data []byte, msg interface{}) error
}

MsgPacker is used to packs and unpacks messages

var (
	// MSG_PACKER is used for packing and unpacking network data
	MSG_PACKER MsgPacker = MessagePackMsgPacker{}
)

type NetConnection

type NetConnection struct {
	net.Conn
}

NetConnection converts net.Conn to Connection

func (NetConnection) Flush

func (c NetConnection) Flush() error

Flush flushes network connection

type Packet

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

Packet is a packet for sending data

func NewPacket

func NewPacket() *Packet

NewPacket allocates a new packet

func (*Packet) AddRefCount

func (p *Packet) AddRefCount(add int64)

AddRefCount adds reference count of packet

func (*Packet) AppendArgs

func (p *Packet) AppendArgs(args []interface{})

AppendArgs appends arguments to the end of payload one by one

func (*Packet) AppendBool

func (p *Packet) AppendBool(b bool)

AppendBool appends one byte 1/0 to the end of payload

func (*Packet) AppendByte

func (p *Packet) AppendByte(b byte)

AppendByte appends one byte to the end of payload

func (*Packet) AppendBytes

func (p *Packet) AppendBytes(v []byte)

AppendBytes appends slice of bytes to the end of payload

func (*Packet) AppendClientID

func (p *Packet) AppendClientID(id common.ClientID)

AppendClientID appends one Client ID to the end of payload

func (*Packet) AppendData

func (p *Packet) AppendData(msg interface{})

AppendData appends one data of any type to the end of payload

func (*Packet) AppendEntityID

func (p *Packet) AppendEntityID(id common.EntityID)

AppendEntityID appends one Entity ID to the end of payload

func (*Packet) AppendFloat32

func (p *Packet) AppendFloat32(f float32)

AppendFloat32 appends one float32 to the end of payload

func (*Packet) AppendFloat64

func (p *Packet) AppendFloat64(f float64)

AppendFloat64 appends one float64 to the end of payload

func (*Packet) AppendStringList

func (p *Packet) AppendStringList(list []string)

AppendStringList appends a list of strings to the end of payload

func (*Packet) AppendUint16

func (p *Packet) AppendUint16(v uint16)

AppendUint16 appends one uint16 to the end of payload

func (*Packet) AppendUint32

func (p *Packet) AppendUint32(v uint32)

AppendUint32 appends one uint32 to the end of payload

func (*Packet) AppendUint64

func (p *Packet) AppendUint64(v uint64)

AppendUint64 appends one uint64 to the end of payload

func (*Packet) AppendVarBytes

func (p *Packet) AppendVarBytes(v []byte)

AppendVarBytes appends varsize bytes to the end of payload

func (*Packet) AppendVarStr

func (p *Packet) AppendVarStr(s string)

AppendVarStr appends a varsize string to the end of payload

func (*Packet) AssureCapacity

func (p *Packet) AssureCapacity(need uint32)

func (*Packet) ClearPayload

func (p *Packet) ClearPayload()

ClearPayload clears packet payload

func (*Packet) GetPayloadLen

func (p *Packet) GetPayloadLen() uint32

GetPayloadLen returns the payload length

func (*Packet) HasUnreadPayload

func (p *Packet) HasUnreadPayload() bool

HasUnreadPayload returns if all payload is read

func (*Packet) Payload

func (p *Packet) Payload() []byte

Payload returns the total payload of packet

func (*Packet) PayloadCap

func (p *Packet) PayloadCap() uint32

PayloadCap returns the current payload capacity

func (*Packet) PopUint32

func (p *Packet) PopUint32() (v uint32)

PopUint32 pops one uint32 from the end of payload

func (*Packet) ReadArgs

func (p *Packet) ReadArgs() [][]byte

ReadArgs reads a number of arguments from the beginning of unread payload

func (*Packet) ReadBool

func (p *Packet) ReadBool() (v bool)

ReadBool reads one byte 1/0 from the beginning of unread payload

func (*Packet) ReadBytes

func (p *Packet) ReadBytes(size uint32) []byte

ReadBytes reads bytes from the beginning of unread payload

func (*Packet) ReadClientID

func (p *Packet) ReadClientID() common.ClientID

ReadClientID reads one ClientID from the beginning of unread payload

func (*Packet) ReadData

func (p *Packet) ReadData(msg interface{})

ReadData reads one data of any type from the beginning of unread payload

func (*Packet) ReadEntityID

func (p *Packet) ReadEntityID() common.EntityID

ReadEntityID reads one EntityID from the beginning of unread payload

func (*Packet) ReadFloat32

func (p *Packet) ReadFloat32() float32

ReadFloat32 reads one float32 from the beginning of unread payload

func (*Packet) ReadFloat64

func (p *Packet) ReadFloat64() float64

ReadFloat64 reads one float64 from the beginning of unread payload

func (*Packet) ReadOneByte

func (p *Packet) ReadOneByte() (v byte)

ReadOneByte reads one byte from the beginning

func (*Packet) ReadStringList

func (p *Packet) ReadStringList() []string

ReadStringList reads a list of strings from the beginning of unread payload

func (*Packet) ReadUint16

func (p *Packet) ReadUint16() (v uint16)

ReadUint16 reads one uint16 from the beginning of unread payload

func (*Packet) ReadUint32

func (p *Packet) ReadUint32() (v uint32)

ReadUint32 reads one uint32 from the beginning of unread payload

func (*Packet) ReadUint64

func (p *Packet) ReadUint64() (v uint64)

ReadUint64 reads one uint64 from the beginning of unread payload

func (*Packet) ReadVarBytes

func (p *Packet) ReadVarBytes() []byte

ReadVarBytes reads a varsize slice of bytes from the beginning of unread payload

func (*Packet) ReadVarStr

func (p *Packet) ReadVarStr() string

ReadVarStr reads a varsize string from the beginning of unread payload

func (*Packet) Release

func (p *Packet) Release()

Release releases the packet to packet pool

func (*Packet) SetNotCompress

func (p *Packet) SetNotCompress()

SetNotCompress force the packet not to be compressed

func (*Packet) SetPayloadLen

func (p *Packet) SetPayloadLen(plen uint32)

SetPayloadLen sets the payload l

func (*Packet) TotalPayload

func (p *Packet) TotalPayload() []byte

func (*Packet) UnreadPayload

func (p *Packet) UnreadPayload() []byte

UnreadPayload returns the unread payload

func (*Packet) UnwrittenPayload

func (p *Packet) UnwrittenPayload() []byte

UnwrittenPayload returns the unwritten payload, which is the left payload capacity

type PacketConnection

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

PacketConnection is a connection that send and receive data packets upon a network stream connection

func NewPacketConnection

func NewPacketConnection(conn Connection, compressor compress.Compressor) *PacketConnection

NewPacketConnection creates a packet connection based on network connection

func (*PacketConnection) Close

func (pc *PacketConnection) Close() error

Close the connection

func (*PacketConnection) Flush

func (pc *PacketConnection) Flush(reason string) (err error)

Flush connection writes

func (*PacketConnection) LocalAddr

func (pc *PacketConnection) LocalAddr() net.Addr

LocalAddr returns the local address

func (*PacketConnection) NewPacket

func (pc *PacketConnection) NewPacket() *Packet

NewPacket allocates a new packet (usually for sending)

func (*PacketConnection) RecvPacket

func (pc *PacketConnection) RecvPacket() (*Packet, error)

RecvPacket receives the next packet

func (*PacketConnection) RemoteAddr

func (pc *PacketConnection) RemoteAddr() net.Addr

RemoteAddr return the remote address

func (*PacketConnection) SendPacket

func (pc *PacketConnection) SendPacket(packet *Packet) error

SendPacket send packets to remote

func (*PacketConnection) SetRecvDeadline

func (pc *PacketConnection) SetRecvDeadline(deadline time.Time) error

SetRecvDeadline sets the receive deadline

func (*PacketConnection) String

func (pc *PacketConnection) String() string

type TCPServerDelegate

type TCPServerDelegate interface {
	ServeTCPConnection(net.Conn)
}

TCPServerDelegate is the implementations that a TCP server should provide

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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