func SpawnConnectionPool(Port int) *gnet.ConnectionPool { config := gnet.NewConfig() config.Port = uint16(Port) //set listening port config.DisconnectCallback = onDisconnect //disconnect callback config.ConnectCallback = onConnect //connect callback config.MessageCallback = onMessage //message callback //create pool cpool := gnet.NewConnectionPool(config) //open lsitening port if err := cpool.StartListen(); err != nil { log.Panic(err) } //listen for connections in new goroutine go cpool.AcceptConnections() //handle income data in new goroutine go func() { for true { time.Sleep(time.Millisecond * 100) cpool.HandleMessages() } }() return cpool }
func (self *PoolOwner) Init(pCMan *MinimalConnectionManager, listen_port uint16, num_id int, nickname string) { config := gnet.NewConfig() config.Port = uint16(listen_port) cp := gnet.NewConnectionPool(config) dm := gnet.NewDispatcherManager() self.isConnSolicited = make(map[string]bool) cp.Config.MessageCallback = dm.OnMessage cp.Config.ConnectCallback = self.OnConnect cp.Config.DisconnectCallback = self.OnDisconnect d := dm.NewDispatcher(cp, common_channel, self) d.RegisterMessages(messageMap) self.pCMan = pCMan self.pDispatcherManager = dm self.pDispatcher = d self.key_map = make(map[string]byte) self.pConnectionPool = cp self.debug_num_id = num_id self.debug_nickname = nickname }
// Returns a Daemon with primitives allocated func NewDaemon(config Config) *Daemon { config = config.preprocess() // c.DHT.address = c.Daemon.Address d := &Daemon{ Config: config.Daemon, Peers: NewPeers(config.Peers), ExpectingIntroductions: make(map[string]time.Time), // TODO -- if there are performance problems from blocking chans, // Its because we are connecting to more things than OutgoingMax // if we have private peers connectionErrors: make(chan ConnectionError, config.Daemon.OutgoingMax), OutgoingConnections: make(map[string]*gnet.Connection, config.Daemon.OutgoingMax), pendingConnections: make(map[string]([]*gnet.Service), config.Daemon.PendingMax), } d.Peers.Init() if config.DHT.Disabled == false { d.DHT = dht.NewDHT(config.DHT) d.DHT.Init() } //gnet set connection pool gnet_config := gnet.NewConfig() gnet_config.Port = uint16(d.Config.Port) //set listening port gnet_config.Address = d.Config.Address d.Pool = gnet.NewConnectionPool(gnet_config) //service manager d.ServiceManager = gnet.NewServiceManager(d.Pool) ds := NewDaemonService(d.ServiceManager, d) d.Service = ds.Service return d }