Esempio n. 1
0
func (this *TcpServer) doConnect(conn net.Conn) {
	log.I_NET(conn.RemoteAddr(), " connect to server success")
	if this.manager != nil {
		this.manager.Connected(conn)
	}
	data := make([]byte, 2048)
	defer conn.Close()
	for {
		len, err := conn.Read(data)
		if err == nil {
			read_data := make([]byte, len)
			copy(read_data, data)
			log.D_NET(conn.LocalAddr(), "read from client", conn.RemoteAddr(), "data :", read_data)
			if this.manager != nil {
				this.manager.ReadData(conn, read_data)
			}
		} else {
			log.E_NET("read from client", conn.RemoteAddr(), "error, reason :", err)
			break
		}
	}
	log.I_NET("client ", conn.RemoteAddr(), " close")
	if this.manager != nil {
		this.manager.Disconnected(conn)
	}
}
Esempio n. 2
0
func (this *MinorConfig) write2MajorUpdateMicroServerNum(num int, addr []string) {
	if num != len(addr) {
		log.E_NET("update identification info error, the micro server num:", num, ",address:", addr)
	} else {
		this.write2Major(Minor2MajorUpdateMicroServerNum(this.config.clientID, num, addr))
	}
}
Esempio n. 3
0
func (this *TcpServer) Listen() {
	ip := ":" + strconv.Itoa(this.port)
	listen, err := net.Listen("tcp", ip)
	if err == nil {
		log.I_NET("tcp server", ip, "listening")
		for true {
			conn, err_conn := listen.Accept()
			if err_conn == nil {
				go this.doConnect(conn)
			} else {
				log.E_NET("accept tcp error, the reason : ", err)
			}
		}
	} else {
		log.E_NET("listen tcp error, the reason : ", err)
	}
}
Esempio n. 4
0
func (this *BaseClient) Disconnected() {
	log.E_NET("client disconnect to server", this.addr)
	this.is_connect = false
	this.conn.Close()
	this.conn = nil
	if this.disconnected_handler != nil {
		this.disconnected_handler()
	}
	this.delayReconnect()
}
Esempio n. 5
0
func (this *BaseServer) Listen(callback func(result bool, reason string)) {
	ip := ":" + strconv.Itoa(this.port)
	listen, err := net.Listen("tcp", ip)
	if err == nil {
		log.I_NET("tcp server", ip, "listening")
		callback(true, "")
		for true {
			conn, err_conn := listen.Accept()
			if err_conn == nil {
				go this.doConnect(conn)
			} else {
				log.E_NET("accept tcp error, the reason : ", err)
			}
		}
	} else {
		log.E_NET("listen tcp error, the reason : ", err)
		callback(false, err.Error())
	}
}
Esempio n. 6
0
func (this *TcpClient) Connect(addr string) {
	this.addr = addr
	conn, err := net.Dial("tcp", addr)
	if err == nil {
		log.I_NET(conn.LocalAddr(), " connect to ", conn.RemoteAddr(), " success")
		this.conn = conn
		this.reconnect_interval_time = 2 * time.Second
		this.doRead(conn)
	} else {
		log.E_NET(" connect to ", addr, " failure")
		this.delayReconnect()
	}
}
Esempio n. 7
0
func (this *MinorConfig) write2Major(data []byte) {
	ret, p := protocol.NewProtocol(this.config.clientID,
		this.config.version,
		protocol.PROTOCOL_MAJOR_SERVER_TYPE,
		protocol.PROTOCOL_NORMAL_JSON_DATA_TYPE,
		len(data),
		data)
	if ret {
		this.client.Write(p)
	} else {
		log.E_NET("minor id", this.config.clientID, "write to major error, id:", this.config.clientID, ",version:", this.config.version,
			",server type:", protocol.PROTOCOL_MAJOR_SERVER_TYPE, ",data type:", protocol.PROTOCOL_NORMAL_JSON_DATA_TYPE, ",data len:", len(data),
			",data:", data)
	}
}
Esempio n. 8
0
func (this *BaseClient) Connect(addr string) {
	this.addr = addr
	conn, err := net.Dial("tcp", addr)
	if err == nil {
		log.I_NET(conn.LocalAddr(), " connect to ", conn.RemoteAddr(), " success")
		this.conn = conn
		this.reconnect_interval_time = 2 * time.Second
		this.is_connect = true
		go this.doRead(conn)
	} else {
		log.E_NET(" connect to ", addr, " failure")
		if this.disconnected_handler != nil {
			this.disconnected_handler()
		}
		this.is_connect = false
		this.delayReconnect()
	}
}
Esempio n. 9
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)
	}
}
Esempio n. 10
0
func (this *BaseServer) doConnect(conn net.Conn) {
	this.Connected(conn)
	data := make([]byte, 2048)

	defer func() {
		this.Disconnected(conn)
	}()

	for {
		len, err := conn.Read(data)
		if err == nil {
			read_data := make([]byte, len)
			copy(read_data, data)
			log.D_NET(conn.LocalAddr(), "read from client", conn.RemoteAddr(), "data :", read_data)
			this.Read(conn, read_data)
		} else {
			log.E_NET("read from client", conn.RemoteAddr(), "error, reason :", err)
			break
		}
	}
}
func (this *BaseProtocolTcpServerOperation) copyBuf(data []byte) {
	data_len := len(data) + this.buf_len
	if data_len > BUF_LEVEL_4 {
		log.E_NET("operation data error, the all buf size more than 64k")
		return
	}
	level := BUF_LEVEL_1
	if data_len < BUF_LEVEL_1 {
		level = BUF_LEVEL_1
	} else if data_len < BUF_LEVEL_2 {
		level = BUF_LEVEL_2
	} else if data_len < BUF_LEVEL_3 {
		level = BUF_LEVEL_3
	} else {
		level = BUF_LEVEL_4
	}
	if level != len(this.buf) {
		old := this.buf
		this.buf = make([]byte, level)
		copy(this.buf, old[0:this.buf_len])
	}
	copy(this.buf[this.buf_len:], data)
	this.buf_len += len(data)
}
Esempio n. 12
0
func (this *MinorConfig) ClientDisconnect() {
	log.E_NET("minor id", this.config.clientID, "disconnect to major", this.client.GetConnection().RemoteAddr())
}