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
}
Exemple #2
0
func (wh *Wormhole) SendRaw(bytes []byte) {
	if wh.closeded {
		return
	}

	if len(bytes) <= 1024 {
		//if len(bytes) < 0 {
		gts.Trace("send send protocol type:%d,%d,%d", wh.GetGuin(), wh.sendConnection.GetType(), wh.sendConnection.GetProtocolType())
		wh.sendConnection.Send(bytes)

	} else {
		//gts.Trace("send ctrl protocol type:",wh.ctrlConnection)
		gts.Trace("send ctrl protocol type:%d,%d,%d", wh.GetGuin(), wh.ctrlConnection.GetType(), wh.ctrlConnection.GetProtocolType())
		gts.Trace("send ctrl:%q", bytes)

		//v := reflect.ValueOf(wh.ctrlConnection)
		//t := reflect.TypeOf(wh.ctrlConnection)

		//gts.Trace("Type:", t)
		//gts.Trace("Value:", v)
		//gts.Trace("Kind:", t.Kind())
		//gts.Trace("Kind:", wh.ctrlConnection.GetId())

		wh.ctrlConnection.Send(bytes)
	}
}
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)
		//}
	}
}
func (c *TcpConnection) Close() {
	gts.Trace("tcp connection close1")
	if !c.closeded {
		c.closeded = true
		c.quit <- true
	}
	gts.Trace("tcp connection close2")
}
Exemple #5
0
func (s *TcpServer) receiveBytes(conn IConnection) {
	gts.Trace("tcp server receiveBytes:% X", conn.GetBuffer().Stream.Bytes())
	n, dps := s.RoutePackHandle.Fetch(conn.GetBuffer())
	gts.Trace("tcp server receivePackets:", n)
	if n > 0 {
		s.receivePackets(conn, dps)
	}
}
Exemple #6
0
func (aw *LogicToAgent) ProcessPackets(dps []*wormhole.RoutePacket) {
	gts.Trace("logicToAgent processpackets receive %d route packets", len(dps))

	for _, dp := range dps {
		gts.Trace("%q", dp)
		//gts.Trace("%q",gts.utils.ByteString(dp.Data))
		aw.SendPacket(dp)
	}
}
func (c *UdpConnection) Close() {
	gts.Trace("udp connection close1")
	if !c.closeded {
		c.closeded = true
		c.quitInterval <- true
		c.quitSender <- true
	}
	gts.Trace("udp connection close2")
}
func (aw *LogicToAgentWormhole) SetGroup(group string) {
	aw.group = group

	gts.Trace("logic register to agent:group(%s)", group)
	packet := &RoutePacket{
		Type: EPACKET_TYPE_LOGIC_REGISTER,
		Guin: 0,
		Data: []byte(group),
	}
	gts.Trace("%q", packet)
	aw.SendPacket(packet)
}
Exemple #9
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)
				}
			}
		}
	}()
}
Exemple #10
0
func (uf *UdpFrame) Unpack(rw *gts.RWStream) bool {
	data, num := rw.Read(3)
	if num == 0 {
		return false
	}

	uf.OrderNo = int(rw.Endianer.Uint16(data[:2]))
	uf.Flag = data[2]
	uf.Count = 0

	switch uf.Flag {
	case UDPFRAME_FLAG_DATA:
		if rw.Len() < 2 {
			rw.SetPos(-3)
			gts.Trace("setpos:-3")
			return false
		}

		buf, n := rw.Read(2)
		if n < 2 {
			return false
		}
		length := rw.Endianer.Uint16(buf)
		leng := int(length)
		//length,err := rw.ReadUint16()
		gts.Trace("udpframe.unpack:%d", leng)
		//if err != nil || rw.Len() < int(leng) {
		if rw.Len() < int(leng) {
			rw.SetPos(-5)
			gts.Trace("setpos:-5, %d", leng)
			return false
		}

		data, _ := rw.Read(int(leng))
		uf.Data = make([]byte, leng)
		copy(uf.Data, data)

		/*
		   uf.Buf = make([]byte, leng + 5)
		   copy(uf.Buf, data)
		   copy(uf.Buf[3:], buf)
		   copy(uf.Buf[5:], uf.Data)
		*/
		return true

	case UDPFRAME_FLAG_DATA_GROUP:
		return false

	default:
		return true
	}
}
Exemple #11
0
func (c *UdpConnection) receiveBytes(frame *UdpFrame) {
	if frame.OrderNo%UDP_SEND_ACT_COUNT == 0 {
		c.sendFrame(&UdpFrame{
			OrderNo: frame.OrderNo,
			Flag:    UDPFRAME_FLAG_ACT,
		})
		gts.Trace("c.lastValidOrderNo:%d", c.lastValidOrderNo)
		gts.Trace("c.lastOrderNo:%d", c.lastOrderNo)
		gts.Trace("c.reqCache:%d,%d", frame.OrderNo, UDPFRAME_FLAG_ACT)
	}

	println("receivebytes:", frame.OrderNo, "\n")
	c.Stream.Write(frame.Data)
	c.receiveCallback(c)
}
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
	}
}
Exemple #13
0
func (c *UdpConnection) reader() {
	for {
		<-c.receiveChan

		for {
			frame := &UdpFrame{}
			if frame.Unpack(c.udpStream) {
				//gts.Trace("%d,%d,%q", frame.OrderNo, frame.Flag, frame.Data)
				//gts.Trace("////////////////////////////////////")
				switch frame.Flag {
				case UDPFRAME_FLAG_ACT:
					//收到包确认frame,将相应的sendcache 删除
					for i := frame.OrderNo; i > 1; i-- {
						if _, ok := c.sendCache.Get(i); ok {
							c.sendCache.Delete(i)
						} else {
							break
						}
					}
					gts.Trace("-------------%d--------------", c.sendCache.Length())

				case UDPFRAME_FLAG_REQ_RETRY:
					gts.Trace("----------recv req:%d------------", frame.OrderNo)
					if rframe, ok := c.sendCache.Get(frame.OrderNo); ok {
						c.sendFrame(rframe.(*UdpFrame))
					} else {
						c.sendFrame(&UdpFrame{
							OrderNo: frame.OrderNo,
							Flag:    UDPFRAME_FLAG_NOT_EXISTS,
						})
					}

				case UDPFRAME_FLAG_NOT_EXISTS:
					//如果该包丢失,就当接受到一个正常frame处理
					frame.Data = []byte{}
					c.recvFrame(frame)

				default:
					c.recvFrame(frame)

				}
			} else {
				break
			}
		}
	}
}
Exemple #14
0
func (wh *Wormhole) dataClosed(id int) {
	gts.Trace("dataClosed")

	if wh.sendConnection == nil {
		//wh.dataConnection.GetType() == wh.sendConnection.GetType() {
		wh.sendConnection = wh.ctrlConnection
	}
}
Exemple #15
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
}
Exemple #16
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)
                }
            }
        }
    }
}
Exemple #17
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)
				}
			}
		}
	}()
}
func (acw *AgentToClientWormhole) ProcessPackets(dps []*RoutePacket) {
	gts.Trace("agenttoclientwormhole processpackets receive %d packets", len(dps))

	for _, dp := range dps {
		gts.Debug("%q", dp)
		gts.Trace("guin:", acw.GetGuin())

		dp.Guin = acw.GetGuin()
		dp.Type = dp.Type | 1

		acw.SendPacket(dp)

		//转发给logic server
		//根据guin进行hash运算非配到相应的logic server
		if server, ok := acw.GetManager().GetServer().(*Agent); ok {
			server.LogicWormholes.(*LogicManager).Delay(dp)
		}
	}
}
Exemple #19
0
func (c *UdpConnection) addReq(rate int) {
	rate = 1
	gts.Trace("addreq:::%d", c.lastOrderNo)
	c.reqCache.Set(c.lastOrderNo, &UdpFrame{
		OrderNo: c.lastOrderNo,
		//Data: make([]byte),
		Flag:  UDPFRAME_FLAG_REQ_RETRY,
		Count: UDP_REQ_CHECK_COUNT * rate,
	})
}
Exemple #20
0
func (s *UdpServer) receiveUdpPackets(conn IConnection, dps []*RoutePacket) {
	for _, dp := range dps {
		if dp.Type == EPACKET_TYPE_HELLO {
			gts.Trace("server receive udp hello:%q", dp)
			//接到连接方hello包
			var guin int
			var wh IWormhole
			var ok bool

			if dp.Guin > 0 {
				//TODO:重连处理
				//如果客户端hello是发送的guin有,则表示是重连,需要处理重连逻辑
				//比如在一定时间内可以重新连接会原有wormhole

				guin = dp.Guin
				if wh, ok = s.Wormholes.Get(guin); ok {
					if wh.GetState() == ECONN_STATE_SUSPEND {
						wh.SetState(ECONN_STATE_ACTIVE)
					} else if wh.GetState() == ECONN_STATE_DISCONNTCT {
						wh = nil
					}
				}
			} else {
				guin = GenerateGuin(s.ServerId, int(conn.GetId()))
			}
			if wh == nil {
				wh = s.makeWormhole(guin, s.Wormholes, s.RoutePackHandle)
				s.Wormholes.Add(wh)
			}

			//将该连接绑定到wormhole,
			//并且connection的receivebytes将被wormhole接管
			//该函数将不会被该connection调用
			wh.AddConnection(conn, ECONN_TYPE_DATA)
			gts.Trace("has wormholes:%d\n-----------------------------------", s.Wormholes.Length())

			fromType := EWormholeType(dp.Data[0])
			wh.SetFromType(fromType)
			break
		}
	}
}
func (wm *WormholeManager) Remove(guin int) {
	wm.wmlock.Lock()
	defer wm.wmlock.Unlock()

	print("wormholemanager remove")

	gts.Trace("wormholemanager remove")
	if _, ok := wm.wormholes[guin]; ok {
		delete(wm.wormholes, guin)
	}
}
func (alw *AgentToLogicWormhole) ProcessPackets(dps []*RoutePacket) {
	gts.Trace("agenttologicwormhole processpackets receive %d route packets", len(dps))
	for _, dp := range dps {
		if dp.Type == EPACKET_TYPE_LOGIC_REGISTER {
			lm := alw.GetManager().(*LogicManager)
			lm.Register(dp.Data, alw)
		} else {
			alw.ProcessPacket(dp)
		}
	}
}
Exemple #23
0
func (c *UdpConnection) ConnSenderClient() {
	for {
		select {
		//case bytes := <-c.outgoingBytes:
		//c.conn.Write(bytes)
		case frame := <-c.outFrame:
			gts.Trace("1outframe:orderno:%d, flag:%d, data:%q", frame.OrderNo, frame.Flag, frame.Data)
			frame.ToString()
			bytes := frame.Pack(c.udpStream.Endianer)
			c.conn.Write(bytes)

		case <-c.quitSender:
			gts.Trace("connsenderclient,quit")
			c.conn.Close()

			c.closeCallback(c.id)
			break
		}
	}
}
func (alw *AgentToLogicWormhole) ProcessPacket(dp *RoutePacket) {
	gts.Trace("agenttologicwormhole processpack receive:\n%q", dp)

	if server, ok := alw.GetManager().GetServer().(*Agent); ok {
		if acw, ok := server.ClientWormholes.Get(dp.Guin); ok {
			//if aw, ok := acw.(*AgentToClientWormhole);ok {
			aw := acw.(*AgentToClientWormhole)
			aw.Send(dp.Guin, dp.Method, dp.Data)
		}
	}
}
Exemple #25
0
func (c *UdpConnection) ConnSenderServer() {
	for {
		select {
		//case dp := <-c.outgoing:
		//gts.Trace("clientpool ConnSender:dp.type=%v,dp.data=% X",dp.Type, dp.Data)
		////c.routePack.PackWrite(c.conn.Write,dp)
		case frame := <-c.outFrame:
			gts.Trace("2outframe:orderno:%d, flag:%d, data:%q", frame.OrderNo, frame.Flag, frame.Data)
			frame.ToString()
			bytes := frame.Pack(c.udpStream.Endianer)
			c.conn.WriteToUDP(bytes, c.userAddr)

		case <-c.quitSender:
			gts.Trace("udp connsenderserver,quit")
			c.conn.Close()

			c.closeCallback(c.id)
			break
		}
	}
}
Exemple #26
0
func (wh *Wormhole) ctrlClosed(id int) {
	gts.Trace("ctrlClosed")

	wh.closeded = true

	if wh.dataConnection != nil {
		wh.dataConnection.Close()
	} else {
		wh.dataConnection = nil
	}
	if wh.ctrlConnection != nil {
		wh.ctrlConnection.Close()
	} else {
		wh.ctrlConnection = nil
	}

	wh.sendConnection = nil

	gts.Trace("//////////////////////////////////////////")
	wh.closeCallback(wh.guin)
	gts.Trace("------------------------------------------")
}
//rule = 10,12,0
func (r *Dispatcher) AddHandler(rule []byte, wh IWormhole) {
	rules := gutils.ByteString(rule)

	rules = strings.Replace(rules, " ", "", -1)
	if len(rules) == 0 {
		r.addRule(0, wh)
		return
	}

	groups := strings.Split(rules, ",")
	gts.Trace("add disp", groups)
	for _, p_ := range groups {
		p := strings.Trim(p_, " ")
		if len(p) == 0 {
			continue
		}
		gcode, err := strconv.Atoi(p)
		if err == nil {
			r.addRule(gcode, wh)
		}
	}
	gts.Trace("messagecodemaps1:", r.handlers)
}
Exemple #28
0
func NewWormhole(guin int, wormholes IWormholeManager, routepack IRoutePack) *Wormhole {
	gts.Trace("new wormhole:")
	wh := &Wormhole{
		Inherit:   NewInherit("ProcessPackets"),
		guin:      guin,
		fromId:    0,
		fromType:  EWORMHOLE_TYPE_CLIENT,
		wormholes: wormholes,
		routePack: routepack,
		closeded:  false,
	}

	return wh
}
Exemple #29
0
func (c *UdpConnection) reqCheck() {
	reqCache := c.reqCache.All()
	for no, val := range reqCache {
		if val == nil {
			gts.Trace("reqFrame is nil:%d", no)
			continue
		}

		rframe := val.(*UdpFrame)
		rframe.Count -= 1
		if rframe.Count <= 0 {
			gts.Trace("send req:", no, len(reqCache))
			//gts.Trace("rframe:::%d,%d,%d", rframe.OrderNo, rframe.Flag,rframe.Count)

			//发送 重传请求
			rframe.Count = UDP_REQ_CHECK_COUNT
			c.sendFrame(rframe)

			gts.Trace("c.lastValidOrderNo:%d", c.lastValidOrderNo)
			gts.Trace("c.lastOrderNo:%d", c.lastOrderNo)
			gts.Trace("c.reqFrame.orderno:%d,flag:%d", rframe.OrderNo, rframe.Flag)
		}
	}
}
Exemple #30
0
func (wh *Wormhole) Send(guin int, method int, data []byte) {
	packet := &RoutePacket{
		Type:   EPACKET_TYPE_GENERAL,
		Guin:   guin,
		Method: method,
		Data:   data,
	}
	gts.Trace("fromType:%d, %d", wh.fromType, EWORMHOLE_TYPE_AGENT)
	if wh.fromType == EWORMHOLE_TYPE_AGENT {
		packet.Type = EPACKET_TYPE_DELAY
	}

	bytes := wh.routePack.Pack(packet)
	wh.SendRaw(bytes)
}