예제 #1
0
func main() {

	clientcount := int32(0)
	packetcount := int32(0)

	clients := util.NewConnCurrMap()

	service := ":8010"
	tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
	if err != nil {
		fmt.Printf("ResolveTCPAddr")
	}
	listener, err := net.ListenTCP("tcp", tcpAddr)
	if err != nil {
		fmt.Printf("ListenTCP")
	}

	ticker := util.DurationTicker()
	ticker.Start(1000, func(_ time.Time) {
		tmp := atomic.LoadInt32(&packetcount)
		atomic.StoreInt32(&packetcount, 0)
		fmt.Printf("clientcount:%d,packetcount:%d/s\n", clientcount, tmp)
	})

	for {
		conn, err := listener.Accept()
		if err != nil {
			continue
		}
		session := socket.NewTcpSession(conn)
		clients.Set(session, session)
		atomic.AddInt32(&clientcount, 1)
		go socket.ProcessSession(session, packet.NewRPacketDecoder(1024),
			func(session *socket.Tcpsession, rpk packet.Packet, errno error) {
				if rpk == nil {
					session.Close()
					atomic.AddInt32(&clientcount, -1)
					fmt.Printf("error:%s\n", errno)
					clients.Del(session)
					return
				}
				for _, v := range clients.Vals() {
					atomic.AddInt32(&packetcount, int32(1))
					wpk := rpk.MakeWrite()
					v.(*socket.Tcpsession).Send(wpk)
				}
			})
	}
}
예제 #2
0
func main() {
	cM := Util.NewConnCurrMap()
	cM.Set("a", "a")
	cM.Set("b", "b")
	cM.Set("c", "c")
	lm := cM.All()
	lm["d"] = "d"
	for k, v := range cM.All() {
		fmt.Printf("%s,%s\n", k.(string), v.(string))
	}
	_, ok := cM.Get("d")
	if !ok {
		fmt.Printf("no d\n")
	}

	for _, v := range cM.Keys() {
		fmt.Printf("%s\n", v.(string))
	}
}