コード例 #1
0
func iNewLspClient(hostport string, params *LspParams) (*LspClient, error) {
	client := new(LspClient)
	client.WBchan = make(chan *LspMessage, 1)
	client.Echan = make(chan int, 1)
	client.EchanBack = make(chan int, 1)
	client.Rchan = make(chan int)
	client.Wchan = make(chan int, 1)
	client.CloseChan = make(chan int, 1)
	client.CloseChanEpoch = make(chan int, 1)
	client.CloseNetworkChan = make(chan int, 1)
	client.RBchan = make(chan *LspMessage)
	client.eventHandClosedChan = make(chan int, 1)
	client.Nchan = make(chan *LspMessage, 1)
	client.established = make(chan int)
	client.writeEnded = make(chan int, 1)
	client.writeList = new(list.List)
	client.readList = new(list.List)
	client.errChan = make(chan error, 1)
	client.exitEpochHandler = make(chan int, 1)
	client.continueEpochHandler = make(chan int, 1)
	client.AckWrite = true
	client.allowWrite = true
	client.sn = 1
	client.rcvn = 1
	client.id = 0
	client.NbWrite = 0
	if params == nil {
		client.params = &LspParams{5, 2000}
	} else {
		client.params = params
	}
	addr, errResolve := lspnet.ResolveUDPAddr("udp", hostport)
	if errResolve != nil {
		return nil, errResolve
	}
	connexion, errDial := lspnet.DialUDP("udp", nil, addr)
	if errDial != nil {
		return nil, errDial
	}
	client.conn = connexion
	client.running = true
	msg, _ := buildMSG(MsgCONNECT, 0, 0, nil)
	client.writeList.PushBack(msg)
	client.NbWrite++
	client.lastWrite = msg
	client.Wchan <- 1
	client.epochCount = 0
	client.eventHandClosed = false
	go NetworkHandlerClient(client)
	go EpochHandlerClient(client)
	go EventHandlerClient(client)
	<-client.established
	return client, nil
}
コード例 #2
0
func iNewLspClient(hostport string, params *LspParams) (*LspClient, error) {
	cli := new(LspClient)
	if params == nil {
		// Insert default parameters
		params = &LspParams{5, 2000}
	}
	cli.params = params
	addr, err := lspnet.ResolveUDPAddr("udp", hostport)
	if lsplog.CheckReport(1, err) {
		return nil, err
	}
	cli.lspConn = newConn(addr, 0, 0)
	// Client's first received message will be data message.
	cli.lspConn.nextRecvSeqNum = NextSeqNum(0)
	cli.udpConn, err = lspnet.DialUDP("udp", nil, addr)
	if lsplog.CheckReport(1, err) {
		return nil, err
	}
	// Need enough room to recycle close messages at end
	cli.appReadChan = make(LspMessageChan, 2)
	cli.readBuf = NewBuf()
	cli.appWriteChan = make(LspMessageChan, 1)
	cli.netInChan = make(LspMessageChan, 1)
	cli.epochChan = make(chan int)
	cli.closeReplyChan = make(chan error, 2)
	cli.writeReplyChan = make(chan error, 2)

	go cli.clientLoop()
	go cli.udpReader()
	go epochTrigger(cli.params.EpochMilliseconds, cli.epochChan, &cli.lspConn.stopNetworkFlag)
	// Send connection request to server
	nm := GenConnectMessage()
	cli.udpWrite(nm)
	cli.lspConn.pendingMsg = nm
	cli.lspConn.nextSendSeqNum = NextSeqNum(0)
	cm := <-cli.appReadChan
	if cm.Type == MsgCONNECT {
		return cli, nil
	}
	return nil, lsplog.MakeErr("Connection failed")
}