Пример #1
0
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)
		}
	})
}
Пример #2
0
func main() {
	flag.Parse()

	session, err := link.Connect("tcp", *addr, packet.New(
		binary.SplitByUint16BE, 1024, 1024, 1024,
	))
	if err != nil {
		panic(err)
	}

	go func() {
		var msg packet.RAW
		for {
			if err := session.Receive(&msg); err != nil {
				break
			}
			fmt.Printf("%s\n", msg)
		}
	}()

	for {
		var msg string
		if _, err := fmt.Scanf("%s\n", &msg); err != nil {
			break
		}
		if err = session.Send(packet.RAW(msg)); err != nil {
			break
		}
	}

	session.Close()
	println("bye")
}
Пример #3
0
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")
	})
}
Пример #4
0
//初始化
func InitClient(ip string, port string) error {
	//连接DB服务器
	addr := ip + ":" + port
	client, err := link.Connect("tcp", addr, packet.New(binary.SplitByUint32BE, 1024, 1024, 1024))
	if err != nil {
		return err
	}

	session = client
	go dealReceiveMsgS2C()
	ConnectDBServer()

	return nil
}
Пример #5
0
func client(initWait *sync.WaitGroup, conn *CountConn, startChan chan int, timeout time.Time, msg packet.RAW) {
	client := link.NewSession(0, packet.NewConn(conn, packet.New(
		binary.SplitByUint16BE, 1024, 1024, 1024,
	)))

	var wg sync.WaitGroup

	wg.Add(1)
	go func() {
		defer wg.Done()
		initWait.Done()
		<-startChan

		for {
			outMsg := msg
			if *randsize {
				outMsg = packet.RAW(make([]byte, rand.Intn(*messageSize)))
			}
			if err := client.Send(outMsg); err != nil {
				if timeout.After(time.Now()) {
					println("send error:", err.Error())
				}
				break
			}
			atomic.AddUint32(&conn.SendCount, 1)
		}
	}()

	wg.Add(1)
	go func() {
		defer wg.Done()
		initWait.Done()
		<-startChan

		var inMsg packet.RAW
		for {
			if err := client.Receive(&inMsg); err != nil {
				if timeout.After(time.Now()) {
					println("recv error:", err.Error())
				}
				break
			}
			atomic.AddUint32(&conn.RecvCount, 1)
		}
	}()

	wg.Wait()
}
Пример #6
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
}
Пример #7
0
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)
		}
	})
}
Пример #8
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)
	})
}
Пример #9
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
}
Пример #10
0
	"testing"
	"time"

	"github.com/funny/binary"
	"github.com/funny/link"
	"github.com/funny/link/packet"
	"github.com/funny/link/stream"
	"github.com/funny/unitest"
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

var protocol = packet.New(
	binary.SplitByUint32BE, 1024, 1024, 1024,
)

func RandBytes(n int) []byte {
	n = rand.Intn(n)
	b := make([]byte, n)
	for i := 0; i < n; i++ {
		b[i] = byte(rand.Intn(255))
	}
	return b
}

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,
	))