network

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ConnPropertyHttpAuthorization = "HttpAuthorization"
	ConnPropertyHttpReader        = "HttpReader"
	ConnPropertyHttpWriter        = "HttpWriter"
)
View Source
var SHARD_COUNT = 32

Functions

func ConnPropertyGet

func ConnPropertyGet[T any](c iface.IConnection, key string) T

获取连接属性

func ConnPropertyRemove

func ConnPropertyRemove(c iface.IConnection, key string)

删除连接属性

func ConnPropertySet

func ConnPropertySet(c iface.IConnection, key string, value any)

设置连接属性

func GetInstanceConnManager

func GetInstanceConnManager() iface.IConnectionManager

连接管理器

func GetInstanceMsgHandler

func GetInstanceMsgHandler() iface.IMsgHandler

消息处理器

func GetInstanceServerManager

func GetInstanceServerManager() iface.IServerManager

服务管理器

func GetServerHTTP

func GetServerHTTP() iface.IServer

func GetServerKCP

func GetServerKCP() iface.IServer

func GetServerTCP

func GetServerTCP() iface.IServer

func GetServerWS

func GetServerWS() iface.IServer

func NewConnectionHTTP

func NewConnectionHTTP(server iface.IServer, writer http.ResponseWriter, reader *http.Request) iface.IConnection

func NewConnectionKCP

func NewConnectionKCP(server *serverKCP, conn net.Conn) iface.IConnection

func NewConnectionTCP

func NewConnectionTCP(server iface.IServer, conn *net.TCPConn) iface.IConnection

func NewConnectionWS

func NewConnectionWS(server iface.IServer, conn *websocket.Conn) iface.IConnection

func NewDataPack

func NewDataPack() iface.IDataPack

func NewMsgPackage

func NewMsgPackage(id int32, data []byte) iface.IMessage

func SetCustomServer

func SetCustomServer(custom *CustomServer)

设置自定义服务器参数

Types

type ConcurrentMap

type ConcurrentMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

ConcurrentMap A "thread" safe map of type string:Anything. To avoid lock bottlenecks this map is dived to several (SHARD_COUNT) map shards.

func NewConcurrentMap

func NewConcurrentMap[V any]() ConcurrentMap[string, V]

NewConcurrentMap Creates a new concurrent map.

func NewConcurrentStringer

func NewConcurrentStringer[K Stringer, V any]() ConcurrentMap[K, V]

NewConcurrentStringer Creates a new concurrent map.

func NewWithCustomShardingFunction

func NewWithCustomShardingFunction[K comparable, V any](sharding func(key K) uint32) ConcurrentMap[K, V]

NewWithCustomShardingFunction Creates a new concurrent map.

func (ConcurrentMap[K, V]) Clear

func (m ConcurrentMap[K, V]) Clear()

Clear removes all items from map.

func (ConcurrentMap[K, V]) Count

func (m ConcurrentMap[K, V]) Count() int

Count returns the number of elements within the map.

func (ConcurrentMap[K, V]) Get

func (m ConcurrentMap[K, V]) Get(key K) (V, bool)

Get retrieves an element from map under given key.

func (ConcurrentMap[K, V]) GetShard

func (m ConcurrentMap[K, V]) GetShard(key K) *ConcurrentMapShared[K, V]

GetShard returns shard under given key

func (ConcurrentMap[K, V]) Has

func (m ConcurrentMap[K, V]) Has(key K) bool

Has Looks up an item under specified key

func (ConcurrentMap[K, V]) IsEmpty

func (m ConcurrentMap[K, V]) IsEmpty() bool

IsEmpty checks if map is empty.

func (ConcurrentMap[K, V]) Items

func (m ConcurrentMap[K, V]) Items() map[K]V

Items returns all items as map[string]V

func (ConcurrentMap[K, V]) IterBuffered

func (m ConcurrentMap[K, V]) IterBuffered() <-chan Tuple[K, V]

IterBuffered returns a buffered iterator which could be used in a for range loop.

func (ConcurrentMap[K, V]) IterCb

func (m ConcurrentMap[K, V]) IterCb(fn IterCb[K, V])

IterCb Callback based iterator, cheapest way to read all elements in a map.

