本文整理汇总了Golang中push-server/buffpool.BuffPool类的典型用法代码示例。如果您正苦于以下问题:Golang BuffPool类的具体用法?Golang BuffPool怎么用?Golang BuffPool使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BuffPool类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: checkConnActivity
func (this *DispatcherOutComponent) checkConnActivity(Time time.Time, client *model.Client, buffPool *buffpool.BuffPool, number uint16, userUuid string, devID uint32, token uint8) bool {
//5分钟
if time.Now().Sub(Time) > define.KEEP_ALIVE_PEORID {
if client.Conn != nil {
(*client.Conn).Close()
}
if token > 0 {
err := buffPool.RemoveClient(number, userUuid, devID, token)
if err != nil {
err = errors.New("DispatcherComponent.checkConnActivity() Error:can not remove invalid client's connection, " + err.Error())
fmt.Println(err.Error())
return false
}
clientArr := buffPool.QueryForDeviceId(number, userUuid, devID)
if clientArr == nil || len(clientArr) == 0 {
//offline broadcast
unregister := GetUnregisterComponent()
if unregister != nil {
err2 := unregister.offlineBroadcast(buffPool, userUuid, devID)
if err2 != nil {
err2 = errors.New("cDispatcherComponent.checkConnActivity() Error: " + err2.Error())
fmt.Println(err2.Error())
}
} else {
fmt.Println("DispatcherComponent.checkConnActivity() Error: one client disconnected, but offline broadcast send failed, ")
}
}
}
return false
}
return true
}
示例2: handleService
func (this *RegisterComponent) handleService(buffPool *buffpool.BuffPool, message *protocol.Message, conn *net.Conn) {
if buffPool == nil || message == nil || conn == nil {
err := errors.New("RegisterComponent handleService Error: the argument is nil")
return err
}
err := buffPool.AddService(message.Number, message.UserUuid, conn)
if err != nil {
err = fmt.Errorf("RegisterCompoennt handleService Error: %v", err)
return err
}
responseMessage, err := protocol.NewMessage(protocol.VERSION, protocol.MT_REGISTER, protocol.PT_PUSHSERVER, 0, message.Number, 0, P_UID,
message.UserUuid, 0, nil)
if err != nil {
err = fmt.Errorf("RegisterComponent handleService Error:%v", err)
return err
}
GetDispatcherOutComponent().NPushBack(responseMessage, false)
if GetRunWithSnode().Flag {
onlineInfo := structure.ClientOnlineInfo{
PushID: this.PushID,
ParentID: this.PushID,
}
data, err := onlineInfo.Encode()
if err != nil {
err = fmt.Errorf("RegisterCompoennt handleService Error:%v", err)
return err
}
snode_component.GetNodeComponent().Forward(stree_define.ONLINE, msg.UserId, 0, data) //added for QueryUser
}
go this.listenService(conn, buffPool, message.Number, message.UserUuid)
return nil
}
示例3: handleMTRegister
func (this *DispatcherOutComponent) handleMTRegister(msg *protocol.Message, buffPool *buffpool.BuffPool, respComp *ResponseComponent) error {
if msg.PushType == protocol.PT_PUSHSERVER {
conn := buffPool.QueryService(msg.Number, msg.DestUuid)
if conn == nil {
clientConn := buffPool.GetClient(msg.Number, msg.DestUuid, msg.DeviceId, msg.Token)
if clientConn == nil || clientConn.Conn == nil {
err := errors.New("DispatcherOutComponent.handleMTRegister() Error: can not find the dest conn")
fmt.Println(err.Error())
return err
}
conn = clientConn.Conn
}
if conn == nil {
err := errors.New("DispatcherOutComponent.handleMTRegister() Error: can not find the dest conn")
fmt.Println(err.Error())
return err
}
err := this.sendMessage(msg, conn)
if err != nil {
err = errors.New("DispatcherComponnet.handleMTRegister() Error: can not send message out, " + err.Error())
fmt.Println(err.Error())
return err
}
}
return nil
}
示例4: listenService
func (this *RegisterComponent) listenService(conn *net.Conn, buffPool *buffpool.BuffPool, number uint16, userUuid string) error {
if buffPool == nil {
err := errors.New("RegisterComponent listenService Error: buffpool is nil")
return err
}
if conn == nil {
err := errors.New("RegisterComponent listenService Error: the conn is nil")
if GetRunWithSnode().Flag {
this.ServiceOffline(userUuid)
}
err = buffPool.DeleteService(number, userUuid)
if err != nil {
err = fmt.Errorf("RegisterComponent listenService Error:%v", err)
return err
}
return err
}
var dispatcher *DispatcherComponent
for {
dispatcher = GetDispatcherComponent()
if dispatcher != nil {
break
} else {
fmt.Println("RegisterComponent listenService Error: can not get DispatcherCompoennt")
}
}
for {
message, err := this.readMessage(conn)
if number == protocol.N_ACCOUNT_SYS {
}
if err != nil && err != io.EOF && conn != nil {
fmt.Println("RegisterComponent listenService Error: read message fail")
continue
} else if err != nil && (err == io.EOF|conn == nil) {
err = errors.New("RegisterComponent listenService Error:the connection is invalid")
err2 := buffPool.DeleteService(number, userUuid)
if GetRunWithSnode().Flag {
this.ServiceOffline(userUuid)
}
if err2 != nil {
err2 = fmt.Errorf("RegisterComponent listenService Error: %v", err)
fmt.Println(err2.Error())
return err2
}
fmt.Println(err.Error())
return err
}
if message == nil {
continue
}
dispatcher.NPushBack(message, true)
}
return nil
}
示例5: unregisterClient
func (this *UnregisterComponent) unregisterClient(buffPool *buffpool.BuffPool, msg *protocol.Message) error {
if buffPool == nil || msg == nil {
err := errors.New("UnregisterComponent.unregisterClient() Error: invalid argument")
fmt.Println(err.Error())
return err
}
clientConn := buffPool.GetClient(msg.Number, msg.UserUuid, msg.DeviceId, msg.Token)
if clientConn == nil {
return nil
}
//only remove conn from connPool, but do not disconnect this connection
err := buffPool.RemoveClient(msg.Number, msg.UserUuid, msg.DeviceId, msg.Token)
(*clientConn.Conn).Close()
if err != nil {
err = errors.New("UnregisterComponent.unregisterClient() Error: " + err.Error())
fmt.Println(err.Error())
//send failed response to client
resp, err2 := protocol.NewMessage(protocol.VERSION, protocol.MT_ERROR, protocol.PT_PUSHSERVER, msg.Token, msg.Number, uint16(0),
P_UID, msg.UserUuid, msg.DeviceId, nil)
if err2 != nil {
err2 = errors.New("UnregisterComponent.unregisterClient() Error:" + err2.Error())
fmt.Println(err2.Error())
return err2
}
err3 := sendResponse(clientConn.Conn, resp)
if err3 != nil {
err3 = errors.New("UnregisterComponent.unregisterClient() Error:, " + err3.Error())
fmt.Println(err3.Error())
return err3
}
return err
}
//offline broadcast
err = this.offlineBroadcast(buffPool, msg.UserUuid, msg.DeviceId)
if err != nil {
err = errors.New("UnregisterComponent.unregisterClient() Error: offlineBroadcast send failed, " + err.Error())
fmt.Println(err.Error())
}
resp, err := protocol.NewMessage(protocol.VERSION, protocol.MT_UNREGISTER, protocol.PT_PUSHSERVER, msg.Token, msg.Number, uint16(0),
P_UID, msg.UserUuid, msg.DeviceId, nil)
if err != nil {
err = errors.New("UnregisterComponent.unregisterClient() Error: conn is removed from BuffPool, but can not send response to service" + err.Error())
fmt.Println(err.Error())
return err
}
err2 := sendResponse(clientConn.Conn, resp)
if err2 != nil {
err2 = errors.New("UnregisterComponent.unregisterClient() Error:conn is reomved from BuffPool, but failed when send response to service" + err2.Error())
return err2
}
return nil
}
示例6: offlineBroadcast
func (this *UnregisterComponent) offlineBroadcast(buffPool *buffpool.BuffPool, userUuid string, devID uint32) error {
if GetRunWithSnode().Flag {
snode_component.GetNodeComponent().Logout(userID, devID)
snode_component.GetNodeComponent().DecUsersNumber()
snode_component.GetNodeComponent().DecConnNumber()
}
if buffPool == nil {
err := errors.New("UnRegisterComponent offlineBroadcast Error: invalid argument")
return err
}
var dispatcherOut *DispatcherOutComponent
for {
dispatcherOut = GetDispatcherOutComponent()
if dispatcherOut != nil {
break
} else {
fmt.Println("UnregisterComponent.offlineBroadcast() Error:can not get a dispatcher, will try again")
}
}
brMsg := protocol.BRMsg{
UserUuid: userUuid,
DeviceId: devID,
}
brData, err := brMsg.Encode()
if err != nil {
err := errors.New("UnregisterComponent.offlienBroadcast() Error: " + err.Error())
return err
}
servNoUuidArr := buffPool.GetAllServices()
for num, uuidArr := range servNoUuidArr {
for _, uuid := range uuidArr {
offlineMsg, err := protocol.NewMessage(protocol.VERSION, protocol.MT_BROADCAST_OFFLINE, protocol.PT_PUSHSERVER, 0, num, uint16(len(brData)),
P_UID, uuid, 0, brData)
if err != nil {
err = errors.New("UnregisterComponent offlineBroadcast() Error: " + err.Error())
return err
}
dispatcherOut.NPushBack(offlineMsg, false) //not need forward
}
}
return nil
}
示例7: onlineBroadcast
func (this *RegisterComponent) onlineBroadcast(buffPool *buffpool.BuffPool, dispatcherOut *DispatcherOutComponent, userUuid string, deviceID uint32) error {
if GetRunWithSnode().Flag {
onlineInfo := structure.ClientOnlineInfo{
PushID: this.PushID,
ParentID: this.PushID,
}
data, err := onlineInfo.Encode()
if err != nil {
err = errors.New("RegisterComponent onlineBroadcast() Error: " + err.Error())
return err
}
snode_component.GetNodeComponent().Forward(define.ONLINE, userUuid, deviceID, data)
snode_component.GetNodeComponent().AddUsersNumber()
snode_component.GetNodeComponent().AddConnNumber()
}
if buffPool == nil || dispatcherOut == nil {
err := errors.New("RegisterComponent onlineBroadcast() Error: invalid argument")
return err
}
brMsg := protocol.BRMsg{
userUuid,
deviceID,
}
brData, err := brMsg.Encode()
if err != nil {
err := errors.New("RegisterCompoent onlineBroadcast() Error: " + err.Error())
return err
}
servNoUuidArr := buffPool.GetAllServices()
for num, uuidArr := range servNoUuidArr {
for _, uuid := range uuidArr {
onlineMsg, err := protocol.NewMessage(protocol.VERSION, protocol.MT_BROADCAST_ONLINE, protocol.PT_PUSHSERVER, 0, num, uint16(len(brData)),
P_UID, uuid, 0, brData)
if err != nil {
err = errors.New("RegisterComponent onlineBroadcast() Error: " + err.Error())
return err
}
dispatcherOut.NPushBack(onlineMsg, false) //not need forward
}
}
return nil
}
示例8: checkAccountServer
func (this *RegisterComponent) checkAccountServer(buffPool *buffpool.BuffPool, message *protocol.Message, conn *net.Conn) bool {
if buffPool == nil || message == nil || conn == nil {
fmt.Println("RegisterComponent checkAccountServer() Error: invalid argument")
return false
}
connService := buffPool.GetService(protocol.N_ACCOUNT_SYS)
if connService == nil {
fmt.Println("RegisterComponent checkAccountServer() Error: can not get the account_sys server")
response, _ := protocol.NewMessage(protocol.VERSION, protoocl.MT_ERROR, protocol.PT_PUSHSERVER, message.Token, message.Number, 0, P_UID,
message.UserUuid, 0, nil)
err := sendResponse(conn, response)
if err != nil {
err = fmt.Errorf("RegisterComponent checkAccountServer() Error:%v", err)
fmt.Println(err.Error())
}
return false
}
return true
}
示例9: unregisterService
func (this *UnregisterComponent) unregisterService(buffPool *buffpool.BuffPool, msg *protocol.Message) error {
if buffPool == nil || msg == nil {
err := errors.New("UnregisterComponent unregisterService() Error: invalid argument")
return err
}
conn := buffPool.QueryService(msg.Number, msg.UserUuid)
if conn == nil {
return nil
}
//only remove conn from connPool, but do not disconnect this connection
err := buffPool.DeleteService(msg.Number, msg.UserUuid)
if err != nil {
err = errors.New("UnregisterComponent unregisterService() Error: " + err.Error())
//send failed response to client
resp, err2 := protocol.NewMessage(protocol.VERSION, protocol.MT_ERROR, protocol.PT_PUSHSERVER, msg.Token, msg.Number, uint16(0),
P_UID, msg.UserUuid, msg.DeviceId, nil)
if err2 != nil {
err2 = errors.New("UnregisterComponent.unregisterService() Error: " + err2.Error())
return err2
}
err3 := sendResponse(conn, resp)
if err3 != nil {
err3 = errors.New("UnregisterComponent.unregisterService() Error: " + err3.Error())
return err3
}
return err
}
resp, err := protocol.NewMessage(protocol.VERSION, protocol.MT_UNREGISTER, protocol.PT_PUSHSERVER, msg.Token, msg.Number, uint16(0),
P_UID, msg.UserUuid, msg.DeviceId, nil)
if err != nil {
err = errors.New("UnregisterComponent unregisterService() Error:conn is removed from BuffPool, but can not send response to service" + err.Error())
return err
}
err2 := sendResponse(conn, resp)
if err2 != nil {
err2 = errors.New("UnregisterComponent unregisterService() Error: conn is reomved from BuffPool, but failed when send response to service" + err2.Error())
return err2
}
return nil
}
示例10: sendToService
func (this *DispatcherOutComponent) sendToService(msg *protocol.Message, buffPool *buffpool.BuffPool) (*net.Conn, error) {
if msg == nil || buffPool == nil {
err := errors.New("DispatcherOutComponent.sendToService() Error: invalid argument")
fmt.Println(err.Error())
return nil, err
}
var conn *net.Conn
if msg.DestUuid == P_UID {
conn = buffPool.GetService(msg.Number)
} else {
conn = buffPool.QueryService(msg.Number, msg.DestUuid)
if conn == nil {
conn = buffPool.GetService(msg.Number)
}
}
if conn == nil {
err := errors.New("DispatcherOutComponent.sendToService() Error: can not get a server connection from buffpool")
fmt.Println(err.Error())
return nil, err
}
err := this.sendMessage(msg, conn)
if err != nil {
err = errors.New("DispatcherOutComponent.sendToService() Error: can not send message to server's connection")
fmt.Println(err.Error())
return conn, err
}
return conn, nil
}
示例11: sendToService2
func (this *DispatcherOutComponent) sendToService2(msg *protocol.Message, forward bool, buffPool *buffpool.BuffPool) (*net.Conn, error) {
if msg == nil || buffPool == nil {
err := errors.New("DispatcherOutComponent.sendToService() Error: invalid argument")
fmt.Println(err.Error())
return nil, err
}
var conn *net.Conn
if msg.DestUuid == P_UID {
conn = buffPool.GetService(msg.Number)
} else {
conn = buffPool.QueryService(msg.Number, msg.DestUuid)
if conn == nil && forward && GetRunWithSnode().Flag {
buf, err := msg.Encode()
if err != nil {
fmt.Println(err)
return nil, err
}
snode_component.GetNodeComponent().Forward(stree_define.FORWARD, msg.DestUuid, msg.DeviceId, buf)
return nil, nil
}
}
if conn == nil {
err := errors.New("DispatcherOutComponent.sendToService() Error: can not find dest server")
fmt.Println(err.Error())
return nil, err
}
err := this.sendMessage(msg, conn)
if err != nil {
err = errors.New("DispatcherOutComponent.sendToService() Error: can not send message to server's connection")
fmt.Println(err.Error())
return conn, err
}
if msg.MsgType == protocol.MT_PUBLISH {
fmt.Println("\n", time.Now().String(), "MT_PUBLISH: sendToService2 Directly OK", "\n")
}
return conn, nil
}
示例12: sendToClient2
func (this *DispatcherOutComponent) sendToClient2(msg *protocol.Message, forward bool, buffPool *buffpool.BuffPool) error {
if msg == nil || buffPool == nil {
err := errors.New("DispatcherOutComponent.sendToClient2() Error: invalid argument")
fmt.Println(err.Error())
return err
}
clientConnArr := make([]*model.Client, 0)
if msg.Token > 0 {
clientConn := buffPool.GetClient(msg.Number, msg.DestUuid, msg.DeviceId, msg.Token)
if clientConn != nil && clientConn.Conn != nil {
clientConnArr = append(clientConnArr, clientConn)
}
if (clientConnArr == nil || len(clientConnArr) <= 0) && forward && GetRunWithSnode().Flag {
buf, err := msg.Encode()
if err != nil {
fmt.Println("DispatcherOutComponent.sendToClient2() Error: " + err.Error())
fmt.Println("\n", time.Now().String(), "Not find user, not forward due to encode failed.", "\n")
return err
}
snode_component.GetNodeComponent().Forward(stree_define.FORWARD, msg.DestUuid, msg.DeviceId, buf)
return nil
}
} else if msg.Token == 0 && msg.DeviceId > 0 {
clientConnArr = buffPool.QueryForDeviceId(msg.Number, msg.DestUuid, msg.DeviceId)
if (clientConnArr == nil || len(clientConnArr) <= 0) && forward && GetRunWithSnode().Flag {
buf, err := msg.Encode()
if err != nil {
fmt.Println("DispatcherOutComponent.sendToClient2() Error:" + err.Error())
fmt.Println("\n", time.Now().String(), "Not find user, not forward due to encode failed.", "\n")
return err
}
snode_component.GetNodeComponent().Forward(stree_define.FORWARD, msg.DestUuid, msg.DeviceId, buf)
return nil
}
} else if msg.Token == 0 && msg.DeviceId == 0 {
clientConnArr = buffPool.QueryForUuid(msg.Number, msg.DestUuid)
}
err := errors.New("DispatcherOutComponent.sendToClient2() Error: the client's connection not pass authorization yet")
for _, client := range clientConnArr {
if client == nil || client.Conn == nil {
continue
}
if !this.checkConnActivity(client.Time, client, buffPool, msg.Number, msg.DestUuid, msg.DeviceId, msg.Token) {
err := errors.New("DispatcherOutComponent.sendToClient2() Error: client's connection is not alive")
fmt.Println(err.Error())
continue
}
err2 := this.sendMessage(msg, client.Conn)
if err2 != nil {
err2 = errors.New("DispatcherOutComponent.sendToClient2() Error: can not send message to client's connection")
fmt.Println(err.Error())
continue
}
}
return nil
}
示例13: handleClient
func (this *RegisterComponent) handleClient(buffPool *buffpool.BuffPool, queryComponent *QueryComponent,
message *protocol.Message, conn *net.Conn) {
if buffPool == nil || queryComponent == nil || message == nil || conn == nil || len(msg.Data) != DEVNO_LEN {
err := errors.New("RegisterComponent handleClient() Error : invaild argument")
return err
}
if !this.checkAccountServer(buffPool, message, conn) {
err := errors.New("RegisterCompoennt handleClient() Error: the account server is not register")
return err
}
clientConn := model.Client{Conn: conn, Status: false}
var token uint8
if message.Token == 0 {
token = buffPool.AddTmpClient(message.Number, message.UserUuid, string(message.Data), &clientConn)
} else {
token = buffPool.AddTmpClient2(message.Number, message.UserUuid, string(message.Data), message.DeviceId,
message.Token, &clientConn)
}
if token == 0 {
err := errors.New("RegisterCompoennt handleClient() Error: can not add the client to buffpool")
return err
}
queryInfo := protocol.QueryInfo{
Token: token,
Numebr: message.Number,
UserUuid: message.UserUuid,
DeviceNumber: string(message.Data),
}
err := queryComponent.AcceptInfo(queryInfo)
if err != nil {
err = fmt.Errorf("RegisterCompoennt handleClient() Error: %v", err)
return er
}
return nil
}
示例14: sendToClient
func (this *DispatcherOutComponent) sendToClient(msg *protocol.Message, buffPool *buffpool.BuffPool) error {
if msg == nil || buffPool == nil {
err := errors.New("DispatcherOutComponent.sendToClient() Error: invalid argument")
fmt.Println(err.Error())
return err
}
clientConnArr := make([]*model.Client, 0)
if msg.Token > 0 {
clientConn := buffPool.GetClient(msg.Number, msg.DestUuid, msg.DeviceId, msg.Token)
if clientConn != nil && clientConn.Conn != nil {
clientConnArr = append(clientConnArr, clientConn)
}
} else if msg.Token == 0 && msg.DeviceId > 0 {
clientConnArr = buffPool.QueryForDeviceId(msg.Number, msg.DestUuid, msg.DeviceId)
} else if msg.Token == 0 && msg.DeviceId == 0 {
clientConnArr = buffPool.QueryForUuid(msg.Number, msg.DestUuid)
}
if clientConnArr != nil && len(clientConnArr) <= 0 {
err := errors.New("DispatcherOutComponent.sendToClient() Error: can not find client's connection")
fmt.Println(err.Error())
return err
}
err := errors.New("DispatcherOutComponent.sendToClient() Error: the client's connection not pass authorization yet")
for _, client := range clientConnArr {
if client == nil || client.Conn == nil {
continue
}
if !this.checkConnActivity(client.Time, client, buffPool, msg.Number, msg.DestId, msg.DeviceId, msg.Token) {
err := errors.New("DispatcherOutComponent.sendToClient() Error: client's connection is not alive")
fmt.Println(err.Error())
continue
}
err2 := this.sendMessage(msg, client.Conn)
if err2 != nil {
err2 = errors.New("DispatcherOutComponent.sendToClient() Error: can not send message to client's connection")
fmt.Println(err.Error())
continue
}
}
return err
}
示例15: listenClient
func (this *RegisterComponent) listenClient(conn *net.Conn, buffPool *buffpool.BuffPool, number uint16, userUuid string, deviceID uint32, token uint8) error {
if buffPool == nil {
err := errors.New("RegisterComponent listenClient() Error: invalid argument")
fmt.Println(err.Error())
return err
}
if conn == nil {
err := errors.New("RegisterComponent listenClient() Error:invalid argument")
fmt.Println(err.Error())
fmt.Println("the invalid client connection will be removed")
err2 := buffPool.RemoveClient(number, userUuid, deviceID, token)
if err2 != nil {
err2 = errors.New("RegisterComponent listenClient() Error:remove the invalid client's connection failed" + err2.Error())
fmt.Println(err2.Error())
return err2
}
fmt.Println("the invalid client connection has been removed")
return err
}
var dispatcher *DispatcherComponent
for {
dispatcher = GetDispatcherComponent()
if dispatcher != nil {
break
} else {
fmt.Println("RegisterComponent listenClient() Error:can not get DispatcherComponent, will try again")
}
}
for {
msg, err := this.readMessage(conn)
if err != nil && err != io.EOF && conn != nil {
err = errors.New("RegisterComponent listenClient() Error " + err.Error())
fmt.Println(err.Error())
continue
} else if err != nil && (err == io.EOF || conn == nil) {
err = errors.New("RegisterComponent listenClient() Error the connection is invalid, " + err.Error())
err2 := buffPool.RemoveClient(number, userUuid, deviceID, token)
if conn != nil {
(*conn).Close()
}
if err2 != nil {
err2 = errors.New("RegisterComponent listenClient() Error: can not remove invalid client's connection, " + err2.Error())
fmt.Println(err2.Error())
return err2
}
clientArr := buffPool.QueryForDeviceId(number, userUuid, deviceID)
if clientArr == nil || len(clientArr) == 0 {
//offline broadcast
unregister := GetUnregisterComponent()
if unregister != nil {
err3 := unregister.offlineBroadcast(buffPool, userUuid, deviceID)
if err3 != nil {
err3 = errors.New("RegisterComponent listenClient() Error:one client disconnected but offlineBroadcast send failed, " + err3.Error())
fmt.Println(err3.Error())
}
} else {
fmt.Println("RegisterComponent listenClient() Error:one client disconnected but offlineBroadcast send failed")
}
}
return err
}
//push to DispatcherQueue
if msg == nil {
continue
}
dispatcher.NPushBack(msg, true) //need forward
}
return nil
}