コード例 #1
0
ファイル: stream_test.go プロジェクト: youxidev/GoGameServer
func StreamTest(t *testing.T, callback func(*link.Session)) {
	protocol := New(1024, 1024, 1024)

	server, err := link.Serve("tcp", "0.0.0.0:0", protocol)
	unitest.NotError(t, err)
	addr := server.Listener().Addr().String()

	go server.Serve(func(session *link.Session) {
		for {
			var msg TestMessage
			if err := session.Receive(&msg); err != nil {
				break
			}
			if err := session.Send(msg); err != nil {
				break
			}
		}
	})

	session, err := link.Connect("tcp", addr, protocol)
	unitest.NotError(t, err)
	callback(session)
	session.Close()
	server.Stop()

	MakeSureSessionGoroutineExit(t)
}
コード例 #2
0
ファイル: packet_test.go プロジェクト: youxidev/GoGameServer
func PacketTest(t *testing.T, callback func(*link.Session)) {
	protocol := New(binary.SplitByUvarint, 1024, 1024, 1024)

	server, err := link.Serve("tcp", "0.0.0.0:0", protocol)
	unitest.NotError(t, err)

	go server.Serve(func(session *link.Session) {
		for {
			var msg RAW
			if err := session.Receive(&msg); err != nil {
				break
			}
			if err := session.Send(msg); err != nil {
				break
			}
		}
	})

	session, err := link.Connect("tcp", server.Listener().Addr().String(), protocol)
	unitest.NotError(t, err)
	callback(session)
	session.Close()
	server.Stop()

	MakeSureSessionGoroutineExit(t)
}
コード例 #3
0
ファイル: server2.go プロジェクト: catxuxiang/link
func main() {
	var addr string

	flag.StringVar(&addr, "addr", ":10010", "echo server address")
	flag.Parse()

	server, err := link.Serve("tcp", addr, link.Packet(2, 1024*1024, 1024, binary.LittleEndian, TestCodec{}))
	if err != nil {
		panic(err)
	}

	println("server start:", server.Listener().Addr().String())
	for {
		session, err := server.Accept()
		if err != nil {
			break
		}
		go func() {
			//addr := session.Conn().RemoteAddr().String()
			//println("client", addr, "connected")
			for {
				var msg []byte
				if err = session.Receive(&msg); err != nil {
					break
				}
				if err = session.Send(msg); err != nil {
					break
				}
			}
			//println("client", addr, "closed")
		}()
	}
}
コード例 #4
0
ファイル: echo_server.go プロジェクト: youxidev/GoGameServer
func main() {
	flag.Parse()

	server, err := link.Serve("tcp", *addr, packet.New(
		binary.SplitByUint16BE, 1024, 1024, 1024,
	))
	if err != nil {
		panic(err)
	}
	println("server start:", server.Listener().Addr().String())

	server.Serve(func(session *link.Session) {
		addr := session.Conn().RemoteAddr().String()
		log(addr, "connected")
		for {
			var msg packet.RAW
			if err := session.Receive(&msg); err != nil {
				break
			}
			log(addr, "say:", string(msg))
			if err = session.Send(msg); err != nil {
				break
			}
		}
		log(addr, "closed")
	})
}
コード例 #5
0
ファイル: main.go プロジェクト: youxidev/GoGameServer
func startLocalServer() {
	//连接DB
	db.Init()

	//连接Redis
	redisProxyErr := redisProxy.InitClient(cfg.GetValue("redis_ip"), cfg.GetValue("redis_port"))
	checkError(redisProxyErr)

	listener, err := link.Serve("tcp", "0.0.0.0:"+local_port, packet.New(
		binary.SplitByUint32BE, 1024, 1024, 1024,
	))
	checkError(err)

	listener.Serve(func(session *link.Session) {
		session.AddCloseCallback(session, func() {
			session.Close()
		})
		global.AddSession(session)

		var msg packet.RAW
		for {
			if err := session.Receive(&msg); err != nil {
				break
			}
			module.ReceiveMessage(session, msg)
		}
	})
}
コード例 #6
0
ファイル: unit_test.go プロジェクト: youxidev/GoGameServer
func StartTestBackend(t *testing.T, handler func(*link.Session)) *link.Server {
	backend, err := link.Serve("tcp", "0.0.0.0:0", NewBackend(
		1024, 1024, 1024,
	))
	unitest.NotError(t, err)
	go backend.Serve(handler)
	return backend
}
コード例 #7
0
ファイル: echo_server.go プロジェクト: 616050468/link
func main() {
	var addr string

	flag.StringVar(&addr, "addr", ":10010", "echo server address")
	flag.Parse()

	server, err := link.Serve("tcp://"+addr, link.Stream(), nil)
	if err != nil {
		panic(err)
	}
	println("server start:", server.Listener().Addr().String())
	server.Loop(link.Echo)
}
コード例 #8
0
ファイル: broadcast.go プロジェクト: ericx10ng/link
// This is broadcast server demo work with the echo_client.
// usage:
//     cd src/github.com/funny/link
//     go generate channel.go
//     go run example/echo/broadcast.go
func main() {
	var addr string

	flag.StringVar(&addr, "addr", ":10010", "echo server address")
	flag.Parse()

	server, err := link.Serve("tcp", addr, link.Async(1024, link.Packet(2, 1024*1024, 1024, binary.LittleEndian, TestCodec{})))
	if err != nil {
		panic(err)
	}
	println("server start:", server.Listener().Addr().String())

	channel := link.NewUint64Channel()
	go func() {
		for range time.Tick(time.Second * 2) {
			now := "from channel: " + time.Now().Format("2006-01-02 15:04:05")
			channel.Fetch(func(session *link.Session) {
				session.Send(now)
			})
		}
	}()

	for {
		session, err := server.Accept()
		if err != nil {
			break
		}

		go func() {
			addr := session.Conn().RemoteAddr().String()
			println("client", addr, "connected")

			channel.Put(session.Id(), session)

			for {
				var msg string
				if err := session.Receive(&msg); err != nil {
					break
				}
				println(addr, "say:", msg)
				channel.Fetch(func(session *link.Session) {
					session.Send("from " + addr + ": " + string(msg))
				})
			}

			println("client", addr, "closed")
			channel.Remove(session.Id())
		}()
	}
}
コード例 #9
0
ファイル: echo_broadcast.go プロジェクト: Jasonix/link
// This is broadcast server demo work with the echo_client.
// usage:
//     go run echo_broadcast.go
func main() {
	var addr string

	flag.StringVar(&addr, "addr", ":10010", "echo server address")
	flag.Parse()

	server, err := link.Serve("tcp", addr, link.String(link.Uint16BE))
	if err != nil {
		panic(err)
	}
	println("server start:", server.Listener().Addr().String())

	channel := link.NewChannel()
	go func() {
		for range time.Tick(time.Second * 2) {
			now := time.Now().Format("2006-01-02 15:04:05")
			channel.Broadcast("from channel: " + now)
		}
	}()

	for {
		session, err := server.Accept()
		if err != nil {
			break
		}

		go func() {
			addr := session.Conn().RemoteAddr().String()
			println("client", addr, "connected")

			session.EnableAsyncSend(1024)
			channel.Join(session)

			for {
				var msg string
				if err := session.Receive(&msg); err != nil {
					break
				}
				println(addr, "say:", msg)
				channel.Broadcast("from " + addr + ": " + string(msg))
			}

			println("client", addr, "closed")
			channel.Exit(session)
		}()
	}
}
コード例 #10
0
//初始化
func InitServer(port string) error {
	servers = make(map[string]*link.Session)
	asyncMsgs = list.New()

	db.Init()

	startSysDB()

	createRevGoroutines()

	listener, err := link.Serve("tcp", "0.0.0.0:"+port, packet.New(
		binary.SplitByUint32BE, 1024, 1024, 1024,
	))
	if err != nil {
		return err
	}

	listener.Serve(func(session *link.Session) {
		for {
			var msg packet.RAW
			if err := session.Receive(&msg); err != nil {
				break
			}

			msgID := binary.GetUint16LE(msg[:2])
			if systemProto.IsValidID(msgID) {
				//系统消息
				dealReceiveSystemMsgC2S(session, msg)
			} else if dbProto.IsValidAsyncID(msgID) {
				//异步DB消息
				asyncMsgs.PushBack(msg)
			} else {
				//同步DB消息
				useObj := revSyncMsgGoroutines[revSyncMsgGoroutineIndex]
				useObj.revMsgChan <- goroutineMsg{msg, session}
				revSyncMsgGoroutineIndex++
				if revSyncMsgGoroutineIndex == revSyncMsgGoroutineNum {
					revSyncMsgGoroutineIndex = 0
				}
			}
		}
	})

	return nil
}
コード例 #11
0
ファイル: unit_test.go プロジェクト: 616050468/link
func StartEchoBackend() (*link.Server, error) {
	backend, err := link.Serve("tcp://0.0.0.0:0", NewBackend(), link.Bytes())
	if err != nil {
		return nil, err
	}
	go backend.Loop(func(session *link.Session) {
		var msg []byte
		for {
			if err := session.Receive(&msg); err != nil {
				break
			}
			if err := session.Send(msg); err != nil {
				break
			}
		}
	})
	return backend, nil
}
コード例 #12
0
ファイル: server2.go プロジェクト: 276361270/link
func main() {
	var addr string
	var pro bool

	flag.StringVar(&addr, "addr", ":10010", "echo server address")
	flag.BoolVar(&pro, "pro", true, "use PacketPro()")
	flag.Parse()

	var codecType link.CodecType

	if pro {
		pool := binary.NewBufferPool(2, 1, 32)
		codecType = link.PacketPro(2, 1024*1024, 1024, link.LittleEndian, pool, TestCodec{})
	} else {
		codecType = link.Packet(2, 1024*1024, 1024, link.LittleEndian, TestCodec{})
	}

	server, err := link.Serve("tcp", addr, codecType)
	if err != nil {
		panic(err)
	}

	println("server start:", server.Listener().Addr().String())
	for {
		session, err := server.Accept()
		if err != nil {
			break
		}
		go func() {
			//addr := session.Conn().RemoteAddr().String()
			//println("client", addr, "connected")
			for {
				var msg []byte
				if err = session.Receive(&msg); err != nil {
					break
				}
				if err = session.Send(msg); err != nil {
					break
				}
			}
			//println("client", addr, "closed")
		}()
	}
}
コード例 #13
0
ファイル: server.go プロジェクト: houcy/link
func main() {
	var addr string

	flag.StringVar(&addr, "addr", ":10010", "echo server address")
	flag.Parse()

	server, err := link.Serve("tcp", addr, codec.Bytes(codec.Uint16BE))
	if err != nil {
		panic(err)
	}
	println("server start:", server.Listener().Addr().String())
	for {
		session, err := server.Accept()
		if err != nil {
			break
		}
		go io.Copy(session.Conn(), session.Conn())
	}
}
コード例 #14
0
ファイル: go_web.go プロジェクト: wangzun/go_web
func main() {
	funnk()
	go tcp_test()
	go func() {
		var addr string

		flag.StringVar(&addr, "addr", ":10010", "echo server address")
		flag.Parse()

		fmt.Println("addr:", addr)

		server, err := link.Serve("tcp", addr, codec.Bytes(codec.Uint16BE))
		if err != nil {
			panic(err)
		}
		println("server start:", server.Listener().Addr().String())
		for {
			session, err := server.Accept()
			if err != nil {
				break
			}
			go io.Copy(session.Conn(), session.Conn())
		}
	}()
	fmt.Println(test1)
	fmt.Println("ssssssssssssssssss")
	test_struct(&test2{})
	test_struct(&test2{})
	test_struct(&test2{})
	test_struct(&test2{})
	test_struct(&test2{})
	http.HandleFunc("/", sayhelloName)
	http.HandleFunc("/login/", login)
	http.HandleFunc("/upload/", upload)
	//	amqp_connect()
	err := http.ListenAndServe(":9099", nil) //设置监听的端口
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}

}
コード例 #15
0
ファイル: main.go プロジェクト: youxidev/GoGameServer
func startGateway() {
	listener, err := link.Serve("tcp", "0.0.0.0:"+gateway_port, packet.New(
		binary.SplitByUint32BE, 1024, 1024, 1024,
	))
	checkError(err)

	listener.Serve(func(session *link.Session) {
		session.AddCloseCallback(session, func() {
			transferProxy.SendClientSessionOffline(session.Id())
		})
		transferProxy.SendClientSessionOnline(session)

		var msg packet.RAW
		for {
			if err := session.Receive(&msg); err != nil {
				break
			}
			transferProxy.SendToTransferServer(msg, session)
		}
	})
}
コード例 #16
0
// This is broadcast server demo work with the echo_client.
// usage:
//     go run echo_broadcast.go
func main() {
	flag.Parse()

	protocol := packet.New(
		binary.SplitByUint16BE, 1024, 1024, 1024,
	)

	server, err := link.Serve("tcp", *addr, protocol)
	if err != nil {
		panic(err)
	}
	println("server start:", server.Listener().Addr().String())

	channel := link.NewChannel(link.DefaultBroadcast)
	go func() {
		for range time.Tick(time.Second * 2) {
			now := time.Now().Format("2006-01-02 15:04:05")
			channel.Broadcast(packet.RAW("from channel: " + now))
		}
	}()

	server.Serve(func(session *link.Session) {
		addr := session.Conn().RemoteAddr().String()
		println("client", addr, "connected")
		channel.Join(session, nil)

		for {
			var msg packet.RAW
			if err := session.Receive(&msg); err != nil {
				break
			}
			println(addr, "say:", string(msg))
			channel.Broadcast(packet.RAW("from " + addr + ": " + string(msg)))
		}

		println("client", addr, "closed")
		channel.Exit(session)
	})
}
コード例 #17
0
ファイル: startup.go プロジェクト: yicaoyimuys/GoGameServer
// 开启服务器监听
func Listener(network, address string, codecType link.CodecType, acceptFunc func(*link.Session), dispatch dispatch.DispatchInterface) error {
	listener, err := link.Serve(network, address, codecType)
	if err != nil {
		return err
	}

	go func() {
		for {
			session, err := listener.Accept()
			if err != nil {
				break
			}

			go acceptFunc(session)

			if dispatch != nil {
				go sessionReceive(session, dispatch)
			}
		}
	}()
	return nil
}
コード例 #18
0
ファイル: all_test.go プロジェクト: houcy/link
func SessionTest(t *testing.T, codecType link.CodecType, test func(*testing.T, *link.Session)) {
	server, err := link.Serve("tcp", "0.0.0.0:0", Bytes(Uint16BE))
	unitest.NotError(t, err)
	addr := server.Listener().Addr().String()

	serverWait := new(sync.WaitGroup)
	go func() {
		for {
			session, err := server.Accept()
			if err != nil {
				break
			}
			serverWait.Add(1)
			go func() {
				io.Copy(session.Conn(), session.Conn())
				serverWait.Done()
			}()
		}
	}()

	clientWait := new(sync.WaitGroup)
	for i := 0; i < 60; i++ {
		clientWait.Add(1)
		go func() {
			session, err := link.Connect("tcp", addr, codecType)
			unitest.NotError(t, err)
			test(t, session)
			session.Close()
			clientWait.Done()
		}()
	}
	clientWait.Wait()

	server.Stop()
	serverWait.Wait()

	MakeSureSessionGoroutineExit(t)
}
コード例 #19
0
//初始化
func InitServer(port string) error {
	servers = make(map[string][]*link.Session)
	gameConsistent = hashs.NewConsistent()
	gameUserSessions = make(map[uint64]int)

	listener, err := link.Serve("tcp", "0.0.0.0:"+port, packet.New(
		binary.SplitByUint32BE, 1024, 1024, 1024,
	))
	if err != nil {
		return err
	}

	listener.Serve(func(session *link.Session) {
		var msg packet.RAW
		for {
			if err := session.Receive(&msg); err != nil {
				break
			}
			dealReceiveMsgC2S(session, msg)
		}
	})

	return nil
}
コード例 #20
0
ファイル: unit_test.go プロジェクト: 616050468/link
func Test_Broadcast(t *testing.T) {
	var (
		clientNum     = 20
		packetNum     = 2000
		channel       = NewChannel(link.Bytes())
		broadcastMsg  []byte
		broadcastWait sync.WaitGroup
		clientWait    sync.WaitGroup
	)

	backend, err := link.Serve("tcp://0.0.0.0:0", NewBackend(), link.Bytes())
	unitest.NotError(t, err)

	go backend.Loop(func(session *link.Session) {
		channel.Join(session)
		clientWait.Done()
		for {
			var msg []byte
			if err := session.Receive(&msg); err != nil {
				break
			}
			unitest.Pass(t, bytes.Equal(msg, broadcastMsg))
			broadcastWait.Done()
		}
	})

	clientWait.Add(clientNum)
	go func() {
		clientWait.Wait()
		for i := 0; i < packetNum; i++ {
			broadcastMsg = RandBytes(1024)
			channel.Broadcast(broadcastMsg)
			broadcastWait.Add(clientNum)
			broadcastWait.Wait()
		}
	}()

	gateway := StartTestGateway(t, backend.Listener().Addr().String())
	gatewayAddr := gateway.server.Listener().Addr().String()

	var wg sync.WaitGroup
	for i := 0; i < clientNum; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()

			client, err := link.Connect("tcp://"+gatewayAddr, link.Packet(link.Uint16BE), link.Bytes())
			unitest.NotError(t, err)

			for j := 0; j < packetNum; j++ {
				var msg []byte
				err := client.Receive(&msg)
				unitest.NotError(t, err)

				err = client.Send(msg)
				unitest.NotError(t, err)
			}

			client.Close()
		}()
	}
	wg.Wait()

	gateway.Stop()
	backend.Stop()

	time.Sleep(time.Second * 2)
	MakeSureSessionGoroutineExit(t)
}