Example #1
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
}
Example #2
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)
				}
			}
		}
	}()
}
Example #3
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)
				}
			}
		}
	}()
}
Example #4
0
func (c *Client) Connect() {
    c.tcpConn = NewTcpConnection(1, nil, c.routepack.GetEndianer())
    c.tcpConn.SetReceiveCallback(c.receiveTcpBytes)

    if c.tcpConn.Connect(c.tcpAddr) {
        //连接上服务器
        gts.Info("连接上tcpserver:%s", c.tcpAddr)
        gts.Trace("client send tcp hello:%s, wormtype:%d", c.tcpAddr, c.wormType)
        //hello to tcp server
        packet := &RoutePacket {
            Type:   EPACKET_TYPE_HELLO,
            Guin:   c.guin,
            Data:   []byte{byte(c.wormType)},
        }
        c.tcpConn.Send(c.routepack.Pack(packet))
    }
}
Example #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
}
Example #6
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
}
Example #7
0
func (s *Agent) Start() {
	gts.Info(s.Name + " is starting...")

	s.clientServer.Start()
	s.logicServer.Start()
}