Example #1
0
func (this *BaseTcpOperation) OpRawData(conn net.Conn, data []byte) (result bool, id int, version byte, serverType byte, dataType byte, dataLen int, rawData []byte) {
	this.copyBuf(data)

	for true {

		exist, id, version, serverType, dataType, oLen, rawData := protocol.ParseRawProtocol(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, oLen)
				copy(d, rawData)

				this.FinishOneProtocol()
				return true, id, version, serverType, dataType, oLen, d
			}
		} else {
			return false, 0, 0, 0, 0, 0, nil
		}
	}
	return false, 0, 0, 0, 0, 0, nil
}
Example #2
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)
	}
}
Example #3
0
func (this *RouteServer) Run() {

	initRouteRegister()

	this.Listen(func(result bool, reason string) {
		if result {
			log.I_NET("route server listen", this.GetPort(), "success")
		} else {
			log.I_NET("route server listen", this.GetPort(), "error, reason :", reason)
		}
	})
}
Example #4
0
func (this *BaseClient) Write(data []byte, callback func(error, []byte)) {
	log.I_NET("client connect state is ", this.is_connect)
	err := this.write(data)
	if callback != nil {
		callback(err, data)
	}
}
Example #5
0
func Test_Tcp(t *testing.T) {
	go func() {
		NewTcpProtocolClient().Connect("127.0.0.1:9001")
	}()

	NewTcpProtocolServer(9001).Listen(func(result bool, reason string) {
		log.I_NET("listen success")
	})
}
func (this *BaseProtocolTcpClient) doPing() {
	timer.CancelInaccuracyTimer(this.ping_timer_id)
	timer.NewInaccuracyTimerCallback(this.ping_interval, func(id int64) {
		this.ping_timer_id = id
	}, func(time int64) {
		if time != 0 {
			log.I_NET(this.client.GetConnection().LocalAddr(), " write to ", this.client.GetConnection().RemoteAddr(), ", data : ping")
			this.Write(protocol.NewPingProtocol(this.entity.ID))
		}
	})
}
Example #7
0
func NewRouteServer(config *RouteServerConfig) *RouteServer {
	r := new(RouteServer)
	r.config = config

	r.TcpProtocolServer = base.NewTcpProtocolServer(config.Port)
	r.TcpProtocolServer.SetOperationHandler(func(conn net.Conn, id int, version byte, serverType byte, dataType byte, dataLen int, data []byte, raw []byte) {
		log.I_NET("reoute server recv from", conn.RemoteAddr(), ", data id :", id, ", version :", version,
			", server type :", serverType, ", data type :", dataType, ", data :", string(data))
		r.execute(conn, id, version, serverType, dataType, dataLen, data, raw)
	})

	r.TcpProtocolServer.SetConnectedHandler(func(conn net.Conn) {
		log.I_NET("client", conn.RemoteAddr().String(), "connect to route server", conn.LocalAddr().String(), "success")
	})

	r.TcpProtocolServer.SetDisconnectedHandler(func(conn net.Conn) {
		log.I_NET("client", conn.RemoteAddr().String(), "disconnect to route server", conn.LocalAddr().String())
	})
	return r
}
Example #8
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)
	}
}
Example #9
0
func (this *RouteServer) execute(conn net.Conn, id int, version byte, serverType byte, dataType byte, dataLen int, data []byte, raw []byte) {
	js := json.NewJSON(data)

	commander.ExecuteCmd1(js, func(result json.JSONObject) {
		log.I_NET("callback from commander, data :", result.ToString())
		put := []byte(result.ToString())
		r, d := protocol.NewProtocol(id, version, serverType, dataType, len(put), put)
		if r {
			conn.Write(d)
		}
	})
}
Example #10
0
func (this *BaseClient) doPing() {
	timer.CancelInaccuracyTimer(this.ping_timer_id)
	timer.NewInaccuracyTimerCallback(this.ping_interval, func(id int64) {
		this.ping_timer_id = id
	}, func(time int64) {
		if time != 0 {
			log.I_NET(this.conn.LocalAddr(), " write to ", this.conn.RemoteAddr(), ", data : ping")
			this.Write(protocol.NewPingProtocol(this.id), nil)
			this.doPing()
		}
	})
}
Example #11
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()
	}
}
Example #12
0
func (this *BaseProtocolTcpClient) ClientReadData(data []byte, data_len int) {
	this.doPing()

	copy(this.buf[this.buf_len:], data[0:data_len])
	this.buf_len += data_len

	if protocol.IsPong(this.buf, this.buf_len) {
		log.I_NET(this.client.GetConnection().LocalAddr(), "read from server", this.client.GetConnection().RemoteAddr(), ", data : pong")
		this.FinishOneProtocol()
	} else {
		if this.manager != nil {
			this.manager.ClientReadData(this.buf, this.buf_len)
		}
	}
}
Example #13
0
func NewAuthServer(config *AuthServerConfig) *AuthServer {
	a := new(AuthServer)
	a.config = config
	a.TcpProtocolServer = base.NewTcpProtocolServer(config.Port)
	a.TcpProtocolServer.SetOperationHandler(func(conn net.Conn, id int, version byte, serverType byte, dataType byte, dataLen int, data []byte, raw []byte) {

	})

	a.TcpProtocolServer.SetConnectedHandler(func(conn net.Conn) {
		log.I_NET("connection server", conn.RemoteAddr(), "connnect to auth server success")
	})

	a.TcpProtocolServer.SetDisconnectedHandler(func() {
		log.W_NET("connection server disconnnect to auth server")
	})
	return a
}
Example #14
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)
	}
}
Example #15
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()
	}
}
Example #16
0
/**
解析协议报文,如果能解析出一条协议,则第一个参数返回true;
其他参数依次为协议中客户端id,版本号,服务器类型,数据类型,协议中原始数据实体长度,协议中原始数据实体,最终数据长度,最终数据实体
最终数据实体是根据协议中数据类型来生成,如果数据被压缩,则最终数据实体是解压后的数据
*/
func ParseProtocol(data []byte, dataLen int) (result bool, id int, version byte, serverType byte, dataType byte, oriDataLen int, oriData []byte, finalDataLen int, finalData []byte) {
	if len(data) >= PROTOCOL_HEADER_LEN && dataLen >= PROTOCOL_HEADER_LEN && data[0] == PROTOCOL_FRIST_LETTER {
		pLen := getDataLength(data)
		if (pLen + PROTOCOL_HEADER_LEN) <= dataLen {
			tp := data[PROTOCOL_TYPE_INDEX]
			oriData := data[PROTOCOL_DATA_INDEX : PROTOCOL_DATA_INDEX+pLen]
			dt := tp & 0x0F
			if dt == PROTOCOL_COMPRESS_JSON_DATA_TYPE {
				log.I_NET("run decompress")
				deCom := compress.DeCompress(oriData, pLen)
				return true, getID(data), data[PROTOCOL_VERSION_INDEX], tp & 0xF0, dt, pLen, oriData, len(deCom), deCom
			} else {
				return true, getID(data), data[PROTOCOL_VERSION_INDEX], tp & 0xF0, dt, pLen, oriData, pLen, oriData
			}

		}
	}
	return false, 0, 0, 0, 0, 0, nil, 0, nil
}
Example #17
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())
	}
}
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
}
Example #19
0
func (this *BaseServer) Connected(conn net.Conn) {
	log.I_NET("client:", conn.RemoteAddr(), "connect to server", conn.LocalAddr(), "success")
	if this.connected_handler != nil {
		this.connected_handler(conn)
	}
}
Example #20
0
func (this TcpClient) ReConnect() {
	if this.auto_connect && "" != this.addr {
		log.I_NET("try to reconnect ", this.addr)
		this.Connect(this.addr)
	}
}
Example #21
0
func (this *BaseServer) Disconnected(conn net.Conn) {
	log.I_NET("client", conn.RemoteAddr(), "close")
	if this.disconnected_handler != nil {
		this.disconnected_handler(conn)
	}
}
Example #22
0
//客户端处理接口
func (this *MinorConfig) ClientConnect() {
	log.I_NET("minor id", this.config.clientID, "connect to major", this.client.GetConnection().RemoteAddr(), "success")
	this.write2MajorIdentification()

	this.write2MajorUpdateMicroServerNum(2, []string{"127.0.0.1:9999", "127.0.0.1:9998"})
}
Example #23
0
func (this *MinorConfig) ClientReadData(data []byte, data_len int) {
	log.I_NET("minor id", this.config.clientID, "read from major", this.client.GetConnection().RemoteAddr(), ", data :", data)
}
Example #24
0
func (this *BaseClient) ReConnect() {
	if !this.is_connect && this.auto_connect && "" != this.addr {
		log.I_NET("try to reconnect ", this.addr)
		this.Connect(this.addr)
	}
}