Пример #1
0
func (c *TcpConnection) Connect(addr string) bool {
	gts.Trace("connect to tcpserver[info]:", addr)

	conn, err := net.Dial("tcp", addr)
	if err != nil {
		print(err)
		gts.Warn("net.Dial to %s:%q", addr, err)
		return false
	} else {
		gts.Trace("tcp dial to %s is ok. ", addr)
	}

	//go func() {
	//defer conn.Close()

	c.conn = conn

	//创建go的线程 使用Goroutine
	go c.ConnSender()
	go c.ConnReader()

	//<-c.Quit
	//}()
	return true
}
Пример #2
0
func (r *Dispatcher) Dispatch(dp *RoutePacket) (wh IWormhole) {
	r.dlock.Lock()
	defer r.dlock.Unlock()

	var code, group int
	if len(dp.Data) > 2 {
		code = int(r.routepack.GetEndianer().Uint16(dp.Data))
		group = int(code / 100)
		gts.Trace("msg.group:", group)
	} else {
		group = 0
	}

	hands, ok := r.handlers[group]
	if !ok {
		hands, ok = r.handlers[0]
		if !ok {
			hands = []IWormhole{}
		}
	}

	hlen := len(hands)
	if hlen == 1 {
		return hands[0]
	} else if hlen > 1 {
		return hands[int(dp.Guin)%hlen]
	} else {
		gts.Warn("data packet group is not exists!", code, group)
		return nil
	}
}
Пример #3
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
}
Пример #4
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)
				}
			}
		}
	}()
}
Пример #5
0
func (c *Client) receiveTcpPackets(conn IConnection, dps []*RoutePacket) {
    for _, dp := range dps {
        if dp.Type == EPACKET_TYPE_HELLO {
            gts.Trace("client receive tcp hello:%q", dp)
            c.guin = dp.Guin
            c.initWormhole(dp, conn)

            fromType := EWormholeType(dp.Data[0])
            c.wormhole.SetFromType(fromType)

            c.wormhole.Init()

            if len(dp.Data) > 1 {
                c.udpAddr = string(dp.Data[1:])
            }

        //} else if dp.Type == EPACKET_TYPE_UDP_SERVER {
            //c.guin = dp.Guin
            //c.udpAddr = string(dp.Data)

            //连接udp server
            c.udpConn = NewUdpConnection(1,nil,c.routepack.GetEndianer(),nil)

            if len(c.udpAddr) > 0 {
                gts.Trace("client send udp hello:(%s), wormtype:(%d).", c.udpAddr, c.wormType)
                if c.udpConn.Connect(c.udpAddr) {
                    //hello to tcp server
                    packet := &RoutePacket {
                        Type:   EPACKET_TYPE_HELLO,
                        Guin:   c.guin,
                        Data:   []byte{byte(c.wormType)},
                    }
                    c.udpConn.Send(c.routepack.Pack(packet))

                    c.initWormhole(dp, c.udpConn)
                    print("\n-----------------------------------------------------------\n")

                } else {
                    gts.Warn("dial to udp server lost:%s", c.udpAddr)
                }
            }
        }
    }
}
Пример #6
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)
}