zoreide/multicast.go

69 lines
1.5 KiB
Go

package main
import (
"log"
"net"
)
type AbstractMulticast struct {
MIpAddr string
MPort string
MaxDatagramSize int
MWaddr *net.UDPAddr
MRaddr *net.UDPAddr
Wconn *net.UDPConn
Rconn *net.UDPConn
HierarchyArray []int64
}
func (mip *AbstractMulticast) CreateUdpAddr() {
// This Will create the local UDP Address
addr, err := net.ResolveUDPAddr("udp4", mip.MIpAddr+":"+mip.MPort)
if err != nil {
log.Fatal(err.Error())
}
mip.MRaddr = addr
log.Println("Multicast Listen addr created: ", mip.MRaddr.String())
// This will create the Writing UDP Addr
mip.MWaddr = addr
log.Println("Multicast Writing addr created: ", mip.MWaddr.String())
}
func (mip *AbstractMulticast) CreateWritingSocket() {
conn, err := net.DialUDP("udp4", nil, mip.MWaddr)
if err != nil {
log.Fatal(err.Error())
}
mip.Wconn = conn
log.Println("Multicast Writing Socket Created: ", mip.Wconn.LocalAddr().String(), "->", mip.Wconn.RemoteAddr().String())
}
func (mip *AbstractMulticast) CreateReadingSocket() {
// Open up a connection
conn, err := net.ListenMulticastUDP("udp", nil, mip.MRaddr)
if err != nil {
log.Fatal("UDP " + err.Error())
}
conn.SetReadBuffer(mip.MaxDatagramSize)
mip.Rconn = conn
log.Println("Multicast Reading Socket Created: ", mip.Rconn.LocalAddr().String())
}
func (mip *AbstractMulticast) GetMulticastReady() {
mip.CreateUdpAddr()
mip.CreateWritingSocket()
mip.CreateReadingSocket()
}