Exemplo n.º 1
0
func (s *UdpServer) Start() {
	udpAddr, err := net.ResolveUDPAddr("udp", s.Addr)
	if err != nil {
		gts.Error("udp server addr(%s) is error:%q", s.Addr, err)
		return
	}

	gts.Info(s.Name + " udpserver is starting...")
	sock, _err := net.ListenUDP("udp", udpAddr)
	if _err != nil {
		gts.Error("udp server addr(%s) is error2:%q", s.Addr, err)
		return
	}

	go func() {
		buffer := make([]byte, s.udp_read_buffer_size)
		for {
			defer sock.Close()
			if s.Stop_ {
				return
			}

			n, fromAddr, err := sock.ReadFromUDP(buffer)
			key := fromAddr.String()
			if err == nil {
				//log.Println("recv", n, from)
				gts.Trace("udp connect from: %s", fromAddr)
				udpConn, ok := s.udpAddrs[key]
				if !ok {
					newcid := s.AllocId()
					udpConn = s.makeConn(
						newcid,
						sock,
						s.RoutePackHandle.GetEndianer(),
						fromAddr,
					)

					gts.Trace("new udp connection")
					udpConn.SetReceiveCallback(s.receiveUdpBytes)
					s.udpAddrs[key] = udpConn
				}

				udpConn.ConnReader(buffer[0:n])
			} else {
				e, ok := err.(net.Error)
				if !ok || !e.Timeout() {
					gts.Trace("recv errorserver:%s,%q.", key, err.Error())
					delete(s.udpAddrs, key)
				}
			}
		}
	}()
}
Exemplo n.º 2
0
func NewLogicFromIni(
	c *iniconfig.ConfigFile,
	routepack IRoutePack, wm IWormholeManager,
	makeWormhole NewWormholeFunc) *Logic {

	section := "Default"

	logconf, err := c.GetString(section, "logConfigFile")
	if err != nil {
		logconf = ""
	}
	gts.SetLogger(&logconf)

	//start grid service
	name, err := c.GetString(section, "name")
	if err != nil {
		gts.Error(err.Error())
		return nil
	}

	serverId, err := c.GetInt(section, "serverId")
	if err != nil {
		gts.Error(err.Error())
		return nil
	}

	group, err := c.GetString(section, "group")
	if err != nil {
		group = "0"
	}

	/*
	   endian, err := c.GetInt(section, "endian")
	   if err == nil {
	       routepack.SetEndianer(gts.GetEndianer(endian))
	   } else {
	       routepack.SetEndianer(gts.GetEndianer(gts.LittleEndian))
	   }

	   autoDuration, err := c.GetInt(section, "autoReconnectDuration")
	   if err != nil {
	       autoDuration = 5
	   }
	   autoReconnectDuration := time.Duration(autoDuration) * time.Second
	*/

	ls := NewLogic(name, serverId, routepack, wm, makeWormhole, group)

	return ls
}
Exemplo n.º 3
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	flag.Parse()

	c, err := iniconfig.ReadConfigFile(*logicConf)
	if err != nil {
		gts.Error(err.Error())
		return
	}

	var endianer gts.IEndianer
	section := "Default"
	endian, err := c.GetInt(section, "endian")
	if err == nil {
		endianer = gts.GetEndianer(endian)
	} else {
		endianer = gts.GetEndianer(gts.LittleEndian)
	}
	routepack := wormhole.NewRoutePack(endianer)
	wormholes := server.NewAgentManager(routepack)

	logic := server.NewLogicFromIni(c, routepack, wormholes, NewLogicToAgent)

	logic.ConnectFromIni(c)

	gts.Info("----------------logic server is running!-----------------", logic.GetServerId())

	quit := make(chan bool)
	go exit(quit)

	<-quit
}
Exemplo n.º 4
0
func (c *TcpConnection) ConnReader() {
	gts.Trace("read_buffer_size:", c.read_buffer_size)
	buffer := make([]byte, c.read_buffer_size)
	for {
		bytesRead, err := c.conn.Read(buffer)

		if err != nil {
			gts.Error("tcpconnection connreader error: ", err, bytesRead)
			if c.closeCallback != nil {
				c.closeCallback(c.id)
			}
			break
		}

		//gts.Trace("tcpConnReader read to buff:%d, % X",bytesRead, buffer[:bytesRead])
		gts.Trace("tcpConnReader read to buff:%q", buffer[:bytesRead])
		c.Stream.Write(buffer[0:bytesRead])
		gts.Trace("\n%q", c.Stream.Bytes())
		c.receiveCallback(c)

		//n, dps := c.routePack.Fetch(c)
		//gts.Trace("fetch message number", n)
		//if n > 0 {
		//c.receivePacketCallback(c.id, dps)
		//}
	}
}
Exemplo n.º 5
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	flag.Parse()

	c, err := iniconfig.ReadConfigFile(*clientConf)
	if err != nil {
		gts.Error(err.Error())
		return
	}

	section := "Default"

	logconf, err := c.GetString(section, "logConfigFile")
	if err != nil {
		logconf = ""
	}
	gts.SetLogger(&logconf)

	clientTcpAddr, err := c.GetString(section, "clientTcpAddr")
	if err != nil {
		gts.Error(err.Error())
		return
	}

	var endianer gts.IEndianer
	endian, err := c.GetInt(section, "endian")
	if err == nil {
		endianer = gts.GetEndianer(endian)
	} else {
		endianer = gts.GetEndianer(gts.LittleEndian)
	}
	routepack := wormhole.NewRoutePack(endianer)
	var cwormholes wormhole.IWormholeManager

	client = wormhole.NewClient(clientTcpAddr, routepack, cwormholes, NewClientWormhole, wormhole.EWORMHOLE_TYPE_CLIENT)
	client.Connect()

	gts.Info("----------------client connect to %s,%s-----------------", clientTcpAddr)

	quit := make(chan bool)
	go exit(quit)

	<-quit
}
Exemplo n.º 6
0
func (s *TcpServer) Start() {
	gts.Info(s.Name + " tcpserver is starting...")

	s.Stop_ = false
	//todo: MaxConns don't proccess
	netListen, error := net.Listen("tcp", s.Addr)
	if error != nil {
		gts.Error(error)
		return
	}

	gts.Info("listen with :", s.Addr)
	gts.Info(s.Name + " tcpserver is started !!!")

	go func() {
		//defer函数退出时执行
		defer netListen.Close()

		for {
			gts.Trace("Waiting for connection")
			if s.Stop_ {
				break
			}

			connection, err := netListen.Accept()

			if err != nil {
				gts.Error("Transport error: ", err)
			} else {
				gts.Debug("%v is connection!", connection.RemoteAddr())

				newcid := s.AllocId()
				if newcid == 0 {
					gts.Warn("connection num is more than ", s.MaxConns)
				} else {
					gts.Trace("//////////////////////newcid:", newcid)
					tcpConn := s.makeConn(newcid, connection,
						s.RoutePackHandle.GetEndianer())
					tcpConn.SetReceiveCallback(s.receiveBytes)
				}
			}
		}
	}()
}
Exemplo n.º 7
0
func (wm *AgentManager) Send(guin int, method int, data []byte) {
	agentId, _, _ := ParseGuin(guin)
	wh, ok := wm.Get(agentId)
	if ok {
		wh.Send(guin, method, data)
		return
	}

	gts.Error("agentmanager:guin don't find wormhole:%d", guin)
}
Exemplo n.º 8
0
func (c *UdpConnection) Connect(addr string) bool {
	udpAddr, err := net.ResolveUDPAddr("udp", addr)
	if err != nil {
		gts.Error("dial udp server addr(%s) is error:%q", udpAddr, err)
		return false
	}

	conn, err := net.DialUDP("udp", nil, udpAddr)
	if err != nil {
		gts.Warn("net.Dial to %s:%q", addr, err)
		return false
	}

	gts.Trace("dial to udp(%s) is ok.", addr)

	c.conn = conn
	go func() {

		go func() {
			buffer := make([]byte, 1024)
			for {
				defer conn.Close()
				if c.closeded {
					return
				}

				n, err := conn.Read(buffer[0:])
				if err == nil {
					c.ConnReader(buffer[0:n])
				} else {
					e, ok := err.(net.Error)
					if !ok || !e.Timeout() {
						gts.Trace("recv errorconn:%q.", err.Error())
						c.quitConnect <- true
						return
					}
				}
			}
		}()

		go c.ConnSenderClient()

		<-c.quitConnect
	}()
	return true
}
Exemplo n.º 9
0
func (ls *Logic) ConnectFromIni(c *iniconfig.ConfigFile) {
	gts.Trace("connect from ini")
	//make some connection to game server
	for i := 1; i < 50; i++ {
		section := "Agent" + strconv.Itoa(i)
		if !c.HasSection(section) {
			continue
		}

		enabled, err := c.GetBool(section, "enabled")
		if err == nil && !enabled {
			continue
		}

		/*
		   serverId, err := c.GetInt(section, "serverId")
		   if err != nil {
		       gts.Error(err.Error())
		       continue
		   }

		   gname, err := c.GetString(section, "name")
		   if err != nil {
		       gts.Error(err.Error())
		       continue
		   }
		*/

		tcpAddr, err := c.GetString(section, "tcpAddr")
		if err != nil {
			gts.Error(err.Error())
			continue
		}

		ls.ConnectAgent(tcpAddr)
	}
}
Exemplo n.º 10
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	flag.Parse()

	c, err := iniconfig.ReadConfigFile(*agentConf)
	if err != nil {
		gts.Error(err.Error())
		return
	}

	var endianer gts.IEndianer
	section := "Default"
	endian, err := c.GetInt(section, "endian")
	if err == nil {
		endianer = gts.GetEndianer(endian)
	} else {
		endianer = gts.GetEndianer(gts.LittleEndian)
	}
	routepack := wormhole.NewRoutePack(endianer)

	cwormholes := wormhole.NewWormholeManager(routepack, wormhole.MAX_CONNECTIONS, wormhole.EWORMHOLE_TYPE_CLIENT)

	dispatcher := server.NewDispatcher(routepack)
	lwormholes := server.NewLogicManager(routepack, dispatcher)
	agent := server.NewAgentFromIni(c, routepack, cwormholes, lwormholes, server.NewAgentToClientWormhole, server.NewAgentToLogicWormhole)

	agent.Start()

	gts.Info("----------------agent server is running!-----------------", agent.GetServerId())

	quit := make(chan bool)
	go exit(quit)

	<-quit
}
Exemplo n.º 11
0
func NewAgentFromIni(
	c *iniconfig.ConfigFile,
	routepack IRoutePack,
	clientWormholes IWormholeManager,
	logicWormholes IWormholeManager,
	makeClientWormhole NewWormholeFunc,
	makeLogicWormhole NewWormholeFunc) *Agent {

	section := "Default"

	logconf, err := c.GetString(section, "logConfigFile")
	if err != nil {
		logconf = ""
	}
	gts.SetLogger(&logconf)

	//start grid service
	name, err := c.GetString(section, "name")
	if err != nil {
		gts.Error(err.Error())
		return nil
	}

	serverId, err := c.GetInt(section, "serverId")
	if err != nil {
		gts.Error(err.Error())
		return nil
	}

	clientTcpAddr, err := c.GetString(section, "clientTcpAddr")
	if err != nil {
		gts.Error(err.Error())
		return nil
	}

	clientUdpAddr, err := c.GetString(section, "clientUdpAddr")
	if err != nil {
		gts.Warn(err.Error())
	}

	logicTcpAddr, err := c.GetString(section, "logicTcpAddr")
	if err != nil {
		gts.Error(err.Error())
		return nil
	}

	logicUdpAddr, err := c.GetString(section, "logicUdpAddr")
	if err != nil {
		gts.Warn(err.Error())
	}

	maxConnections, err := c.GetInt(section, "maxConnections")
	if err != nil {
		maxConnections = 1000
	}

	/*
	   endian, err := c.GetInt(section, "endian")
	   if err == nil {
	       routepack.SetEndianer(gts.GetEndianer(endian))
	   } else {
	       routepack.SetEndianer(gts.GetEndianer(gts.LittleEndian))
	   }

	   autoDuration, err := c.GetInt(section, "autoReconnectDuration")
	   if err != nil {
	       autoDuration = 5
	   }
	   autoReconnectDuration := time.Duration(autoDuration) * time.Second
	*/

	return NewAgent(
		name, serverId,
		clientTcpAddr, clientUdpAddr,
		logicTcpAddr, logicUdpAddr,
		maxConnections, routepack,
		clientWormholes,
		logicWormholes,
		makeClientWormhole,
		makeLogicWormhole)
}