Beispiel #1
0
func Init() {
	initparse.DefaultParse("./config.cfg")
	if section, ok := initparse.GetSection("Auth"); ok {
		accountId = section.GetIntValue("accountId")
		accountSession, _ = section.GetValue("accountSession")
		log.Debugf("Load [auth] config accountId [%d] accountSession [%s]", accountId, accountSession)
	}
	if section, ok := initparse.GetSection("Server"); ok {
		server_url, _ = section.GetValue("server_url")
		log.Debugf("Load [Server] config server_url [%s]", server_url)
	}
}
Beispiel #2
0
//global auth
func Auth() {
	log.Debugf("start init")
	err := errors.New("")
	conn, err = net.Dial("tcp", server_url)
	if err != nil {
		fmt.Println("conn game server failed")
		panic(err.Error())
	}
	conn.SetDeadline(time.Now().Add(time.Minute * 30))
	var frbody FRBODYEX
	// Auth
	frbody.Request = "auth"
	frbody.Param = make([]int, 1)
	frbody.Param[0] = accountId
	frbody.Ex = make([]string, 1)
	frbody.Ex[0] = accountSession
	rbytes, _ := json.Marshal(frbody)

	clientSendBody(accountId, string(rbytes))
	recvbody, err := clientRecvBody(accountId)
	//<-time.After(time.Second * 5)
	log.Debugf("Auth result [%+v]", recvbody)
}
Beispiel #3
0
func clientSendBody(accountId int, sendbody string) {
	buf := new(bytes.Buffer)
	w := gzip.NewWriter(buf)
	if _, err := w.Write([]byte(sendbody)); err != nil {
		panic(err.Error())
	}
	if err := w.Close(); err != nil {
		panic(err.Error())
	}
	body := string(buf.Bytes())

	log.Debugf("%d <<< sending %d->%d body:\n%s\n", accountId, len(sendbody), len(body), sendbody)

	var retHead FRHEADER
	retHead.ContentSize = uint32(len(sendbody))
	retHead.Sign = FRHEADERSIGN
	retHead.Version = 0x00000001
	retHead.Bodysize = uint32(len(body))

	retHeadBuf := bytes.NewBuffer(nil)
	binary.Write(retHeadBuf, binary.LittleEndian, &retHead)

	//log.Debugf("<<< sending head: %v, headBuf: %v\n", retHead, *retHeadBuf)
	conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
	n, err := conn.Write(retHeadBuf.Bytes())
	if err != nil || n != len(retHeadBuf.Bytes()) {
		conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
		n, err := conn.Write(retHeadBuf.Bytes())
		if err != nil || n != len(retHeadBuf.Bytes()) {
			panic(err.Error())
		}
	}

	conn.SetWriteDeadline(time.Now().Add(30 * time.Second))
	n, err = conn.Write([]byte(body))
	if err != nil || n != len([]byte(body)) {
		conn.SetWriteDeadline(time.Now().Add(30 * time.Second))
		n, err := conn.Write([]byte(body))
		if err != nil || n != len([]byte(body)) {
			panic(err.Error())
		}
	}
}
Beispiel #4
0
func clientRecvBody(accountId int) (body string, err error) {
	var header FRHEADER
	packetbuf := make([]byte, 16)
	conn.SetReadDeadline(time.Now().Add(60 * time.Second))
	n, err := conn.Read(packetbuf)
	if err != nil {
		log.Debugf("read header error %s \n", err.Error())
		return
	}

	buf := bytes.NewBuffer(packetbuf[0:n])
	binary.Read(buf, binary.LittleEndian, &header)

	if header.Sign != FRHEADERSIGN {
		err = errors.New("header sign not match, disconnect")
		log.Debug("header sign not match, disconnect")
		return
	}

	bytesExpected := int(header.Bodysize)
	bodyBytes := make([]byte, 0)
	bytesRead := 0

	for bytesRead < bytesExpected {
		bytes := make([]byte, bytesExpected-bytesRead)

		conn.SetReadDeadline(time.Now().Add(5 * time.Second))
		n, err = conn.Read(bytes)
		if err != nil {
			log.Debugf("read body error %s \n", err.Error())
			return
		}
		bytesRead += n
		bodyBytes = append(bodyBytes, bytes[:n]...)
	}

	body = string(bodyBytes)

	{
		buf := new(bytes.Buffer)
		if _, e := buf.WriteString(body); e != nil {
			err = errors.New("received body gzip failed")
			log.Debug("received body gzip failed")
			return
		}

		r, e := gzip.NewReader(buf)
		if e != nil {
			err = errors.New("received body gzip failed")
			log.Debug("received body gzip failed")
			return
		}
		b, e := ioutil.ReadAll(r)
		if e != nil {
			err = errors.New("received body gzip failed")
			log.Debug("received body gzip failed")
			return
		}
		body = string(b)

		if e := r.Close(); e != nil {
			err = errors.New("received body gzip failed")
			log.Debug("received body gzip failed")
			return
		}
	}

	if header.ContentSize != uint32(len(body)) {
		err = errors.New("received body content size mismatch")
		log.Debug("received body content size mismatch")
		return
	}

	log.Debugf("%d >>> receiving %d->%d body:\n %s", accountId, header.ContentSize, header.Bodysize, body)
	return
}
Beispiel #5
0
func welcome(w http.ResponseWriter, r *http.Request) {
	t, _ := template.ParseFiles("./html/zym.html")
	log.Debugf("welcome invoke")
	var Server string = server_url
	t.Execute(w, Server)
}