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) }
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 }
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 }
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 }
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) }
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 }
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.") }
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 }
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 }