示例#1
0
func (a *WsAcceptor) start() (err error) {
	http.Handle(a.sc.Path, http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
		defer utils.DumpStackIfPanic("ws.HandlerFunc")
		if req.Method != "GET" {
			http.Error(res, "method not allowed", 405)
			return
		}
		ws, err := a.upgrader.Upgrade(res, req, nil)
		if _, ok := err.(websocket.HandshakeError); ok {
			http.Error(res, "Not a websocket handshake", 400)
			return
		} else if err != nil {
			http.Error(res, fmt.Sprintf("%v", err), 500)
			logger.Error(err)
			return
		}
		ws.SetPongHandler(func(string) error {
			ws.SetReadDeadline(time.Now().Add(readWait))
			return nil
		})
		s := newWsSession(a.idGen.NextId(), ws, a.sc, a)
		s.FireConnectEvent()
		s.start()
		a.acptChan <- s
	}))
	go func() {
		service := a.sc.Ip + ":" + strconv.Itoa(int(a.sc.Port))
		err := http.ListenAndServe(service, nil)
		if err != nil {
			logger.Error(err)
		}
	}()

	return nil
}
示例#2
0
文件: panic.go 项目: zwczou/goserver
func DumpStackIfPanic(f string) {
	if err := recover(); err != nil {
		logger.Error(f, " panic,error=", err)
		var buf [4096]byte
		len := runtime.Stack(buf[:], false)
		logger.Error("stack--->", string(buf[:len]))
	}
}
示例#3
0
func Stop() {
	AppModule.Close()
	err := core.ExecuteHook(core.HOOK_AFTER_STOP)
	if err != nil {
		logger.Error("ExecuteHook(HOOK_BEFORE_START) error", err)
	}
}
示例#4
0
func Start() *utils.Waitor {
	err := core.ExecuteHook(core.HOOK_BEFORE_START)
	if err != nil {
		logger.Error("ExecuteHook(HOOK_BEFORE_START) error", err)
	}
	return AppModule.Start()
}
示例#5
0
func sendTaskReqToExecutor(t *Task, name string) bool {
	if t == nil {
		return false
	}
	if t.n != nil && t.s == nil {
		logger.Error(name, " You must specify the source object task.")
		return false
	}
	return TaskExecutor.SendCommand(&taskReqCommand{t: t, n: name}, true)
}
示例#6
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
}
示例#7
0
func (e *NetEngine) Init() {
	var err error
	for i := 0; i < len(Config.IoServices); i++ {
		s := e.newIoService(&Config.IoServices[i])
		if s != nil {
			e.pool[Config.IoServices[i].Id] = s
			err = s.start()
			if err != nil {
				logger.Error(err)
			}
		}
	}

	time.AfterFunc(time.Minute*5, func() { e.dump() })
}
示例#8
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()
		}
	}
}