From 8399d5943024fd4517e0216dba7adbc34284546d Mon Sep 17 00:00:00 2001 From: Benjamin Date: Tue, 15 Feb 2022 11:07:16 +0100 Subject: [PATCH] =?UTF-8?q?Creation=20d'un=20module=20permettant=20d'envoy?= =?UTF-8?q?er=20des=20donn=C3=A9es=20sur=20une=20adresse=20multicast.=20Po?= =?UTF-8?q?ur=20le=20moment=20le=20message=20envoy=C3=A9,=20l'adress,=20le?= =?UTF-8?q?=20port=20et=20le=20temps=20entre=20chaque=20envoie=20est=20pr?= =?UTF-8?q?=C3=A9d=C3=A9fini.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/network/ClientMulticast.go | 65 ++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 pkg/network/ClientMulticast.go diff --git a/pkg/network/ClientMulticast.go b/pkg/network/ClientMulticast.go new file mode 100644 index 0000000..855d41f --- /dev/null +++ b/pkg/network/ClientMulticast.go @@ -0,0 +1,65 @@ +package network + +import ( + "net" + "time" +) + +// CreateClientMulticast Méthode de construction d'un instance de la stuct ClientMulticast +func CreateClientMulticast(address string, second time.Duration) ClientMulticast { + return ClientMulticast{ + address: address, + second: second, + } +} + +// ClientMulticast Cette structure représente une communication en multicast. +// TODO : Prévoir une fermeture de la connection (con.Close()) +type ClientMulticast struct { + // address Adresse de multicast (address:port) + address string + + // second Temps en seconde entre chaque ping + second time.Duration +} + +// Run Cette méthode démarre une commmunication multicast +func (cMult ClientMulticast) Run() { + addr, done := cMult.ResolveAddr() + if done { + return + } + + con, done2 := cMult.DialUdp(addr) + if done2 { + return + } + + for { + con.Write([]byte("Hello")) + time.Sleep(time.Second * cMult.second) + } + +} + +// ResolveAddr Permet de résoude l'addresse +func (cMult ClientMulticast) ResolveAddr() (*net.UDPAddr, bool) { + addr, errResUdp := net.ResolveUDPAddr("udp", cMult.address) + + if errResUdp != nil { + println(errResUdp.Error()) + return nil, true + } + + return addr, false +} + +// DialUdp Ouvre une connection UDP +func (cMult ClientMulticast) DialUdp(addr *net.UDPAddr) (*net.UDPConn, bool) { + con, errDial := net.DialUDP("udp", nil, addr) + if errDial != nil { + println(errDial.Error()) + return nil, true + } + return con, false +}