📄 session.cpp
字号:
#include "session.h"bool Session::addUser( int id,char *name,int Sockfd)
{
User *p=NULL;
if((p = new User)!=NULL)
{
sprintf(p->UserId,"%d",id);
p->Sockfd=Sockfd;
strcpy(p->UserName,name);
p->next=head->next;
head->next=p;
Count++;
return 1;
}
else return 0;
}
bool Session::deleteUser(int id)
{
User *p=head->next,*q=head;
while(p)
{
if(id==atoi(p->UserId))
{
q->next=p->next;
delete p;
Count--;
return 1;
}
q=p;
p=p->next;
}
return 0;
}
bool Session::deleteid(int socketid)
{
User * p = head->next;
User * q = head;
while(p)
{
if(socketid == p->Sockfd)
{
q->next = p->next;
delete p;
p = NULL;
Count--;
return 1;
}
q = p;
p = p->next;
}
return 0;
}
void Session::getSockfd(int *sockfd)
{ int i=0;
User *p=head->next;
while(p)
{
sockfd[i]=p->Sockfd;
i++;
p=p->next;
}
}
char* Session::getUser()
{
char *string=new char[31*Count];
int i=0;
User *p=head->next;
while(p)
{
strcpy(string+i*31,p->UserName);
strcpy(string+i*31+15,p->UserId);
i++;
p=p->next;
}
return string;
}
Session * SessionList::findSession(int id)
{
SessionList *p=this->next;
while(p)
{
if(p->Slist->SessionId==id)
return p->Slist;
p=p->next;
}
return NULL;
}
bool SessionList::addSession(Session *session)
{
SessionList *p;
if(p=new SessionList)
{
p->Slist=session;
p->next=this->next;
this->next=p;
return 1;
}
else return 0;
}
bool SessionList::deleteSession(int id)
{
SessionList *p=this,*q=next;
while(q)
{
if(q->Slist->SessionId==id)
{
p->next=q->next;
q->next=NULL;
delete q;
return 1;
}
else {
p=q;
q=q->next;
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -