예제 #1
0
파일: cc.go 프로젝트: weichaoduo/zeromore
func reqWithBufferio(conn *net.TCPConn, data string, times int) {

	req_ready_byte, _ := protocol.Packet(data)
	conn.Write(req_ready_byte)

	var i int
	i = 0
	sid := ""
	str := `{ "cmd":"socket.getSession"}`

	reader := bufio.NewReader(conn)
	for {
		msg, err := reader.ReadString('\n')
		if err != nil {
			fmt.Println(conn.RemoteAddr().String(), " connection error: ", err)
			conn.Close()
			return
		}

		if msg == "" {
			continue
		}

		msg_json, errjson := jason.NewObjectFromBytes([]byte(msg))
		if errjson != nil {
			//fmt.Println( " errjson  :",  errjson.Error() )
			continue
		}
		cmd, _ := msg_json.GetString("cmd")
		//fmt.Printf( " msg: %s\n", msg )
		if cmd == "socket.user_login" {
			sid, _ = msg_json.GetString("data")
			//fmt.Printf( " sid: %s\n", sid )
			str = fmt.Sprintf(`{ "cmd":"socket.getSession","params":{"sid":"%s"} }`, sid)
			session_req_byte, _ := protocol.Packet(str)
			conn.Write(session_req_byte)
			//time.Sleep(10 * time.Millisecond )
			checkError(err)

		}
		if cmd == "socket.getSession" && sid != "" {

			// session_str,  _ := msg_json.GetString("data" )
			//session_json, _ := jason.NewObjectFromBytes( []byte(session_str) )
			//sid ,_= session_json.GetString("msg","Sid")
			//fmt.Printf( " sid: %s\n", sid )
			str = fmt.Sprintf(`{ "cmd":"socket.getSession","params":{"sid":"%s"} }`, sid)
			session_req_byte, _ := protocol.Packet(str)
			conn.Write(session_req_byte)
			checkError(err)
			i++
			time.Sleep(10 * time.Millisecond)
			if i > times {
				conn.Close()
				fmt.Println(" i : ", i)
				fmt.Println(" conn close! ", i, "\n")
				break
			}
		}

	}

	conn.Close()

}
예제 #2
0
func workeDispath(msg string, conn *net.TCPConn, client_idf *string, worker_idf *string) (int, error) {

	//ret_json, _ := jason.NewObjectFromBytes(msg)
	//cmd, _ := ret_json.GetString("cmd")

	ret_arr := strings.Split(msg, "||")
	//fmt.Println("workeDispath ret_arr 4:", ret_arr)
	if len(ret_arr) < 4 {
		//fmt.Println("workeDispath data length error!")
		return 0, nil
	} else {
		if len(ret_arr) > 4 {

			for i := 4; i < int(len(ret_arr)); i++ {

				ret_arr[3] = ret_arr[3] + ret_arr[i]
			}

		}
	}

	cmd := ret_arr[0]
	*client_idf = ret_arr[1]
	*worker_idf = ret_arr[2]
	worker_data := ret_arr[3]
	data := ""

	// 前端连接到代理
	if cmd == global.DATA_REQ_CONNECT {

		//data_byte, _ = protocol.Packet(`{"cmd":"req.connect","ret":200,"msg":"ok"}`)
		data := fmt.Sprintf(`%s||%s||%s||%s`, global.DATA_REQ_CONNECT, "", "", "ok")
		return conn.Write([]byte(data))

	}
	// 前端发数据过来
	if cmd == global.DATA_REQ_MSG {

		//fmt.Println( "workeDispath idf  :", *worker_idf )
		// @todo应该自动分配worker

		worker_conn := GetWorkerConn(*worker_idf)
		log.Info(*worker_idf, worker_conn)
		if worker_conn == nil {
			data = fmt.Sprintf(`%s||%s||%s||%s`, global.DATA_REQ_MSG, *client_idf, *worker_idf, "worker no found!")
			fmt.Println("worker_conn ", *worker_idf, " no found!")
			return conn.Write([]byte(data + "\n"))
		}
		data = fmt.Sprintf(`%s||%s||%s||%s`, global.DATA_REQ_MSG, *client_idf, *worker_idf, worker_data)
		log.Info("workeDispath worker_data :", data)
		return worker_conn.Write([]byte(data + "\n"))
	}
	// worker连接到代理
	if cmd == global.DATA_WORKER_CONNECT {
		AddWorkerConn(*worker_idf, conn)
		data = fmt.Sprintf(`%s||%s||%s||%s`, global.DATA_WORKER_CONNECT, *client_idf, *worker_idf, "ok")
		fmt.Println("worker.connect : ", *worker_idf)
		conn.Write([]byte(data + "\n"))
		go workerPingKick(conn, *worker_idf)

	}
	// worker返回处理结果
	if cmd == global.DATA_WORKER_REPLY {

		//fmt.Println( "worker.reply data 7 :" , ret_arr );
		req_conn := area.GetConn(*client_idf)
		if req_conn == nil {
			fmt.Println("req_conn :", *client_idf, " no found")
		}

		//如果没有找到客户端连接对象则报错并返回
		atomic.AddInt64(&global.Qps, 1)
		if req_conn != nil {
			worker_data_byte, _ := protocol.Packet(worker_data)
			log.Info(worker_data_byte)
			//fmt.Println( "worker.reply  worker_data :" ,  worker_data  );
			return req_conn.Write([]byte(worker_data + "\n"))
		}

		data = fmt.Sprintf(`%s||%s||%s||%s`, global.DATA_WORKER_REPLY, *client_idf, *worker_idf, "req conn no found!")
		return conn.Write([]byte(data + "\n"))
	}

	return 1, nil

}