示例#1
0
func readServer(pTCPConn *net.TCPConn) {
	buffer := make([]byte, 1024)
	tempBuf := make([]byte, 4)
	for {
		bufferSize, err := pTCPConn.Read(buffer)
		if err != nil {
			return
		}
		if bufferSize >= 4 {
			copy(tempBuf, buffer)
			length := common.BytesToInt(tempBuf)

			//fmt.Println("length:", length)
			newbuffer := make([]byte, length)
			copy(newbuffer, buffer[4:])

			msg := &protocol.WMessage{}
			err = proto.Unmarshal(newbuffer, msg)
			if err != nil {
				fmt.Println("unmarshal:", err)
				return
			}
			fmt.Println("解码:", msg.String())
		}

	}
}
示例#2
0
文件: main.go 项目: happyEgg/aTalk
func connection(conn net.Conn) {
	defer conn.Close()
	headBuffer := make([]byte, 4)

	//处理文件传输
	bufSize, err := conn.Read(headBuffer)
	if err != nil {
		common.Logger.Error("conn.Read:", err)
		break
	}

	if bufSize < 4 {
		break
	}
	// messager := make(chan byte)
	// //心跳计时
	// go HeadBeating(conn, messager, 2)
	// //检测client是否有数据传来
	// go GravelChannel(headBuffer, messager)

	bodyLen := common.BytesToInt(headBuffer)
	bodyBuffer := make([]byte, bodyLen)
	bodySize, err := conn.Read(bodyBuffer)
	if err != nil {
		break
	}
	if bodySize < bodyLen {
		break
	}

	msg := &protocol.WMessage{}
	err = proto.Unmarshal(bodyBuffer, msg)
	if err != nil {
		common.Logger.Warn("proto:", err)
		break
	}

	switch msg.GetMsgType() {
	case "sendfile":
		handleFile(conn, msg)
	}
}
示例#3
0
文件: listen.go 项目: happyEgg/aTalk
//处理连接的用户操作
func handleConnection(conn net.Conn) {

	defer conn.Close()
	headBuffer := make([]byte, 4)

	//循环处理接收数据
	for {
		bufSize, err := conn.Read(headBuffer)
		if err != nil {
			//Logger.Error("conn.read:", err)
			break
		}
		if bufSize < 4 {
			continue
		}

		messager := make(chan byte)
		//心跳计时
		go common.HeartBeating(conn, messager, 20)
		//检测是client否有数据传来
		go common.GravelChannel(headBuffer, messager)

		bodyLen := common.BytesToInt(headBuffer)
		if bodyLen == 0 {
			common.KeepAliveResult(conn)
			continue
		}
		bodyBuffer := make([]byte, bodyLen)
		bodySize, err := conn.Read(bodyBuffer)
		if err != nil {
			break
		}

		//如果接收的包大小小于包头内容的长度,舍弃
		if bodySize < bodyLen {
			continue
		}

		msg := &protocol.WMessage{}
		err = proto.Unmarshal(bodyBuffer, msg)
		fmt.Println("msg:", msg.String())
		if err != nil {
			Logger.Warn("protobuf解包:", err)
			continue
		}

		//根据包的类型来进行处理
		switch msg.GetMsgType() {
		case "register":
			RegisterController(conn, msg)
			goto Break
		//只允许一方登陆
		case "login":
			LoginController(conn, msg)

		case "logout":

			fmt.Println(msg.UserInfo.GetUsername(), " logout")
			goto Break
		case "modifyInfo":
			if OnlineCheck(msg.UserInfo.GetUsername()) {
				go ModifyInfoController(conn, msg)
			}

		case "sendFriendRequest":
			if OnlineCheck(msg.AddFriend.GetSender()) {
				go SendFirendRequestController(conn, msg)
			}
		case "sendFriendResponse":
			if OnlineCheck(msg.AddFriend.GetSender()) {
				go SendFirendResponseController(conn, msg)
			}
		case "allFriends":
			if OnlineCheck(msg.UserInfo.GetUsername()) {
				go GetAllFriendController(conn, msg)
			}

		case "searchUser":
			if OnlineCheck(msg.UserInfo.GetUsername()) {
				go SearchUserController(conn, msg)
			}
		case "sendMsg":
			if OnlineCheck(msg.UserInfo.GetUsername()) {
				go SendMsgController(conn, msg)
			}
		case "modifyRemark":
			if OnlineCheck(msg.UserInfo.GetUsername()) {
				go ModifyRemarkController(conn, msg)
			}
		case "msgRecord":
			if OnlineCheck(msg.UserInfo.GetUsername()) {
				go SingleMsgRecordController(conn, msg)
			}
		case "groupCreate":
			if OnlineCheck(msg.UserInfo.GetUsername()) {
				go GroupCreateController(conn, msg)
			}
		case "groupInvite":
			if OnlineCheck(msg.UserInfo.GetUsername()) {
				go GroupInviteController(conn, msg)
			}
		case "groupExit":
			if OnlineCheck(msg.UserInfo.GetUsername()) {
				go GroupExitController(conn, msg)
			}
		case "groupKick":
			if OnlineCheck(msg.UserInfo.GetUsername()) {
				go GroupKickController(conn, msg)
			}
		case "groupMsgRecord":
			//在msgRecord_controller这个文件中
			if OnlineCheck(msg.UserInfo.GetUsername()) {
				go GroupMsgRecordController(conn, msg)
			}
		case "groupMsg":
			if OnlineCheck(msg.UserInfo.GetUsername()) {
				go GroupMsgController(conn, msg)
			}
		case "groupModify":
			if OnlineCheck(msg.UserInfo.GetUsername()) {
				go GroupModifyController(conn, msg)
			}
		case "groupRemark":
			if OnlineCheck(msg.UserInfo.GetUsername()) {
				go GroupRemarkController(conn, msg)
			}
		case "sendfile":
			if OnlineCheck(msg.UserInfo.GetUsername()) {
				go SendFile(conn, msg)
			}
		case "recvfile":
			if OnlineCheck(msg.UserInfo.GetUsername()) {
				go RecvFile(conn, msg)
			}
		case "friendInfo":
			go GetFriendInfoController(conn, msg)
		case "groupInfo":
			go GetGroupInfoController(conn, msg)

		case "delFriend":
			if OnlineCheck(msg.UserInfo.GetUsername()) {
				go DelFriendController(conn, msg)
			}
		}
	}

Break:
	if ConnMap[conn] != "" {
		collectionDevice := common.DBMongo.C("device")
		collectionUser := common.DBMongo.C("user")
		nowTime := time.Now()
		// _, err := o.QueryTable("login_message").Filter("id", LoginTimeId).Update(orm.Params{"logout_time": nowTime})
		// _, err = o.QueryTable("user").Filter("user_name", ConnMap[conn]).Update(orm.Params{"logout_time": nowTime})
		err := collectionDevice.Update(bson.M{"_id": LoginTimeId}, bson.M{"$set": bson.M{"logout_time": nowTime}})
		err = collectionUser.Update(bson.M{"user_name": ConnMap[conn]}, bson.M{"$set": bson.M{"logout_time": nowTime}})
		if err != nil {
			Logger.Error("logout time update failed: ", err)
			fmt.Println("logout time update failed: ", err)
		}
		delete(UserMap, ConnMap[conn])
		delete(ConnMap, conn)
	}
	fmt.Println("退出: ", conn.RemoteAddr().String())
}