#include "stdafx.h"
#include "proto.h"
#include <stdlib.h>
#include <pthread.h>
#include <stddef.h>
#include <string.h>
#include <iostream>
#include <vector>
//#include <string>
using namespace std;
//static struct _id_sock_info* psockAddress=0;
static vector<struct _id_sock_info> psockAddress;
void insertinto(struct sockaddr* sockinfo,char*id)
{
time_t timesec;
struct _id_sock_info node;
memset(&node,0,sizeof(struct _id_sock_info));
strcpy(node.id,id);
memcpy(&node.sock_addr,sockinfo,sizeof(struct sockaddr));
time_t times;
time(×);
node.timesec=times;
psockAddress.push_back(node);
}
void deletefrom(struct _id_sock_info* delnodeptr)
{
}
//服务端初始化函数
int data_reset()
{
//清除日志
//SetUnhandledExceptionFilter(MyUnhandledExceptionFilter);
printf("data_reset\n");
initlock();
printf("data_reset2\n");
resetlog();
printf("data_reset3\n");
struct _cfg info;
read_cfg(&info);
//创建监听线程
printf("data_reset4\n");
pthread_t thread,thread2;
pthread_create(&thread,NULL,udplistenthread,NULL);
pthread_create(&thread2,NULL,beatthread,NULL);
return 0;
}
//把网页客户端发送来的心跳请求加入到通信录里,以id为关键字,如果id一样就合并取最后一个
int addressput(struct sockaddr* sockaddr,char* id)
{
flog("addressput");
int i;
int finded =0;
time_t times;
time(×);
int itemsize = 0;
lock();
//如果当前表项已经使用而且id正好和进来的心跳相同
int ii;
for(ii=0; ii < psockAddress.size(); ii++)
{
if(strcasecmp(psockAddress[ii].id,id)==0)
{
//把套接字保存进来
memcpy(&psockAddress[ii].sock_addr,sockaddr,sizeof(struct sockaddr));
//把时间保存下来
psockAddress[ii].timesec = times;
finded = 1;
break;
}
}
//如果没有找到合适的id,并且有空余的表项
if(!finded){
insertinto(sockaddr,id);
}
unlock();
return 0;
}
int timeoutaddress(int timeoutclient)
{
time_t times;
time(×);
lock();
int ii;
vector<struct _id_sock_info>::iterator it;
for(it=psockAddress.begin(); it!=psockAddress.end(); )
{
if((times-it->timesec) >timeoutclient)
{
//把套接字保存进来
//memcpy(&psockAddress[ii].sock_addr,sockaddr,sizeof(struct sockaddr));
//把时间保存下来
//psockAddress[ii].timesec = times;
psockAddress.erase(it);
//finded = 1;
//break;
} else{
it++;
}
}
unlock();
return 0;
}
int broadcastpacket(char* pmsg,int msglen,int s)
{
int i;
lock();
for(i=0; i < psockAddress.size(); i++){
sendto(s,pmsg,msglen,0,(struct sockaddr*)&psockAddress[i].sock_addr,sizeof(struct sockaddr));
printf("broadcast a packet\n");
}
unlock();
return 0;
}
int makebroadcastpacket(char* pbuff,int cmdid,const char* id,const char* longc,const char* latc,const char* timec)
{
memset(pbuff,0,132);
pbuff[0]=cmdid;
pbuff[1]='1';
pbuff[2]='2';
pbuff[3]='8';
strcpy(pbuff+4,id);
char* plong=pbuff+4+32;
strcpy(plong,longc);
plong += 32;
strcpy(plong,latc);
plong += 32;
strcpy(plong,timec);
return 128+4;
}
#include "proto.h"
#include <stdlib.h>
#include <pthread.h>
#include <stddef.h>
#include <string.h>
#include <iostream>
#include <vector>
//#include <string>
using namespace std;
//static struct _id_sock_info* psockAddress=0;
static vector<struct _id_sock_info> psockAddress;
void insertinto(struct sockaddr* sockinfo,char*id)
{
time_t timesec;
struct _id_sock_info node;
memset(&node,0,sizeof(struct _id_sock_info));
strcpy(node.id,id);
memcpy(&node.sock_addr,sockinfo,sizeof(struct sockaddr));
time_t times;
time(×);
node.timesec=times;
psockAddress.push_back(node);
}
void deletefrom(struct _id_sock_info* delnodeptr)
{
}
//服务端初始化函数
int data_reset()
{
//清除日志
//SetUnhandledExceptionFilter(MyUnhandledExceptionFilter);
printf("data_reset\n");
initlock();
printf("data_reset2\n");
resetlog();
printf("data_reset3\n");
struct _cfg info;
read_cfg(&info);
//创建监听线程
printf("data_reset4\n");
pthread_t thread,thread2;
pthread_create(&thread,NULL,udplistenthread,NULL);
pthread_create(&thread2,NULL,beatthread,NULL);
return 0;
}
//把网页客户端发送来的心跳请求加入到通信录里,以id为关键字,如果id一样就合并取最后一个
int addressput(struct sockaddr* sockaddr,char* id)
{
flog("addressput");
int i;
int finded =0;
time_t times;
time(×);
int itemsize = 0;
lock();
//如果当前表项已经使用而且id正好和进来的心跳相同
int ii;
for(ii=0; ii < psockAddress.size(); ii++)
{
if(strcasecmp(psockAddress[ii].id,id)==0)
{
//把套接字保存进来
memcpy(&psockAddress[ii].sock_addr,sockaddr,sizeof(struct sockaddr));
//把时间保存下来
psockAddress[ii].timesec = times;
finded = 1;
break;
}
}
//如果没有找到合适的id,并且有空余的表项
if(!finded){
insertinto(sockaddr,id);
}
unlock();
return 0;
}
int timeoutaddress(int timeoutclient)
{
time_t times;
time(×);
lock();
int ii;
vector<struct _id_sock_info>::iterator it;
for(it=psockAddress.begin(); it!=psockAddress.end(); )
{
if((times-it->timesec) >timeoutclient)
{
//把套接字保存进来
//memcpy(&psockAddress[ii].sock_addr,sockaddr,sizeof(struct sockaddr));
//把时间保存下来
//psockAddress[ii].timesec = times;
psockAddress.erase(it);
//finded = 1;
//break;
} else{
it++;
}
}
unlock();
return 0;
}
int broadcastpacket(char* pmsg,int msglen,int s)
{
int i;
lock();
for(i=0; i < psockAddress.size(); i++){
sendto(s,pmsg,msglen,0,(struct sockaddr*)&psockAddress[i].sock_addr,sizeof(struct sockaddr));
printf("broadcast a packet\n");
}
unlock();
return 0;
}
int makebroadcastpacket(char* pbuff,int cmdid,const char* id,const char* longc,const char* latc,const char* timec)
{
memset(pbuff,0,132);
pbuff[0]=cmdid;
pbuff[1]='1';
pbuff[2]='2';
pbuff[3]='8';
strcpy(pbuff+4,id);
char* plong=pbuff+4+32;
strcpy(plong,longc);
plong += 32;
strcpy(plong,latc);
plong += 32;
strcpy(plong,timec);
return 128+4;
}