Fetch the repository succeeded.
This action will force synchronization from andeyalee/erpc, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
// Copyright 2017 HenryLee. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package socket
import (
"github.com/henrylee2cn/goutil"
)
// SocketHub sockets hub
type SocketHub struct {
// key: socket id (ip, name and so on)
// value: Socket
sockets goutil.Map
}
// NewSocketHub creates a new sockets hub.
func NewSocketHub() *SocketHub {
chub := &SocketHub{
sockets: goutil.AtomicMap(),
}
return chub
}
// Set sets a Socket.
func (sh *SocketHub) Set(socket Socket) {
_socket, loaded := sh.sockets.LoadOrStore(socket.Id(), socket)
if !loaded {
return
}
sh.sockets.Store(socket.Id(), socket)
if oldSocket := _socket.(Socket); socket != oldSocket {
oldSocket.Close()
}
}
// Get gets Socket by id.
// If second returned arg is false, mean the Socket is not found.
func (sh *SocketHub) Get(id string) (Socket, bool) {
_socket, ok := sh.sockets.Load(id)
if !ok {
return nil, false
}
return _socket.(Socket), true
}
// Range calls f sequentially for each id and Socket present in the socket hub.
// If f returns false, range stops the iteration.
func (sh *SocketHub) Range(f func(Socket) bool) {
sh.sockets.Range(func(key, value interface{}) bool {
return f(value.(Socket))
})
}
// Random gets a Socket randomly.
// If third returned arg is false, mean no Socket is exist.
func (sh *SocketHub) Random() (Socket, bool) {
_, socket, exist := sh.sockets.Random()
if !exist {
return nil, false
}
return socket.(Socket), true
}
// Len returns the length of the socket hub.
// Note: the count implemented using sync.Map may be inaccurate.
func (sh *SocketHub) Len() int {
return sh.sockets.Len()
}
// Delete deletes the Socket for a id.
func (sh *SocketHub) Delete(id string) {
sh.sockets.Delete(id)
}
// ChangeId changes the socket id.
// Note: if the old id is remoteAddr, won't delete the index from socketHub.
func (sh *SocketHub) ChangeId(newId string, socket Socket) {
oldId := socket.Id()
socket.SetId(newId)
sh.Set(socket)
if oldId != socket.RemoteAddr().String() {
sh.Delete(oldId)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。