Beispiel #1
0
func (this *SNetListener) listenFunc() {
	GSLog.Mustln("listenFunc")
	vListener := this.M_Listener
	defer bsn_common.FuncGuard()
	defer func() {
		// GSLog.Debugln("send close before")

		GSLog.Debugln("close all connect")
		for vConn := range this.M_chanConn {
			vConn.Close()
		}
		// GSLog.Debugln("send close after")

		this.SNotifyClose.Close()
		this.Set(bsn_common.CState_Idle)
	}()

	this.Change(bsn_common.CState_Op, bsn_common.CState_Runing)

	vbQuit := false
	for !vbQuit {
		vConn, err := vListener.Accept()
		if err != nil {
			GSLog.Errorln(err)
			vbQuit = true
			continue
		}

		select {
		case this.M_chanConn <- vConn:
		case <-this.SNotifyClose.M_chanNotifyClose:
			vbQuit = true
		}
	}
}
Beispiel #2
0
func (this *SNetServer) runImp() {
	defer bsn_common.FuncGuard()
	defer func() {
		this.StopListen()

		this.M_INetServerImp.NetServerImpOnClose()
		this.SNotifyClose.Close()
		this.Set(bsn_common.CState_Idle)
	}()

	this.Change(bsn_common.CState_Op, bsn_common.CState_Runing)
	GSLog.Debugln("run imp")
	vbQuit := false
	for !vbQuit {
		vbQuit = true
		select {
		case vConn, ok := <-this.M_chanConn:
			if !ok {
				GSLog.Errorln("!ok")
				break
			}
			err := this.M_INetServerImp.NetServerImpAccept(vConn)
			if err != nil {
				GSLog.Errorln(err)
				vConn.Close()
				break
			}

			vbQuit = false
		case <-this.SNotifyClose.M_chanNotifyClose:
			GSLog.Mustln("receive a notify to close")
		}
	}
}
Beispiel #3
0
func (this *SClientUser) runImp() {
	defer bsn_common.FuncGuard()
	defer func() {
		GSLog.Debugln("close connect")
		this.Conn().Close()

		GSLog.Debugln("delete from user mgr")
		this.M_SClientUserMgr.delClient(this.Id())

		this.SNotifyClose.Close()
		this.Set(bsn_common.CState_Idle)
		GSLog.Debugln("close ok")
	}()

	this.Change(bsn_common.CState_Op, bsn_common.CState_Runing)
	for {
		err := this.RecvMsg()
		if err != nil {
			GSLog.Errorln(err)
			break
		}

		err = this.procMsg()
		if err != nil {
			GSLog.Errorln(err)
			break
		}
	}
}
Beispiel #4
0
func (this *SNetConnecter) runImp() {
	defer bsn_common.FuncGuard()
	defer func() {
		GSLog.Debugln("close connect")
		this.Conn().Close()
		this.SetConn(nil)

		this.M_INetConnecterImp.NetConnecterImpOnClose()
		this.SNotifyClose.Close()
		this.Set(bsn_common.CState_Idle)
		GSLog.Debugln("close ok")
	}()

	this.Change(bsn_common.CState_Op, bsn_common.CState_Runing)
	GSLog.Debugln("run imp")
	vbQuit := false
	for !vbQuit {
		err := this.M_INetConnecterImp.NetConnecterImpRun()
		if err != nil {
			GSLog.Errorln(err)
			vbQuit = true
			break
		}

		select {
		case <-this.SNotifyClose.M_chanNotifyClose:
			GSLog.Mustln("receive a notify to close")
			vbQuit = true
		default:
		}
	}
}
Beispiel #5
0
Datei: log.go Projekt: bsn069/go
func (this *SLog) Debugf(format string, v ...interface{}) {
	defer this.M_Mutex.Unlock()
	this.M_Mutex.Lock()
	defer bsn_common.FuncGuard()
	this.M_depth++
	strInfo := fmt.Sprintf(format, v...)
	this.output(bsn_common.ELogLevel_Debug, strInfo)
}
Beispiel #6
0
Datei: log.go Projekt: bsn069/go
func (this *SLog) Must(v ...interface{}) {
	defer this.M_Mutex.Unlock()
	this.M_Mutex.Lock()
	defer bsn_common.FuncGuard()
	this.M_depth++
	strInfo := fmt.Sprint(v...)
	this.output(bsn_common.ELogLevel_Must, strInfo)
}
Beispiel #7
0
func (this *SUserMgr) runImp() {
	defer bsn_common.FuncGuard()
	defer func() {
		GSLog.Debugln("on closing")
		this.M_bRun = false

		GSLog.Debugln("close listen")
		this.StopListen()

		GSLog.Debugln("close all not process connect")
		var vbQuit bool = false
		for !vbQuit {
			select {
			case conn := <-this.SListen.M_chanConn:
				conn.Close()
			default:
				vbQuit = true
			}
		}

		GSLog.Debugln("close all user")
		for _, vIUser := range this.M_TId2User {
			vIUser.Close()
		}

		GSLog.Debugln("send notify to wait close chan")
		select {
		case <-this.M_chanWaitClose:
		default:
		}
		this.M_chanWaitClose <- true

		GSLog.Debugln("close all ok")
	}()

	GSLog.Debugln("run imp")
	var vbQuit bool = false
	for !vbQuit {
		vbQuit = true
		select {
		case vConn, ok := <-this.SListen.M_chanConn:
			if !ok {
				GSLog.Errorln("!ok")
				break
			}
			err := this.M_TGateFuncOnNewUser(this.M_Imp, vConn)
			if err != nil {
				GSLog.Errorln(err)
				vConn.Close()
				break
			}

			vbQuit = false
		case <-this.M_chanNotifyClose:
			GSLog.Mustln("receive a notify to close")
		}
	}
}
Beispiel #8
0
Datei: log.go Projekt: bsn069/go
func (this *SLog) Debugln(v ...interface{}) {
	defer this.M_Mutex.Unlock()
	this.M_Mutex.Lock()
	defer bsn_common.FuncGuard()
	this.M_depth++
	v = append(v, "\n")
	strInfo := fmt.Sprint(v...)
	this.output(bsn_common.ELogLevel_Debug, strInfo)
}
Beispiel #9
0
func (this *SClientUser) runImp() {
	defer bsn_common.FuncGuard()
	defer func() {
		GSLog.Debugln("on closing")

		GSLog.Debugln("close connect")
		this.Conn().Close()

		GSLog.Debugln("close from user mgr")
		this.M_SClientUserMgr.delClient(this.Id())

		this.SNotifyClose.Close()
		this.Set(bsn_common.CState_Idle)
		GSLog.Debugln("close ok")
	}()

	this.Change(bsn_common.CState_Op, bsn_common.CState_Runing)
	for {
		GSLog.Debugln("read msg header")
		byMsgHeader := this.M_byRecvBuff[0:bsn_msg.CSMsgHeader_Size]
		err := this.Recv(byMsgHeader)
		if err != nil {
			GSLog.Errorln(err)
			break
		}
		GSLog.Debugln("recv byMsgHeader= ", byMsgHeader)

		this.M_SMsgHeader.DeSerialize(byMsgHeader)
		GSLog.Debugln("recv this.M_SMsgHeader= ", this.M_SMsgHeader)

		vTotalLen := int(this.M_SMsgHeader.Len())
		if vTotalLen > cap(this.M_byRecvBuff) {
			// realloc recv buffer
			this.M_byRecvBuff = make([]byte, vTotalLen)
		}

		GSLog.Debugln("read this.M_by2MsgBody")
		this.M_by2MsgBody = this.M_byRecvBuff[0:vTotalLen]
		if vTotalLen > 0 {
			err = this.Recv(this.M_by2MsgBody)
			if err != nil {
				GSLog.Errorln(err)
				break
			}
			GSLog.Debugln("recv this.M_by2MsgBody= ", this.M_by2MsgBody)
		}

		err = this.procMsg()
		if err != nil {
			GSLog.Errorln(err)
			break
		}
	}
}
Beispiel #10
0
Datei: log.go Projekt: bsn069/go
func (this *SLog) output(level bsn_common.TLogLevel, strInfo string) {
	defer bsn_common.FuncGuard()
	fl_u32Id++
	this.M_depth++
	this.M_time = time.Now()
	strTime := this.M_timeFmtFunc(&this.M_time)
	strDebugInfo := this.M_debugFmtFunc(this.M_depth)
	if (this.M_u32OutMask & uint32(level)) != 0 {
		strOutInfo := this.M_outFmtFunc(level, &strTime, &strInfo, &strDebugInfo, fl_u32Id)
		fmt.Print(strOutInfo)
	}
	if (this.M_u32LogMask & uint32(level)) != 0 {

	}
	this.M_depth = 0
}
Beispiel #11
0
func (this *SClientUser) runImp() {
	defer bsn_common.FuncGuard()
	defer func() {
		this.Close()
		this.UserMgr().DelUser(this)
	}()

	vSServerUserMgr := this.UserMgr().Gate().GetServerMgr()
	var vMsg bsn_msg.SMsgHeaderGateClient
	for !this.M_bClose {
		err := this.ReadMsgHeader()
		if err != nil {
			GSLog.Errorln(err)
			break
		}
		GSLog.Debugln("recv this.M_byMsgHeader= ", this.M_byMsgHeader)
		vMsg.DeSerialize(this.M_byMsgHeader)
		GSLog.Debugln("recv vMsg= ", vMsg)

		vLen := vMsg.Len()
		if vLen < bsn_msg.CSMsgHeader_Size {
			GSLog.Errorln("error size")
			break
		}

		this.M_byMsgBody = make([]byte, vLen)
		err = this.ReadMsgBody()
		if err != nil {
			GSLog.Errorln(err)
			return
		}

		GSLog.Debugln("recv this.M_byMsgBody= ", this.M_byMsgBody)
		if vMsg.UserId() == 0 {
			vSMsgHeader := bsn_msg.NewMsgHeaderFromBytes(this.M_byMsgBody)
			GSLog.Debugln("recv vSMsgHeader= ", vSMsgHeader)
			GSLog.Debugln("recv msg= ", string(this.M_byMsgBody[int(unsafe.Sizeof(*vSMsgHeader)):]))
		} else {
			err = vSServerUserMgr.SendMsgToUser(vMsg.UserId(), this.M_byMsgBody)
			if err != nil {
				GSLog.Errorln("process msg err: ", vMsg.Type(), err)
			}
		}
	}
}
Beispiel #12
0
func (this *STCPRecver) workerTCPRecver() {
	defer bsn_common.FuncGuard()

	byMsgHeader := make([]byte, bsn_msg.CSMsgHeader_Size)
	readLen := 0
	var err error
	for {
		readLen, err = io.ReadFull(this.M_Conn, byMsgHeader)
		if err != nil {
			break
		}
		if readLen != int(bsn_msg.CSMsgHeader_Size) {
			err = errors.New("not read all header data")
			break
		}

		vSMsg := bsn_msg.NewSMsg()
		vSMsg.Init(byMsgHeader)
		byMsgBody := vSMsg.MsgBodyBuffer()

		readLen, err = io.ReadFull(this.M_Conn, byMsgBody)
		if err != nil {
			break
		}
		if readLen != int(vSMsg.Len()) {
			err = errors.New("not read all body data")
			break
		}

		err = this.M_IRecverCB.RecverCBOnMsg(vSMsg)
		if err != nil {
			break
		}
	}

	if err != nil {
		GSLog.Debugln("err=", err)
	}

	this.M_Conn.Close()
	this.M_Conn = nil

	this.M_IRecverCB.RecverCBOnClose()
}
Beispiel #13
0
func (this *SServerUser) runImp() {
	defer bsn_common.FuncGuard()
	defer func() {
		this.Close()
		this.UserMgr().DelUser(this)
	}()

	vSClientUserMgr := this.UserMgr().Gate().GetClientMgr()
	var vMsg bsn_msg.SMsgHeaderGateServer
	for !this.M_bClose {
		err := this.ReadMsgHeader()
		if err != nil {
			GSLog.Errorln(err)
			return
		}
		vMsg.DeSerialize(this.M_byMsgHeader)

		vLen := vMsg.Len()
		this.M_byMsgBody = make([]byte, vLen)
		if vLen > 0 {
			err = this.ReadMsgBody()
			if err != nil {
				GSLog.Errorln(err)
				return
			}
		}

		switch vMsg.ServerMsgType() {
		case bsn_msg.CServerMsgType_Reg:
			err = this.msg_reg(&vMsg)
		case bsn_msg.CServerMsgType_ToUser:
			err = vSClientUserMgr.SendMsgToUser(vMsg.UserId(), this.M_byMsgBody)
		default:
			err = errors.New("unknown msg type " + strconv.Itoa(int(vMsg.Type())))
		}

		if err != nil {
			GSLog.Errorln("process msg err: ", vMsg.Type(), err)
		}
	}
}
Beispiel #14
0
func (this *STCPListener) workerTCPListen() {
	defer bsn_common.FuncGuard()
	defer func() {
		this.M_SNotifyClose.Close()
		this.M_Listener = nil
		this.M_SState.Set(bsn_common.CState_Idle)
		this.M_IListenerCB.ListenerCBOnClose()
	}()

	this.M_SState.Change(bsn_common.CState_Op, bsn_common.CState_Runing)

	vbQuit := false
	for !vbQuit {
		vConn, err := this.M_Listener.Accept()
		if err != nil {
			GSLog.Errorln("11111", err)
			vbQuit = true
			continue
		}
		this.onTCPClient(vConn)
	}
}