Exemple #1
0
func (this *SignalHandler) ProcessSignal() {
	logger.Info("(this *SignalHandler) ProcessSignal()")
	for {
		select {
		case s := <-this.sc:
			logger.Info("-------->receive UnHandle Signal:", s)
			this.lock.RLock()
			defer this.lock.RUnlock()
			if v, ok := this.mh[s]; ok {
				for hk, hv := range v {
					hk.Process(s, hv)
				}
			} else {
				logger.Info("-------->receive UnHandle Signal:", s)
			}
		}
	}
}
Exemple #2
0
func (c *Configuration) Init() error {
	for _, str := range c.SrvInfo.Banner {
		logger.Info(str)
	}

	for i := 0; i < len(c.IoServices); i++ {
		c.IoServices[i].Init()
	}
	return nil
}
Exemple #3
0
func LoadPackages(configFile string) {
	data, err := ioutil.ReadFile(configFile)
	if err != nil {
		logger.Errorf("Error while reading config file %s: %s", configFile, err)
	}

	switch path.Ext(configFile) {
	case ".json":
		// Compact JSON to make it easier to extract JSON per package
		var buf bytes.Buffer
		err = json.Compact(&buf, data)
		if err != nil {
			logger.Errorf("Error in JSON config file %s: %s", configFile, err)
		}
		data = buf.Bytes()

		// Unmarshal packages /*in given order*/
		for _, pkg := range packages {
			// Extract JSON only for this package
			key := []byte(`"` + pkg.Name() + `":{`)
			begin := bytes.Index(data, key)
			if begin != -1 {
				begin += len(key) - 1
				end := 0
				braceCounter := 0
				for i := begin; i < len(data); i++ {
					switch data[i] {
					case '{':
						braceCounter++
					case '}':
						braceCounter--
					}
					if braceCounter == 0 {
						end = i + 1
						break
					}
				}

				err = json.Unmarshal(data[begin:end], pkg)
				if err != nil {
					logger.Errorf("Error while unmarshalling JSON from config file %s: %s", configFile, err)
				}
			}
			err := pkg.Init()
			if err != nil {
				logger.Errorf("Error while initializing package %s: %s", pkg.Name(), err)
			} else {
				logger.Info("module [", pkg.Name(), "] load success")
			}
		}

	default:
		panic("Unsupported config file: " + configFile)
	}
}
Exemple #4
0
func (a *TcpAcceptor) start() (err error) {
	service := a.sc.Ip + ":" + strconv.Itoa(int(a.sc.Port))
	a.listener, err = net.Listen("tcp", service)
	if err != nil {
		logger.Error(err)
		return err
	}
	logger.Info(a.sc.Name, " listen at ", a.listener.Addr().String())

	go a.acceptRoutine()
	go a.sessionRoutine()

	return nil
}
Exemple #5
0
func (t *openTimer) OnTimer(h timer.TimerHandle, ud interface{}) bool {

	if StartCnt >= Config.Count {

	} else {
		logger.Info("Start ", StartCnt, " Times Connect")
		cfg := Config.Connects
		cfg.Id = cfg.Id - StartCnt
		netlib.Connect(&cfg)
		StartCnt = StartCnt + 1
	}

	return true
}
Exemple #6
0
func (this *MulticastPacketFactory) CreateMulticastPacket(data interface{}, sis ...*protocol.MCSessionUnion) proto.Message {
	pack := &protocol.SSPacketMulticast{Sessions: sis}
	if byteData, ok := data.([]byte); ok {
		pack.Data = byteData
	} else {
		byteData, err := netlib.MarshalPacket(data)
		if err == nil {
			pack.Data = byteData
		} else {
			logger.Info("MulticastPacketFactory.CreateMulticastPacket err:", err)
		}
	}
	proto.SetDefaults(pack)
	return pack
}
Exemple #7
0
func ContructTxResultPacket(parent, me *transact.TransNodeParam, tr *transact.TransResult) proto.Message {
	packet := &protocol.TransactResult{
		MyTId:    proto.Int64(int64(parent.TId)),
		ChildTId: proto.Int64(int64(me.TId)),
		RetCode:  proto.Int32(int32(tr.RetCode)),
	}
	if tr.RetFiels != nil {
		b, err := netlib.MarshalPacketNoPackId(tr.RetFiels)
		if err != nil {
			logger.Info("ContructTxResultPacket Marshal UserData error:", err)
		} else {
			packet.CustomData = b
		}
	}
	proto.SetDefaults(packet)
	return packet
}
Exemple #8
0
func (t *Task) run() (e error) {
	defer utils.DumpStackIfPanic("Task::run")

	t.tStart = time.Now()
	ret := t.c.Call()

	if t.r != nil {
		t.r <- ret
	}

	if t.n != nil {
		SendTaskRes(t.s, t)
	}
	if t.name != "" {
		tNow := time.Now()
		logger.Info("task [", t.name, "] since createTime(", tNow.Sub(t.tCreate), ") since startTime(", tNow.Sub(t.tStart), ")")
	}
	return nil
}
Exemple #9
0
func (this *Configuration) Init() error {
	this.Connects.Init()
	f := this.Connects.GetFilter(filter.AuthenticationFilterName)
	if f != nil {
		f.(*filter.AuthenticationFilter).SessionAuthHandler = func(s *netlib.Session, bSuc bool) {
			if bSuc {
				packet := &protocol.CSPacketPing{
					TimeStamb: proto.Int64(time.Now().Unix()),
					Message:   []byte("=1234567890abcderghijklmnopqrstuvwxyz="),
				}
				//for i := 0; i < 1024*32; i++ {
				//	packet.Message = append(packet.Message, byte('x'))
				//}
				proto.SetDefaults(packet)
				s.Send(packet)
			} else {
				logger.Info("SessionAuthHandler auth failed")
			}
		}
	}
	return nil
}
Exemple #10
0
func ContructTxStartPacket(parent, me *transact.TransNodeParam, ud interface{}) proto.Message {
	packet := &protocol.TransactStart{
		MyTNP: &protocol.TransactParam{
			TransNodeID:     proto.Int64(int64(me.TId)),
			TransType:       proto.Int32(int32(me.Tt)),
			OwnerType:       proto.Int32(int32(me.Ot)),
			TransCommitType: proto.Int32(int32(me.Tct)),
			OwnerID:         proto.Int32(int32(me.Oid)),
			SkeletonID:      proto.Int32(int32(me.SkeletonID)),
			LevelNo:         proto.Int32(int32(me.LevelNo)),
			AreaID:          proto.Int32(int32(me.AreaID)),
			TimeOut:         proto.Int64(int64(me.TimeOut)),
		},
		ParenTNP: &protocol.TransactParam{
			TransNodeID:     proto.Int64(int64(parent.TId)),
			TransType:       proto.Int32(int32(parent.Tt)),
			OwnerType:       proto.Int32(int32(parent.Ot)),
			TransCommitType: proto.Int32(int32(parent.Tct)),
			OwnerID:         proto.Int32(int32(parent.Oid)),
			SkeletonID:      proto.Int32(int32(parent.SkeletonID)),
			LevelNo:         proto.Int32(int32(parent.LevelNo)),
			AreaID:          proto.Int32(int32(parent.AreaID)),
			TimeOut:         proto.Int64(int64(parent.TimeOut)),
		},
	}

	if ud != nil {
		b, err := netlib.MarshalPacketNoPackId(ud)
		if err != nil {
			logger.Info("ContructTxStartPacket Marshal UserData error:", err)
		} else {
			packet.CustomData = b
		}
	}
	proto.SetDefaults(packet)
	return packet
}
Exemple #11
0
func (c *WsConnector) dump() {
	logger.Info("=========wsconnector dump maxDone=", c.maxDone)
	logger.Info("=========wssession recvBuffer size=", len(c.s.recvBuffer), " sendBuffer size=", len(c.s.sendBuffer))
}
Exemple #12
0
func (a *TcpAcceptor) dump() {
	logger.Info("=========accept dump maxSessions=", a.maxActive, " maxDone=", a.maxDone)
	for sid, s := range a.mapSessions {
		logger.Info("=========session:", sid, " recvBuffer size=", len(s.recvBuffer), " sendBuffer size=", len(s.sendBuffer))
	}
}
Exemple #13
0
func (ish *KillSignalHandler) Process(s os.Signal, ud interface{}) error {
	logger.Info("Receive Kill signal, process be close")
	module.Stop()
	return nil
}
Exemple #14
0
func (ish *InterruptSignalHandler) Process(s os.Signal, ud interface{}) error {
	logger.Info("Receive Interrupt signal, process start quit.")
	module.Stop()
	return nil
}
Exemple #15
0
func (s *WsSession) recvRoutine() {
	defer func() {
		if err := recover(); err != nil {
			logger.Trace(s.Id, " ->close: Session.procRecv err: ", err)
		}
		s.sc.decoder.FinishDecode(&s.Session)
		s.shutRead()
		s.Close()
	}()

	s.waitor.Add(1)
	defer s.waitor.Done()

	s.conn.SetReadLimit(int64(s.sc.MaxPacket))
	var (
		pck      interface{}
		packetid int
		raw      []byte
	)

	for {
		if s.sc.IsInnerLink {
			var timeZero time.Time
			s.conn.SetReadDeadline(timeZero)
		} else {
			if s.sc.ReadTimeout != 0 {
				s.conn.SetReadDeadline(time.Now().Add(s.sc.ReadTimeout))
			}
		}
		op, r, err := s.conn.NextReader()
		if err != nil {
			logger.Info("s.conn.NextReader err:", err)
			panic(err)
		}
		switch op {
		case websocket.BinaryMessage:
			packetid, pck, err, raw = s.sc.decoder.Decode(&s.Session, r)
			if err != nil {
				bUnproc := true
				bPackErr := false
				if _, ok := err.(*UnparsePacketTypeErr); ok {
					bPackErr = true
					if s.sc.eph != nil && s.sc.eph.OnErrorPacket(&s.Session, packetid, raw) {
						bUnproc = false
					}
				}
				if bUnproc {
					logger.Error("s.sc.decoder.Decode err ", err)
					if s.sc.IsInnerLink == false {
						panic(err)
					} else if !bPackErr {
						panic(err)
					}
				}
			}
			if pck != nil {
				if s.FirePacketReceived(packetid, pck) {
					act := AllocAction()
					act.s = &s.Session
					act.p = pck
					act.packid = packetid
					act.n = "packet:" + strconv.Itoa(packetid)
					s.recvBuffer <- act
				}
			}
			s.lastRcvTime = time.Now()
		}
	}
}
Exemple #16
0
func (sft *SessionFilterTrace) dump() {
	if time.Now().Sub(sft.dumpTime) >= time.Minute*5 {
		logger.Info("Session per five minuts: recvCntPerSec=", sft.recvCntPerSec, " sendCntPerSec=", sft.sendCntPerSec)
		sft.dumpTime = time.Now()
	}
}