Blame view

components/net/server.go 1.77 KB
fee11bff   zhangqijia   客户端无法使用grpc热更,不用g...
1
2
3
4
  package net
  
  import (
  	"fmt"
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
5
  	"github.com/golang/protobuf/proto"
fee11bff   zhangqijia   客户端无法使用grpc热更,不用g...
6
  	"net"
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
7
  	"pro2d/components/db"
c92a54a3   zhangqijia   循环引用的问题
8
  	"pro2d/components/etcd"
fee11bff   zhangqijia   客户端无法使用grpc热更,不用g...
9
  	"pro2d/conf"
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
10
11
  	"pro2d/models"
  	"pro2d/protos/pb"
fee11bff   zhangqijia   客户端无法使用grpc热更,不用g...
12
13
14
15
  	"pro2d/utils"
  	"sync"
  )
  
c92a54a3   zhangqijia   循环引用的问题
16
17
18
  type ActionHandler func (msg *MsgPkg)  (int32, proto.Message)
  var ActionMap map[pb.ProtoCode]ActionHandler
  
fee11bff   zhangqijia   客户端无法使用grpc热更,不用g...
19
20
21
  type Server struct {
  	SConf      *conf.SConf
  	Clients *sync.Map
c92a54a3   zhangqijia   循环引用的问题
22
  	EtcdClient *etcd.EtcdClient
fee11bff   zhangqijia   客户端无法使用grpc热更,不用g...
23
24
25
26
27
28
29
30
31
32
33
34
  
  }
  
  func NewServer(sConf *conf.SConf) *Server {
  	return &Server{
  		SConf:      sConf,
  		Clients:    new(sync.Map),
  	}
  }
  
  func (s *Server) OnRecv(msg *MsgPkg) {
  	utils.Sugar.Debugf("cmd: %d, data: %s", msg.Head.Cmd, msg.Body)
c92a54a3   zhangqijia   循环引用的问题
35
  	if md, ok := ActionMap[pb.ProtoCode(msg.Head.Cmd)]; ok {
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
36
37
38
39
40
41
42
43
44
45
  		errCode, protomsg := md(msg)
  		rsp, err := proto.Marshal(protomsg)
  		if err != nil {
  			msg.Conn.SendMsgByCode(-100, msg.Head.Cmd, nil)
  			return
  		}
  		msg.Conn.SendMsgByCode(errCode, msg.Head.Cmd, rsp)
  		return
  	}
  	utils.Sugar.Errorf("protocode not handler: %d", msg.Head.Cmd)
fee11bff   zhangqijia   客户端无法使用grpc热更,不用g...
46
47
48
49
50
51
52
  }
  
  func (s *Server) OnClose(conn *Connection) {
  	s.Clients.Delete(conn.Id)
  }
  
  func (s *Server)Start() error {
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
53
54
55
56
57
  	//mongo 初始化
  	db.MongoDatabase = db.MongoClient.Database(conf.GlobalConf.AccountConf.DBName)
  	models.InitGameServerModels()
  
  	//Etcd 初始化
c92a54a3   zhangqijia   循环引用的问题
58
59
  	s.EtcdClient = etcd.NewEtcdClient(conf.GlobalConf.Etcd)
  	s.EtcdClient.PutWithLeasePrefix(conf.GlobalConf.GameConf.Name, conf.GlobalConf.GameConf.ID, fmt.Sprintf("%s:%d", conf.GlobalConf.GameConf.IP, conf.GlobalConf.GameConf.Port), 5)
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
60
  
fee11bff   zhangqijia   客户端无法使用grpc热更,不用g...
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  	port := fmt.Sprintf(":%d", s.SConf.Port)
  	l, err := net.Listen("tcp", port)
  	if err != nil {
  		return err
  	}
  
  	utils.Sugar.Debugf("listen on %s\n", port)
  	id := 0
  	for {
  		conn, err := l.Accept()
  		if err != nil {
  			return err
  		}
  
  		id++
  		client := NewConn(id, conn, s)
  		s.Clients.Store(id, client)
  		go client.Start()
  	}
  }
  
  func (s *Server)Stop()  {
  }