Ejemplo n.º 1
0
func client(username string, server io.ReadWriteCloser) {
	log.Print("Connected to server")
	defer server.Close()
	defer log.Print("Server disconnected")

	buf := bufio.NewWriter(server)
	encode, decode := gob.NewEncoder(buf), gob.NewDecoder(server)

	var handshake Handshake
	if err := encode.Encode(handshake); err != nil {
		log.Printf("Error while sending handshake: %s", err)
		return
	}

	send := make(chan *packet.Packet)
	defer close(send)

	clientpkg.Network = send

	go clientEncode(encode, buf, send)
	go clientDecode(decode)

	go keepAlive(send)

	clientpkg.Main()
}
Ejemplo n.º 2
0
func client(username string, server io.ReadWriteCloser) {
	log.Printf("Connected to server")
	defer log.Printf("Server disconnected")

	xport := fatchan.New(server, nil)
	login := make(chan Handshake)
	xport.FromChan(login)
	defer close(login)

	me := Handshake{
		User: username,
		Send: make(chan packet.Packet),
		Recv: make(chan packet.Packet),
	}
	login <- me

	in := bufio.NewReader(os.Stdin)

	clientpkg.Network = me.Send

	go func() {
		defer close(me.Send)
		for {
			line, err := in.ReadString('\n')
			if err == io.EOF {
				return
			}

			if err != nil {
				log.Fatalf("readline(): %s", err)
			}

			me.Send <- packet.Packet{
				Chat: &packet.Chat{
					Message: line,
				},
			}
		}
	}()

	go func() {
		for msg := range me.Recv {
			clientpkg.Handle(msg)
		}
		clientpkg.Disconnected()
	}()

	clientpkg.Main()
}