Ejemplo 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)
	}
}
Ejemplo n.º 2
0
func (this *BaseClient) doRead(conn net.Conn) {
	this.Connected()

	defer this.Disconnected()

	data := make([]byte, 2048)
	for {
		d_len, err := conn.Read(data)
		if err != nil {
			break
		}
		temp := data[0:d_len]
		log.D_NET(this.conn.LocalAddr(), "read from server", conn.RemoteAddr(), ", data : ", temp)
		this.ReadData(temp)
	}
}
Ejemplo n.º 3
0
func (this *BaseClient) write(data []byte) error {
	if this.conn != nil {
		log.D_NET(this.conn.LocalAddr(), "write to server", this.conn.RemoteAddr(), " data : ", data)
		read_n := 0
		for true {
			n, err := this.conn.Write(data[read_n:])
			if err == nil {
				if read_n >= len(data) {
					return nil
				}
				read_n += n
			} else {
				return err
			}
		}
	}
	return errors.New("the connection is nil")
}
Ejemplo n.º 4
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
		}
	}
}
Ejemplo n.º 5
0
func (this TcpClient) doRead(conn net.Conn) {
	if this.manager != nil {
		this.manager.ClientConnect()
	}
	data := make([]byte, 2048)
	defer conn.Close()
	for {
		d_len, err := conn.Read(data)
		if err != nil {
			break
		}
		temp := data[0:d_len]
		log.D_NET(this.conn.LocalAddr(), "read from server", conn.RemoteAddr(), ", data : ", temp)
		if this.manager != nil {
			this.manager.ClientReadData(temp, len(temp))
		}
	}

	if this.manager != nil {
		this.manager.ClientDisconnect()
	}

	defer this.ReConnect()
}
Ejemplo n.º 6
0
func (this TcpClient) Write(data []byte) {
	if this.conn != nil {
		log.D_NET(this.conn.LocalAddr(), "write to server", this.conn.RemoteAddr(), " data : ", data)
		this.conn.Write(data)
	}
}