Exemplo n.º 1
0
func (s *TcpServer) Exit() {

	s.eventDispatch.Close()
	s.stopChan <- false
	close(s.stopChan)
	elog.LogSys(" close new conn ch ")
	close(s.newConnCh)
	elog.LogSys(" end  new conn ch ")
	elog.LogSys("wait...")

	s.waitGroup.Wait()
	elog.Flush()

}
Exemplo n.º 2
0
func defErrorCallback(conn *Connection) {

	if conn.IsConnected() {
		elog.LogSys(" read msg error, close socket ")
		conn.Close()
	}

}
Exemplo n.º 3
0
func (tcpClient *TcpClient) onRead(e IEvent) {

	ev := e.(*ConnReadEvent)
	//在这里加上用户消息处理时间
	tcpClient.msgDispatcher.DispatchMsg(ev.conn, ev.id, ev.msg)
	elog.LogSys(" client read msg id :%d ", ev.id)

}
Exemplo n.º 4
0
func (s *TcpServer) onRead(e IEvent) {

	ev := e.(*ConnReadEvent)
	//在这里加上用户消息处理时间
	s.msgDispatcher.DispatchMsg(ev.conn, ev.id, ev.msg)
	elog.LogSys(" server  read msg id :%d ", ev.id)

}
Exemplo n.º 5
0
func (tcpClient *TcpClient) onClose(e IEvent) {

	ev := e.(*ConnCloseEvent)
	if tcpClient.closeCb != nil {
		tcpClient.closeCb(ev.conn)
	}
	elog.LogSys("cient close")
}
Exemplo n.º 6
0
func (tcpClient *TcpClient) Loop() {
	if !tcpClient.isConnection {
		elog.LogSys(" client not connevt server ")
	}

	tcpClient.eventDispatch.Loop()

}
Exemplo n.º 7
0
func (ed *EventDispatch) Close() {

	elog.LogSys("close event channel")

	for i := range ed.eventCh {
		close(ed.eventCh[i])
	}
}
Exemplo n.º 8
0
func (md *MsgDispatcher) DispatchMsg(conn *Connection, id int32, msg proto.Message) {

	if id >= MSG_MAX {
		return
	}

	elog.LogSys(" dispatcher msg :%d ", id)

	cb := md.cbMap[id]

	if cb == nil {

		elog.LogSys(" dispatcher msg :%d  not register cb ", id)
		return
	}

	cb(conn, msg)
}
Exemplo n.º 9
0
func (s *TcpServer) onClose(e IEvent) {

	ev := e.(*ConnCloseEvent)
	if s.closeCb != nil {
		s.closeCb(ev.conn)
	}
	s.DelConn(ev.conn)
	elog.LogSys("cient close")
}
Exemplo n.º 10
0
func (tcpClient *TcpClient) Close() {

	tcpClient.eventDispatch.Close()
	elog.LogSys(" close  conn  ")
	tcpClient.conn.Close()
	tcpClient.waitGroup.Wait()
	//关闭连接
	elog.Flush()

}
Exemplo n.º 11
0
func (s *TcpServer) Loop() {

	if s.isListen == false {

		elog.LogSys(" not listen, please first listen ")

		return
	}
	s.eventDispatch.Loop()
}
Exemplo n.º 12
0
func (a *Acceptor) loop(ch chan *Connection) {

	elog.LogSys(" acceptor is begin ")
	a.waitGroup.Add(1)
	defer a.waitGroup.Done()

	for {

		select {
		case <-a.stopChan:
			elog.LogSys(" recv stop chan ,stop Accept ")
			a.listener.Close()
			return
		default:

		}

		a.listener.SetDeadline(time.Now().Add(1e9))
		conn, err := a.listener.AcceptTCP()
		if err != nil {
			if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
				continue
			}
			elog.LogSysln(" accetp fail :", err)
			break
		}

		elog.LogSys("receive new conn")

		//newConn ,err := NewConn( conn, a.eventDispatch, a.waitGroup )

		a.eventDispatch.AddNewConnEvent(conn)

	}

	elog.LogSys(" acceptor is end ")
}
Exemplo n.º 13
0
func (e *EventDispatch) FreeEvent(ev IEvent) {

	et := ev.GetEventType()
	e.eventMu[et].Lock()
	ne := ev.Next()
	// make compliler happy
	if ne != nil {
		return
	}
	ne = e.eventList[et]
	e.eventList[et] = ev
	e.eventMu[et].Unlock()

	elog.LogSys(" free event type : %d", ev.GetEventType())
}
Exemplo n.º 14
0
func main() {

	elog.InitLog(elog.INFO)

	server := NewEsServer()

	ch := make(chan os.Signal)
	signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)

	server.Init()
	server.Run()

	fmt.Println(<-ch)

	server.tcpServer.Exit()

	elog.LogSys("Hhhhhh")

}
Exemplo n.º 15
0
func (s *TcpServer) onConn(e IEvent) {

	ev := e.(*ConnNewEvent)
	//控制数量
	if len(s.connMap) >= s.maxConn {
		ev.conn.Close()
		return
	}

	conn, _ := NewConn(ev.conn, s.eventDispatch, s.waitGroup)
	//设置消息解析器
	conn.setMsgParse(s.msgParse)
	//回调用户
	if s.connCb != nil {
		s.connCb(conn)
	}
	//hi, go
	conn.SetConnStatus(CONNECTED)
	conn.SetConnId(atomic.AddUint64(&s.maxConnId, 1))
	s.AddConn(conn)
	go conn.handleEvent()
	elog.LogSys("receive conn id: %s , total : %d", conn.connId, len(s.connMap))
}
Exemplo n.º 16
0
func (lmp *LiteMsgParse) parse(conn *Connection) (id int32, errNo int, msg proto.Message) {

	var len int32
	//Fixme  add little big endian cfg
	err := binary.Read(&conn.recvBuf, binary.LittleEndian, &len)
	if err != nil {
		elog.LogSysln("read  msg len  err : ", err)
		errNo = c_ErrLen
		return
	}

	elog.LogSysln(" parse  msg len : ", len)

	if len > c_MaxMsgLen || len < c_HeaderLen {
		elog.LogSysln(" msg len is  err : ")
		errNo = c_ErrLen
		return
	}

	if conn.recvBuf.Size() >= int(len-c_HeaderLen) {
		err = binary.Read(&conn.recvBuf, binary.LittleEndian, &id)
		if err != nil || id >= MSG_MAX {
			elog.LogSysln("read  msg id err : ", err)
			errNo = c_ErrMsgId
			return
		}
		elog.LogSysln(" parsr  msg id : ", id)

		/*info := GetMsgDispatcher().GetMsgInfo( int(id) )
		  if info == nil {
		  elog.LogSys("not register msg is : %d", id )
		  return c_ErrMsgId
		}*/

		// msg may be nil
		if (len - c_HeaderLen) > 0 {
			b := make([]byte, len-c_HeaderLen)
			err = binary.Read(&conn.recvBuf, binary.LittleEndian, &b)
			if err != nil {
				elog.LogSys("read  msg body fail ", id)
				errNo = c_ParseErr
				return
			}

			msg = lmp.msgFactory.createMsgById(id)
			elog.LogSysln(" create msg type ", reflect.TypeOf(msg))
			err = proto.Unmarshal(b, msg)
			if err != nil {
				elog.LogSys(" protobuf parse  msg body fail ", err)
				errNo = c_ParseErr
				return
			}
			errNo = c_NoError
			return
		}

	}
	elog.LogSysln(" recv msg len is big than buffer  ")
	errNo = c_NeedContinue
	id = 0
	return
}
Exemplo n.º 17
0
func (ed *EventDispatch) pushEvent(ev IEvent) {

	elog.LogSys("push %d event to event channel ", ev.GetEventType())
	ed.eventCh[ev.GetEventType()] <- ev
}