Esempio n. 1
0
func (this *MinorConfig) ReadData(conn net.Conn, data []byte) {
	ret, id, _, st, dt, _, _, l, d := protocol.ParseProtocol(data, len(data))
	if ret {
		if dt == protocol.PROTOCOL_NORMAL_JSON_DATA_TYPE {
			j := json.NewJSON(d[:l])
			ok, msg_type := j.GetString(MSG_TYPE)
			okk, identity := j.GetString(IDENTITY)
			if ok && okk {
				if msg_type == MSG_TYPE_VALUE_IDENTIFICATION && identity == IDENTITY_VALUE_MICRO_SERVER && st == this.config.microServerType {
					this.connectedMicroServerNum++
					this.connectedMicroServerAddress[id] = conn.RemoteAddr().String()

					this.write2MajorUpdataMicroServer()
				}
			}
		}
	}
}
Esempio n. 2
0
func (this *MajorConfig) ReadData(conn net.Conn, data []byte) {
	ret, id, _, _, dt, _, _, l, d := protocol.ParseProtocol(data, len(data))
	if ret {
		if dt == protocol.PROTOCOL_NORMAL_JSON_DATA_TYPE {
			str := string(d[:l])
			log.I_NET("major read from", conn.RemoteAddr(), ", data :", str)
			j := json.NewJSONByString(str)
			msg_type := j.Get(MSG_TYPE)
			if msg_type == MSG_TYPE_VALUE_IDENTIFICATION {
				//minor上报身份
				is, micro_server_type := j.GetInt(MICRO_SERVER_TYPE)
				if is {
					mi := new(minorInfo)
					mi.id = id
					mi.micro_server_type = byte(micro_server_type)

					this.addMinor(mi)
					log.I_NET("add minor identification id:", id, " , type:", micro_server_type, " success")
				}

			} else if msg_type == MSG_TYPE_VALUE_UPDATE_IDENTIFICATION {
				//minor更新身份
				is, micro_server_num := j.GetInt(MICRO_SERVER_NUM)
				iss, addr := j.GetArray(MICRO_SERVER_ADDRESS)
				if is && iss {
					mi := this.getMinor(id)
					mi.micro_server_num = micro_server_num
					address := make([]string, addr.GetLen())
					for i := 0; i < addr.GetLen(); i++ {
						address[i] = addr[i].(string)
					}
					log.I_NET("update minor identification id:", id, " , num:", micro_server_num, "address:", address, " success")
				}
			}

			this.check()
		}
	} else {
		log.E_NET("major read from", conn.RemoteAddr(), "error, data :", data)
	}
}
func (this *BaseProtocolTcpServerOperation) OpReadData(data []byte) bool {

	this.copyBuf(data)

	ret := true
	for true {
		exist, id, _, _, dataType, _, _, flen, _ := protocol.ParseProtocol(this.buf, this.buf_len)
		if exist {
			this.id = id
			if protocol.IsPingByType(dataType) {
				log.I_NET(this.conn.LocalAddr(), "write to client", this.conn.RemoteAddr(), "pong")
				this.conn.Write(protocol.NewPongProtocol(id))
				this.FinishOneProtocol()
				ret = true
			} else {
				ret = false
				this.protocol_buf = this.buf[:(flen + protocol.PROTOCOL_HEADER_LEN)]
				break
			}
		} else {
			this.protocol_buf = nil
			break
		}
	}

	timer.CancelInaccuracyTimer(this.timer_id)
	timer.NewInaccuracyTimerCallback(this.time_for_disconnected, func(id int64) {
		this.timer_id = id
	}, func(time int64) {
		if time != 0 {
			this.OpDisconnected()
		}
	})

	return ret
}
Esempio n. 4
0
func (this *BaseTcpOperation) OpData(conn net.Conn, data []byte) (result bool, id int, version byte, serverType byte, dataType byte, dataLen int, protocolData []byte, rawData []byte) {
	this.copyBuf(data)

	for true {
		exist, id, version, serverType, dataType, oLen, rawData, protocolLen, protocolData := protocol.ParseProtocol(this.buf, this.buf_len)
		if exist {
			if protocol.IsPingByType(dataType) {
				log.I_NET(conn.LocalAddr(), "write to client", conn.RemoteAddr(), "pong")
				conn.Write(protocol.NewPongProtocol(id))
				this.FinishOneProtocol()
			} else if protocol.IsPongByType(dataType) {
				log.I_NET("client", conn.LocalAddr(), "read from server", conn.RemoteAddr(), " pong")
				this.FinishOneProtocol()
			} else {

				d := make([]byte, protocolLen)
				copy(d, protocolData)

				np := make([]byte, oLen)
				copy(np, rawData)

				this.FinishOneProtocol()
				return true, id, version, serverType, dataType, protocolLen, d, np
			}
		} else {
			return false, 0, 0, 0, 0, 0, nil, nil
		}
	}

	return false, 0, 0, 0, 0, 0, nil, nil
}