//管道消息结构 struct pipemsg { int op; int fd;
unsigned int ip;
unsigned short port;
};
//地址端口结构 struct ipport {
unsigned int ip;
unsigned short port; booloperator< (const ipport rhs) const{return (ip < rhs.ip || (ip == rhs.ip && port < rhs.port));} booloperator== (const ipport rhs) const{return (ip == rhs.ip && port == rhs.port);}
};
//对应于对方地址端口的连接信息 struct peerinfo { int fd; //对应连接句柄 unsigned int contime; //最后连接时间 unsigned int rcvtime; //收到数据时间 unsigned int rcvbyte; //收到字节个数 unsigned int sndtime; //发送数据时间 unsigned int sndbyte; //发送字节个数 };
//连接结构 struct conninfo { int rfd; //管道读端 int wfd; //管道写端 map<struct ipport, struct peerinfo> peer; //对方信息 };
#pragma pack()
//全局运行标志 bool g_bRun;
//全局连接信息 struct conninfo g_ConnInfo;
void setnonblocking(int sock) { int opts;
opts = fcntl(sock,F_GETFL); if (opts <0) {
perror("fcntl(sock,GETFL)");
exit(1);
}
opts = opts|O_NONBLOCK; if (fcntl(sock, F_SETFL, opts) <0) {
perror("fcntl(sock,SETFL,opts)");
exit(1);
}
}
void setreuseaddr(int sock) { int opt;
opt =1; if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(&opt)) <0) {
perror("setsockopt");
exit(1);
}
}