예제 #1
0
func (cli *LspClient) lspSend(message *LspMessage) {
	marshalled, err := json.Marshal(message)
	if err != nil {
		lsplog.CheckFatal(err)
	}
	lsplog.Vlogf(6, "[Client] Marshalled message: %s", marshalled)
	cli.udpConn.Write(marshalled)
}
예제 #2
0
func UDPResolve(hostport string) (*lspnet.UDPAddr, error) {
	lsplog.Vlogf(3, "Attempting to resolve address of %s", hostport)
	a, e := lspnet.ResolveUDPAddr(NET, hostport)
	if e != nil {
		lsplog.CheckFatal(e)
		return nil, e
	}
	lsplog.Vlogf(3, "Address %s resolved", hostport)
	return a, nil
}
예제 #3
0
func (cli *LspClient) udpConnect() error {
	lsplog.Vlogf(1, "[Client] Attempting to connect to %s:%d", cli.udpAddr.IP, cli.udpAddr.Port)
	b, e := lspnet.DialUDP(NET, nil, cli.udpAddr)
	if e != nil {
		lsplog.CheckFatal(e)
		return e
	}
	lsplog.Vlogf(6, "[Client] Connected to %s:%d", cli.udpAddr.IP, cli.udpAddr.Port)
	cli.udpConn = b
	return nil
}
예제 #4
0
func (srv *LspServer) lspListen() error {
	lsplog.Vlogf(1, "[Server] Attempting to listen to port to %d", srv.udpAddr.Port)
	conn, e := lspnet.ListenUDP(NET, srv.udpAddr)
	if e != nil {
		lsplog.CheckFatal(e)
		return e
	}
	lsplog.Vlogf(1, "[Server] Listening to port %d", srv.udpAddr.Port)
	srv.udpConn = conn
	return nil
}
예제 #5
0
func (srv *LspServer) lspSend(m *LspMessage, c *clientRepresenter) {
	if m == nil || c == nil {
		lsplog.Vlogf(6, "[Server] Got a send request for m: %+v c:%+v Ignoring message", m, c)
		return
	}
	marshalled, err := json.Marshal(m)
	if err != nil {
		lsplog.CheckFatal(err)
	}
	lsplog.Vlogf(6, "[Server] Marshalled message: %s", marshalled)
	srv.udpConn.WriteToUDP(marshalled, c.addr)
}
예제 #6
0
func iNewLspServer(port int, params *LspParams) (*LspServer, error) {
	srv := new(LspServer)
	srv.params = HandleParams(params)
	srv.clients = make(map[string]*clientRepresenter)
	srv.sent = list.New()
	srv.sentLocker = make(chan int, 1)
	srv.sentLocker <- 1
	srv.read = abuf.NewBuf()
	addr, e := UDPResolve("127.0.0.1:" + strconv.Itoa(port))
	if e != nil {
		lsplog.CheckFatal(e)
		return nil, e
	}
	srv.udpAddr = addr
	e = srv.lspListen()
	if e != nil {
		lsplog.CheckFatal(e)
		return nil, e
	}
	go srv.networkHandler()
	go srv.epochHandler()
	return srv, nil
}
예제 #7
0
func (cli *LspClient) lspConnect() {
	e := cli.udpConnect()
	if e != nil {
		lsplog.CheckFatal(e)
		panic("[Client] Unable to establish a connection.")
	}
	connectMessage := CreateConnectionPacket()
	lsplog.Vlogf(5, "[Client] Inserting connection message into sent buffer.")
	<-cli.sentLocker
	cli.sent.PushBack(connectMessage)
	cli.sentLocker <- 1
	cli.lspSend(connectMessage)
	go cli.networkHandler() // Starts the network handler for reading. Making sure it is fired after a UDP is issued.
	go cli.epochHandler()
	<-cli.connectedSyncer // Wait for the connection to be established.
	lsplog.Vlogf(1, "[Client] Connection established completely.")
}
예제 #8
0
func (cli *LspClient) readPacket() (m *LspMessage, addr *lspnet.UDPAddr, e error) {
	b := make([]byte, 1000)
	n, addr, err := cli.udpConn.ReadFromUDP(b)
	if addr == nil || n == 0 {
		lsplog.Vlogf(3, "[Client] Recieved a terminating packet.")
		return nil, addr, lsplog.ConnectionClosed()
	} else {
		lsplog.Vlogf(5, "[Client] Recieved a packet from %s:%d", addr.IP.String(), addr.Port)
	}
	if err != nil {
		lsplog.CheckFatal(err)
		return nil, nil, err
	}
	recieved := CreateEmptyPacket()
	err = json.Unmarshal(b[0:n], recieved)
	if err != nil {
		lsplog.CheckReport(3, err)
		return nil, addr, err
	}
	lsplog.Vlogf(6, "[Client] Recieved packet %+v", recieved)
	return recieved, addr, err
}
예제 #9
0
func (srv *LspServer) readPacket() (m *LspMessage, addr *lspnet.UDPAddr, e error) {
	b := make([]byte, 1000)
	n, addr, err := srv.udpConn.ReadFromUDP(b)
	if addr == nil {
		lsplog.Vlogf(3, "[Server] Recieved a terminating packet.")
		return nil, addr, lsplog.ConnectionClosed()
	} else {
		lsplog.Vlogf(4, "[Server] Recieved a packet from %s:%d with length %d", addr.IP.String(), addr.Port, n)
	}
	if err != nil {
		lsplog.Vlogf(3, "[Server] Error occured while reading from UDP.")
		lsplog.CheckFatal(err)
		return nil, nil, err
	}
	recieved := CreateEmptyPacket()
	err = json.Unmarshal(b[0:n], recieved)
	if err != nil {
		lsplog.Vlogf(6, "[Server] Error while unmarshalling %X", b)
		lsplog.CheckReport(4, err)
		return nil, addr, err
	}
	lsplog.Vlogf(5, "[Server] Recieved packet %+v", recieved)
	return recieved, addr, err
}