Exemplo n.º 1
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()
}
Exemplo n.º 2
0
func clientDecode(decode *gob.Decoder) {
	defer clientpkg.Disconnected()
	for {
		msg := new(packet.Packet)

		if err := decode.Decode(msg); err != nil {
			if err == io.EOF {
				return
			}

			log.Printf("Error decoding packet: %s", err)
			continue
		}

		clientpkg.Handle(msg)
	}
}