Ejemplo n.º 1
0
func (lmp *LiteMsgParse) Decode(conn *Connection, id int32, msg proto.Message) error {

	var err error
	err = binary.Write(&conn.sendBuf, binary.LittleEndian, &id)
	if err != nil {
		elog.LogSysln(" write id  error", err)
		return err
	}

	var buf []byte
	buf, err = proto.Marshal(msg)
	if err != nil {
		elog.LogSysln(" parse msg body  error", err)
		return err
	}
	err = binary.Write(&conn.sendBuf, binary.LittleEndian, int32(len(buf)))
	if err != nil {
		elog.LogSysln(" write msg len error", err)
		return err
	}
	err = binary.Write(&conn.sendBuf, binary.LittleEndian, buf)
	if err != nil {
		elog.LogSysln(" write msg body error", err)
		return err
	}
	return err
}
Ejemplo n.º 2
0
func (tcpClient *TcpClient) Connect(addr string) error {

	var err error
	tcpClient.tcpAddr, err = net.ResolveTCPAddr("tcp", addr)
	if err != nil {
		elog.LogSysln(addr, ":resolve tcp addr fail, please usage: 0.0.0.1:2345, fail: ", err)
		return err
	}

	var localaddr *net.TCPAddr
	localaddr, err = net.ResolveTCPAddr("tcp", "0.0.0.0")
	conn, err := net.DialTCP("tcp", localaddr, tcpClient.tcpAddr)
	if err != nil {
		elog.LogSysln("connect server , because :", err)
		return err
	}

	tcpClient.isConnection = true
	tcpClient.conn, err = NewConn(conn, tcpClient.eventDispatch, tcpClient.waitGroup)

	if tcpClient.connCb != nil {
		tcpClient.connCb(tcpClient.conn)
	}
	//开始处理网络
	go tcpClient.conn.handleEvent()

	return err
}
Ejemplo n.º 3
0
func (s *TcpServer) Listen(addr string) (err error) {

	s.tcpAddr, err = net.ResolveTCPAddr("tcp", addr)
	if err != nil {
		elog.LogSysln(addr, ":resolve tcp addr fail, please usage: 0.0.0.1:2345, fail: ", err)
		return
	}

	s.acceptor.listener, err = net.ListenTCP("tcp", s.tcpAddr)
	if err != nil {
		elog.LogSysln("listen tcp fail, because :", err)
		return
	}

	s.isListen = true
	// main socket process
	go s.acceptor.loop(s.newConnCh)

	return
}
Ejemplo n.º 4
0
func (lmp *LiteMsgParse) Encode(conn *Connection) error {

	// let go ...
	var err error
	elog.LogSysln("recv msg :", conn.recvBuf.Size())
	for conn.recvBuf.Size() >= int(c_MinMsgLen) {

		id, ret, msg := lmp.parse(conn)
		elog.LogSysln(" parse msg type ", reflect.TypeOf(msg))
		if ret == c_NoError {
			//call back
			lmp.msgCb(conn, id, msg)
		} else if ret == c_NeedContinue {
			return err
		} else {
			lmp.errCb(conn)
			err = errors.New("read err ")
			return err
		}
	}
	return err
}
Ejemplo n.º 5
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 ")
}
Ejemplo n.º 6
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
}
Ejemplo n.º 7
0
func onMsg(es *es.Connection, pb proto.Message) {

	msg := pb.(*netpb.NetMsg)

	elog.LogSysln("i receieve msg ", msg.M_ID, reflect.TypeOf(pb))
}