func (ConcurrentMap[K, V]) Keys

func (m ConcurrentMap[K, V]) Keys() []K

Keys returns all keys as []string

func (ConcurrentMap[K, V]) MSet

func (m ConcurrentMap[K, V]) MSet(data map[K]V)

func (ConcurrentMap[K, V]) MarshalJSON

func (m ConcurrentMap[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON Reviles ConcurrentMap "private" variables to json marshal.

func (ConcurrentMap[K, V]) Pop

func (m ConcurrentMap[K, V]) Pop(key K) (v V, exists bool)

Pop removes an element from the map and returns it

func (ConcurrentMap[K, V]) Remove

func (m ConcurrentMap[K, V]) Remove(key K)

Remove removes an element from the map.

func (ConcurrentMap[K, V]) RemoveCb

func (m ConcurrentMap[K, V]) RemoveCb(key K, cb RemoveCb[K, V]) bool

RemoveCb locks the shard containing the key, retrieves its current value and calls the callback with those params If callback returns true and element exists, it will remove it from the map Returns the value returned by the callback (even if element was not present in the map)

func (ConcurrentMap[K, V]) Set

func (m ConcurrentMap[K, V]) Set(key K, value V)

Set Sets the given value under the specified key.

func (ConcurrentMap[K, V]) SetIfAbsent

func (m ConcurrentMap[K, V]) SetIfAbsent(key K, value V) bool

SetIfAbsent Sets the given value under the specified key if no value was associated with it.

func (ConcurrentMap[K, V]) UnmarshalJSON

func (m ConcurrentMap[K, V]) UnmarshalJSON(b []byte) (err error)

UnmarshalJSON Reverse process of Marshal.

func (ConcurrentMap[K, V]) Upsert

func (m ConcurrentMap[K, V]) Upsert(key K, value V, cb UpsertCb[V]) (res V)

Upsert Insert or Update - updates existing element or inserts a new one using UpsertCb

type ConcurrentMapShared

type ConcurrentMapShared[K comparable, V any] struct {
	sync.RWMutex // Read Write mutex, guards access to internal map.
	// contains filtered or unexported fields
}

ConcurrentMapShared A "thread" safe string to anything map.

type CustomServer

type CustomServer struct {
	AppConf    *config.AppConf // 服务启动配置
	DataPacket iface.IDataPack // 编码/解码器
}

自定义服务器

type Integer

type Integer int

func (Integer) String

func (i Integer) String() string

type IterCb

type IterCb[K comparable, V any] func(key K, v V)

IterCb Iterator callbacalled for every key,value found in maps. RLock is held for all calls for a given shard therefore callback sess consistent view of a shard, but not across the shards

type Message added in v1.1.2

type Message struct {
	proto.Message `json:"-"`
	Id            uint16 `protobuf:"bytes,1,opt,name=msg_id,proto3" json:"msg_id"` // 消息Id
	Data          string `protobuf:"bytes,2,opt,name=data,proto3" json:"data"`     // 消息内容
	DataLen       uint16 `json:"-"`                                                // 消息长度
}

func (*Message) GetData added in v1.1.2

func (m *Message) GetData() []byte

func (*Message) GetDataLen added in v1.1.2

func (m *Message) GetDataLen() uint16

func (*Message) GetMsgId added in v1.1.2

func (m *Message) GetMsgId() uint16

func (*Message) SetData added in v1.1.2

func (m *Message) SetData(bytes []byte)

type RemoveCb

type RemoveCb[K any, V any] func(key K, v V, exists bool) bool

RemoveCb is a callback executed in a map.RemoveCb() call, while Lock is held If returns true, the element will be removed from the map

type Stringer

type Stringer interface {
	fmt.Stringer
	comparable
}

type Tuple

type Tuple[K comparable, V any] struct {
	Key K
	Val V
}

Tuple Used by the Iter & IterBuffered functions to wrap two variables together over a channel,

type UpsertCb

type UpsertCb[V any] func(exist bool, valueInMap V, newValue V) V

UpsertCb Callback to return new element to be inserted into the map It is called while lock is held, therefore it MUST NOT try to access other keys in same map, as it can lead to deadlock since Go sync.RWLock is not reentrant

Jump to

Keyboard shortcuts

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