int main() { int fd; struct userInfo myAddrBook; fd = open("Info.txt",O_CREAT | O_WRONLY,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); if(fd<0) { perror("file open error"); } makeUserInfo(&myAddrBook,"semtax",23,0,"Programming"); write(fd,(void *)&myAddrBook,sizeof(myAddrBook)); makeUserInfo(&myAddrBook,"hello",22,1,"Game"); write(fd,(void *)&myAddrBook,sizeof(myAddrBook)); makeUserInfo(&myAddrBook,"Killer",33,0,"Hunting Deer"); write(fd,(void *)&myAddrBook,sizeof(myAddrBook)); close(fd); return 0; }
// data :descrip + fromer + account + pw void MyServer::userLogin(QByteArray& data) { // qDebug()<<"mainThread id:"<< QThread::currentThreadId(); DataFormat_login loginInfo = DataFormat::getFrom(data); userInfo *info = model.checkUser(loginInfo.account,loginInfo.pw) ; for (int i=0;i<clients.count();i++) { if (clients.at(i)->getDescription() == loginInfo.descrip) { if (info==NULL) { qDebug()<<loginInfo.account<<" :登陆失败"; QByteArray *errorData = sql::getErrorData("账号或密码不正确"); clients[i]->getSocket()->write(*errorData); delete errorData; //clients.removeAt(i); } else{ qDebug()<<loginInfo.account<<":登陆成功"; // 将QQ号写入clients[i]中,以作以后转发聊天信息标识 makeUserInfo(info,userStateType::online); clients[i]->setAccount(loginInfo.account); clients[i]->getSocket()->write(bufBlock); bufBlock.resize(0); clients[i]->setUserState(userStateType::online); hx[ loginInfo.account ] = i; // 建立QQ号与clients链表节点之间的哈希映射 //QVector<groupInfo> groups = model.friendGroup(loginInfo.account); //sendGroupSize(groups,clients[i]); //sendFriendGroup(groups,clients[i]); } break; } } delete info; }
void MyServer::userRegister(QByteArray &data) { DataFormat_register registerInfo = DataFormat::getRegisterInfo(data); // 返回新注册用户在QQUser表中的id,加上N后就是QQ号 qint64 id = model.addUser(registerInfo.nick,registerInfo.pw); for (int i=0;i<clients.count();i++) { if (clients.at(i)->getDescription() == registerInfo.descrip) { if (id==-1) { qDebug()<<id<<": 注册失败 "; QByteArray *errorData = sql::getErrorData("注册失败"); clients[i]->getSocket()->write(*errorData); delete errorData; //clients.removeAt(i); } else{ qDebug()<<id<<": 注册成功"; // 将QQ号写入clients[i]中,以作以后转发聊天信息标识 makeUserInfo(id); clients[i]->setAccount(QString::number(id+N)); clients[i]->getSocket()->write(bufBlock); bufBlock.resize(0); clients[i]->setUserState(userStateType::online); hx[ clients[i]->getAccount() ] = i; // 建立QQ号与clients链表节点之间的哈希映射 } break; } } }
void MyServer::userAddFriend(QByteArray &data) { DataFormat_login queryInfo = DataFormat::getAddFriend(data); qint32 friendID = queryInfo.descrip; QString groupName = queryInfo.pw; QTcpSocket *socket = clients.at( hx[ queryInfo.account ] )->getSocket(); int userID = model.getFriendID(queryInfo.account.toLongLong()); // 是否已经是好友 if ( model.isFriend(userID,friendID) ) { QByteArray *errorData = model.getErrorData("你们已经是好友",respondType::addFriendError); socket->write(*errorData); delete errorData; return ; } // 加为好友,并修改数据库 model.addFriend(userID,groupName,friendID); // 将好友信息发给用户 userInfo *info = model.getFriendsInfo(friendID); makeUserInfo(info,getUserState(info->account), respondType::addFriend); socket->write(bufBlock); delete info; }