コード例 #1
0
ファイル: client.go プロジェクト: sunyuantao/windows
func (c *Client) Response(msg *protocol.DataPackage) error {
	head := msg.Head
	tail := msg.Tail

	if (head.Command == protocol.CLIENT_CMD_USER_LOGIN || head.Command == protocol.CLIENT_CMD_USER_REGISTER) && tail.UID != 0 {
		c.Uid = tail.UID
		c.Stat = CLIENT_STATE_ONLINE
	}

	buf := protocol.GetBuffer()
	defer protocol.PutBuffer(buf)
	binary.Write(buf, binary.BigEndian, head)
	binary.Write(buf, binary.BigEndian, msg.BodyData())

	// write to Conn
	c.Conn.SetDeadline(time.Now().Add(NET_WRITE_DATELINE))
	if _, err := c.Conn.Write(buf.Bytes()); err != nil {
		if e, ok := err.(net.Error); ok && e.Timeout() {
			syslog.Info("write timeouat:", err, *c)
			return nil
		}
		syslog.Warn("write to conn fail :", err.Error(), msg.Tail)
		return errors.As(err)
	} else {
		syslog.Debug2("write to conn success:", head, string(msg.BodyData()), msg.Tail)
	}
	return nil
}
コード例 #2
0
ファイル: qiniu.go プロジェクト: sunyuantao/windows
func (this *FileMgr) run() {
	exitNotify := this.waitGroup.ExitNotify()
	for {
		select {
		case <-exitNotify:
			return
		case <-this.expiredTimer:
			if err := this.RemoveExpiredFile(); err != nil {
				syslog.Warn(err)
			}
		case file := <-this.fileCh:
			if err := addFile(file); err != nil {
				syslog.Warn(err, *file)
			}

		}
	}
}
コード例 #3
0
ファイル: msg_center.go プロジェクト: sunyuantao/windows
func (h *Handler) HandleMessage(message *nsq.Message) error {
	dataPackage := common.GetDataPackage()
	defer common.PutDataPackage(dataPackage)
	if err := dataPackage.Unpackage(message.Body); err != nil {
		syslog.Warn(err)
		return nil
	}

	head := &dataPackage.Head
	jsonStr := dataPackage.BodyData()
	tail := &dataPackage.Tail

	switch head.Command {
	case common.XXX_CMD_SEND_USER_MSG:
	default:
		syslog.Warn("Invalid Cmd :", head.Command, *tail)
	}
	// get user state
	us, err := user.GetUserState(tail.UID)
	if err != nil {
		syslog.Info("GetUserState failed!!!", tail)
		return nil
	}
	// not online
	if us.Online == false {
		syslog.Debug("client not online!!! uid:", tail.UID)
		return nil
	}

	tail.IP = us.ConnIP
	tail.Port = us.ConnPort
	tail.SID = us.Sid
	tail.UID = us.Uid

	respBuf := common.GetBuffer()
	defer common.PutBuffer(respBuf)
	if err := common.Package(respBuf, head, jsonStr, tail); err != nil {
		syslog.Info(err)
		return nil
	}
	topic := fmt.Sprintf("conn_%s_%d", util.IPToStr(tail.IP), tail.Port)
	common.NsqPublish(topic, respBuf.Bytes())
	return nil
}
コード例 #4
0
ファイル: msg_server.go プロジェクト: sunyuantao/windows
func (h *Handler) HandleMessage(message *nsq.Message) error {
	dataPackage := common.GetDataPackage()
	defer common.PutDataPackage(dataPackage)
	if err := dataPackage.Unpackage(message.Body); err != nil {
		syslog.Warn(err)
		return nil
	}
	head := &dataPackage.Head
	jsonStr := dataPackage.BodyData()
	tail := &dataPackage.Tail

	var err error = nil
	resp := []byte("")
	switch head.Command {
	case common.XXX_CMD_SEND_USER_MSG:
		resp, err = h.SendMsg(head, jsonStr, tail)
	case common.XXX_CMD_RECV_USER_MSG:
		//resp, err = h.ReceivedUserMsg(head, jsonStr, &tail)
		common.NsqPublish(common.MSG_STORE_TOPIC, message.Body)
	default:
		syslog.Info("invalid cmd:", head.Command, *tail)
		return nil
	}

	if err != nil {
		syslog.Warn("msg_server msg handle failed:", err, head, tail)
		head.ErrorCode = uint32(errors.Code(err))
		resp = []byte("")
	}
	head.PkgLen = uint16(common.DATA_PACKAGE_HEAD_SIZE + len(resp))
	respBuf := common.GetBuffer()
	defer common.PutBuffer(respBuf)
	if err := common.Package(respBuf, head, resp, tail); err != nil {
		syslog.Info(err)
		return nil
	}
	topic := fmt.Sprintf("conn_%s_%d", util.IPToStr(tail.IP), tail.Port)
	common.NsqPublish(topic, respBuf.Bytes())
	return nil
}
コード例 #5
0
ファイル: client.go プロジェクト: sunyuantao/windows
func (c *Client) Hello() {
	//dataPackage := pool.Get(DATA_PACKAGE).(*protocol.DataPackage)
	dataPackage := protocol.GetDataPackage()
	dataPackage.Head.PkgLen = uint16(protocol.DATA_PACKAGE_HEAD_SIZE)
	dataPackage.Head.Command = protocol.CLIENT_CMD_HEARTBEAT
	buf := protocol.GetBuffer()
	binary.Write(buf, binary.BigEndian, dataPackage.Head)
	c.Conn.SetDeadline(time.Now().Add(NET_WRITE_DATELINE))
	if _, err := c.Conn.Write(buf.Bytes()); err != nil {
		syslog.Warn(err)
	}
	protocol.PutBuffer(buf)
	protocol.PutDataPackage(dataPackage)
}
コード例 #6
0
ファイル: client.go プロジェクト: sunyuantao/windows
func (this *ClientGroup) Response() {
	var dp *protocol.DataPackage = nil
	var client *Client = nil
	for this.Exit == false {
		select {
		case dp = <-this.RespCh:
			client = dp.Data.(*Client)
			if err := client.Response(dp); err != nil {
				syslog.Warn(err)
			}
			atomic.AddInt32(&client.MsgCount, -1)
			protocol.PutDataPackage(dp)
		}
	}
}
コード例 #7
0
ファイル: gateway.go プロジェクト: sunyuantao/windows
func (this *Service) Serve() {
	this.WaitGroup.AddOne()
	defer this.WaitGroup.Done()
	exitNotify := this.WaitGroup.ExitNotify()
	var grpCount int32 = 0
	var clientCount int32 = 0
	var sid uint32 = 10000
	for {
		select {
		case <-exitNotify:
			return
		default:
		}

		this.Listener.SetDeadline(time.Now().Add(NET_READ_DATELINE))
		conn, err := this.Listener.AcceptTCP()
		if err != nil {
			if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
				continue
			}
			syslog.Warn("Unknow Net Error:", err)
			break
		}
		syslog.Debug(conn.RemoteAddr(), "connected, seesionId:", sid)
		// 建立客户端
		client := GetClient()
		client.Service = this
		client.Conn = conn
		client.Sid = sid
		client.Stat = CLIENT_STATE_INIT
		client.RespCh = nil
		client.MsgCount = 0
		client.LastTime = time.Now().Unix()
		this.RWMutex.Lock()
		this.ClientMap[sid] = client
		this.RWMutex.Unlock()
		this.ClientCh <- client
		clientCount = atomic.AddInt32(&this.ClientCount, 1)

		grpCount = atomic.LoadInt32(&this.ClientGroupCount)
		if clientCount >= ((grpCount - 10) * CLIENT_NUM_PER_GROUP) {
			NewClientGroup(this)
		}
		// 处理客户端请求
		sid++ // increase sid
	}
}
コード例 #8
0
ファイル: user_center.go プロジェクト: sunyuantao/windows
func (h *DBHandler) HandleMessage(message *nsq.Message) error {
	var resp []byte
	var err error
	defer func() {
		if err != nil {
			syslog.Warn(err)
		}
	}()
	dataPackage := protocol.GetDataPackage()
	defer protocol.PutDataPackage(dataPackage)

	if err := dataPackage.Unpackage(message.Body); err != nil {
		return nil
	}
	head := &dataPackage.Head
	jsonStr := dataPackage.BodyData()
	tail := &dataPackage.Tail

	syslog.Debug(*head, string(jsonStr), *tail)

	switch head.Command {
	case protocol.CLIENT_CMD_USER_REGISTER:
		resp = h.Register(head, jsonStr, tail)
	case protocol.CLIENT_CMD_USER_LOGIN:
		resp = h.Login(head, jsonStr, tail)
	default:
		syslog.Debug("invalid cmd:", head.Command, *tail)
		return nil
	}

	respBuf := protocol.GetBuffer()
	defer protocol.PutBuffer(respBuf)
	head.PkgLen = uint16(protocol.DATA_PACKAGE_HEAD_SIZE + len(resp))
	binary.Write(respBuf, binary.BigEndian, head)
	binary.Write(respBuf, binary.BigEndian, resp)
	binary.Write(respBuf, binary.BigEndian, tail)
	topic := fmt.Sprintf("conn_%s_%d", util.IPToStr(tail.IP), tail.Port)
	common.NsqPublish(topic, respBuf.Bytes())
	syslog.Debug2("db_server --> conn_server publish:", topic, head, string(resp), tail)
	return nil
}
コード例 #9
0
ファイル: gateway.go プロジェクト: sunyuantao/windows
func (this *Service) HandleMessage(message *nsq.Message) error {
	dp := protocol.GetDataPackage()
	if err := dp.Unpackage(message.Body); err != nil {
		syslog.Warn(err, string(message.Body))
		protocol.PutDataPackage(dp)
		return nil
	}
	this.RWMutex.RLock()
	client, ok := this.ClientMap[dp.Tail.SID]
	this.RWMutex.RUnlock()

	if !ok || client.Stat == CLIENT_STATE_OFFLINE || dp.Tail.SID != client.Sid {
		protocol.PutDataPackage(dp)
		return nil
	}
	if client.RespCh == nil {
		protocol.PutDataPackage(dp)
		return nil
	}
	dp.Data = client
	client.RespCh <- dp
	atomic.AddInt32(&client.MsgCount, 1)
	return nil
}