Exemplo n.º 1
0
func (this *Client) handleClient() {

	this.PrefixText = "UP: " + this.conn.RemoteAddr().String() + " - "
	log.W(this.PrefixText, " Connect OK.") //remote ip
	// set 2 minutes timeout
	this.conn.SetReadDeadline(time.Now().Add(30 * time.Minute))

	defer this.conn.Close()                             // close connection before exit
	defer GlobalSessions.SessionDestroy(this.SessionId) // close session before exit

	status, err := this.processData()
	if err != nil {
		log.W(this.PrefixText, "status:"+strconv.Itoa(status)+", error:"+err.Error())
	}

}
Exemplo n.º 2
0
func init() {
	log.W("client", "global init")
	t := time.Now()
	SessionProvider = "memory"
	SessionName = "YT"
	SessionGCMaxLifetime = 60
	SessionSavePath = ""
	Year = strconv.Itoa(t.Year())
	Month = t.Month().String()
	Day = strconv.Itoa(t.Day())
	AudioDirPath = Year + `/` + Month + `/` + Day
	GlobalSessions, _ = session.NewManager(SessionProvider, SessionGCMaxLifetime, SessionSavePath)
	go GlobalSessions.GC()
	os.MkdirAll(AudioDirPath, 0777)
}
Exemplo n.º 3
0
// processData thead
func (this *Client) processData() (int, error) {
	var status int = 0
	var frame UpPack
	//open file
	fout, err := os.OpenFile(this.AudioDirPath+`/`+this.SessionId+".pcm", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
	if err != nil {
		log.W(this.PrefixText, err.Error())
	}
	defer fout.Close()

	for {
		switch status {
		case 0:

			data, err := this.getNextByte()

			if err != nil {
				return 0, err
			}
			if data == 'Y' {
				status = 1
			} else {
				status = 0
			}
			break

		case 1:

			data, err := this.getNextByte()

			if err != nil {
				return 1, err
			}

			if data == 'Y' {
				status = 1
				break
			} else if data == 'T' {
				status = 2
				break
			} else {
				status = 0
				break
			}
			break

		case 2:

			data, err := this.getNextData(2)
			if err != nil {
				return 2, err
			}

			frame.PackLength = util.BytesToShort(data)

			if frame.PackLength < 0 {
				return 2, errors.New("frame : PackLengthError")
			} else {
				status = 3
			}
			break

		case 3:

			data, err := this.getNextData((frame.PackLength))
			if err != nil {
				return 3, err
			}

			var buf []byte = make([]byte, 4)
			buf[0] = data[0]
			buf[1] = data[1]
			frame.HeadLength = util.BytesToInt(buf)
			frame.Version = data[2]
			frame.Seq[0] = data[3]
			frame.Seq[1] = data[4]
			frame.Type = data[5]
			frame.Status = data[6]
			frame.Encrypt = data[7]

			frame.CheckSum[0] = data[frame.PackLength-4]
			frame.CheckSum[1] = data[frame.PackLength-3]
			frame.Appendfix[0] = data[frame.PackLength-2]
			frame.Appendfix[1] = data[frame.PackLength-1]

			if frame.Appendfix[0] == 'C' && frame.Appendfix[1] == 'N' {
				// 数据存放到Frame中

				frame.Data = data[8 : frame.PackLength-4]
				//frame.Data.Append("")
				status = 4

			} else {
				log.W(this.PrefixText, "Frame: error, skip.")
				status = 0
			}

			break

		case 4:

			// 上面的桢有效 , 做操作
			log.W(this.PrefixText, "Frame: data len"+strconv.Itoa(frame.PackLength))

			// 写入文件
			fout.Write(util.DecoderFix(frame.Data))
			//fout.Write(frame.Data)
			// 解压缩, 上传google
			// 向客户端写入

			status = 0
			break
		}

	}

	return 0, nil
}