#include "net_base.h"
#define MAX_num 500
#define FILEPATH "usrlist.dat"
static usron clients[MAX_num];
static userdata counts[MAX_num];
void EventSet(Usron ev, int fd, int pos,int states,char addrbuf[]){
ev->fd = fd;
ev->state= states;
ev->pos=pos;
ev->last_active = time(NULL);
strcpy(ev->addr,addrbuf);
}
int netinit()
{
FILE *fp;
char id[128];
char pwd[128];
int i,flag=0;
char buf[512];
fp = fopen (FILEPATH , "r");
if (fp == NULL)
{
perror ("open db error");
return -1;
}
fseek (fp, 0, SEEK_SET);
while ((fgets (buf, sizeof (buf), fp)) != NULL){
if(flag==0){
flag=1;
strcpy(id,buf);
}
else if(flag==1){
flag=0;
strcpy(pwd,buf);
strcpy(counts[i].Id,id);
strcpy(counts[i].pwd,pwd);
counts[i].link=NULL;
i++;
}
strcpy(counts[i].Id,"none");
}
i=0;
while(strcmp(counts[i].Id,"none")!=0){
printf("%s,%s,%d\n",counts[i].Id,counts[i].pwd,i);
i++;
}
for(i = 0; i < MAX_num; i++)
{
clients[i].state = 0;
clients[i].usrinfo=NULL;
}
fclose(fp);
}
void deal_addre(const char *add,int port,struct sockaddr_in * mysockadd)
{
struct sockaddr_in addr;
unsigned address;
if(strcmp(add,"any")==0)
{address=INADDR_ANY;}
else{address= inet_addr(add);}
bzero(&addr,sizeof(struct sockaddr_in));
addr.sin_family=PF_INET;
addr.sin_addr.s_addr=address;
addr.sin_port=htons(port);
* mysockadd=addr;
}
userdata *serchusrbyname(userdata usrlist[],char * usrid){
int i=0;
userdata usr;
strcpy(usr.Id,"none");
for(i;i<MAX_num;i++){
if((memcmp(usrlist[i].Id,usrid,strlen(usrid)))==0)
{
return &usrlist[i];
}
}
return &usr;;
}
int CheckusrInfo(void* arg,userdata usrlist[]){
Usron usr=(Usron)arg;
char ** result;
char usrname[18];
char pwd[18];
result=dealwithdata(usr->buff);
printf("%s result[0]\n",result[0]);
strcpy(usrname,result[0]);
strcpy(pwd,result[1]);
userdata *usrlogin=serchusrbyname( usrlist, usrname);
if(memcmp(usrlogin->pwd,pwd,strlen(pwd))||(strcmp(usrlogin->Id,"none")==0)){return -1;}
else {
strcpy(usrlogin->Id,usrname);
usrlogin->link=usr;
usr->usrinfo=usrlogin;
usr->last_active=time(NULL);
return 1;
}
}
int DealwithMESSAGE(void* arg){
Usron usr=(Usron)arg;
printf("and here the addr is %s",usr->addr);
char* delim = "|";
char* t;
char * buf;
char dealbuf[512];
buf=dealbuf;
strcpy(dealbuf,usr->buff);
t=strsep(&buf,delim);
switch(t[0]){
case LOGIN:{
int ret;
if((ret=CheckusrInfo((void*)usr,counts) )==-1)
{
usr->buff[0]=LOGIN_FAILL;
}
else{
usr->buff[0]=LOGIN_SUCCEESS;}
senddatas((void*)usr);
break;
}
case REGISTER:{
int ret;
if((ret=Registerfunc(usr,counts))==-1)
{ usr->buff[0]=REGISTER_ERROR;}
else{ usr->buff[0]=REGISTER_SUCCESS;}
senddatas((void *)usr);
writeusrlist();
break;
}
case SHOWLIST:{
char * listsend=calloc(510,sizeof(char));
strcatfun(counts,listsend);
if(memcmp(listsend,"none",5)==0)
{ usr->buff[0]=LISTSEND_ERROR;
printf("didn't send list!\n");
}
else{
printf(" send list!\n");usr->buff[0]=LISTSEND;}
printf("the list is %s",listsend);
strcat(usr->buff,"|");
strcat(usr->buff,listsend);
printf("the list is %s",listsend);
senddatas((void *) usr);
break;
}
}
}
void AcceptConn(void * arg ){
pollitem *item=(pollitem *)arg;
struct sockaddr_in sin;
socklen_t len = sizeof(struct sockaddr_in);
int nfd, i;
while((nfd = accept(item->fd, (struct sockaddr*)&sin, &len))>0){
do{
for(i = 0; i < MAX_num; i++)
{
if(clients[i].state == 0)
{
break;
}
}
if(i ==MAX_num) {
printf("%s:max connection limit[%d].", __func__, MAX_num);
break; }
// set nonblocking
setnonblocking( nfd );
printf("before nfd is %d",nfd);
EventSet(&clients[i],nfd,i,1,inet_ntoa(sin.sin_addr));
fdhd(item->pp,nfd,recievedatas,(EPOLLIN| EPOLLET) , EPOLL_CTL_ADD,(void*)(&clients[i]));
//clients[i].ptr=newitem;
// clients[i].ptr=
}while(0);
printf("the addr is %s, addr = %p and the pos is%d\n",clients[i].addr, clients[i].addr, clients[i].pos);
printf("fpr example the addr is %s\n ",clients[0].addr);
printf("new conn[%s:%d] \n", inet_ntoa(sin.sin_addr),ntohs(sin.sin_port));
}
if(nfd == -1)
{
if(errno != EAGAIN && errno != EINTR) {
}
printf("%s: accept, %d", __func__, errno);
return;
}
}
void recievedatas(void * arg){
Usron usr=(Usron)arg;
printf("when recieve the nfd is %d\n",usr->fd);
int len,n = 0; // receive data
while( ( len = read(usr->fd, usr->buff+n, (sizeof(usr->buff)-n)))>0){
n+=len;
}
//len = recv((*fd), buf, sizeof(buf), 0);
printf("addr = %p.\n", usr->addr);
printf("here the addr is %s\n", usr->addr );
printf("here the addr is %s\n", (char *) (usr->addr + 1));
//printf("addr %c %c %c\n", usr->addr[0], usr->addr[1], usr->addr[2] );
if(n> 0) {
usr->buff[n] = '\0';
printf("C[%d]:%s\n",(usr-> fd),usr->buff );
DealwithMESSAGE(usr);
// change to send event
}
else if(len == -1 && errno != EAGAIN){
printf("wan le!\n");
usr->state=0;
usr->usrinfo=NULL;
close(usr->fd);
}
}
void senddatas(void *arg){
Usron usr=(Usron) arg;
int len,n=0;
if((len=write(usr->fd,usr->buff,sizeof(usr->buff)))>0){
}
else if(len == -1 && errno != EAGAIN){
usr->state=0;
usr->usrinfo=NULL;
close(usr->fd);
}
}
char **dealwithdata(char buf[])
{
char **result=(char **)calloc(3,sizeof(char *));
char* delim = "|";
int i=0;
char* t;
char *buff;
buff=buf;
strsep(&buff,delim);
while((t=strsep(&buff,delim))!=NULL){
result[i]=t;
i++;
}
printf("%s\n",result[0]);
return result;
}
int Registerfunc(Usron usr,userdata usrlist[ ]){
char **result;
printf("%s",usr->buff);
printf("start to register!\n");
result=dealwithdata(usr->buff);
userdata usrnew;
strcpy(usrnew.Id,result[0]);
strcpy(usrnew.pwd,result[1]);
usrnew.link=NULL;
int i=0;
while((strcmp(counts[i].Id,"none"))!=0)
{
if(i>=MAX_num){
printf("no more place for register!");
return -1;
}
i++;
}
counts[i]=usrnew;
strcpy(counts[i+1].Id,"none");
return 1;
}
void strcatfun(userdata usrlist[],char *dest){
char temp[40];
int i=0,value=0;
for(i=0;i<MAX_num;i++){
if((strcmp(usrlist[i].Id,"none")!=0)&&(usrlist[i].link!=NULL)){
stringbug(usrlist[i].link->addr);
printf("the %d Id is %s and the addr is %s",i,usrlist[i].Id,usrlist[i].link->addr);
strcpy(temp,usrlist[i].Id);
// printf("%s\n",buff[i]);
strcat(dest,temp);
strcat(dest,"|");
strcpy(temp,usrlist[i].link->addr);
strcat(dest,temp);
strcat(dest,"|");
value=1;
}
}
if(value==0){
strcpy(dest,"none");
}
}
int writeusrlist(){
FILE *fp;
int i=0;
fp = fopen (FILEPATH , "w");
if (fp == NULL)