Added.
This commit is contained in:
parent
be9940f570
commit
608352b366
@ -14,10 +14,13 @@ int main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Socket client("127.0.0.1",9048,false,false);
|
Server client("127.0.0.1",9049,"127.0.0.1",9048);
|
||||||
while (1) {
|
while (1) {
|
||||||
client.PacketSend("Hello");
|
raw_data trdt;
|
||||||
usleep(50000);
|
trdt.setData("Hello");
|
||||||
|
client.SignedRawdata(&trdt, "RSTR");
|
||||||
|
client.SentRawdata(&trdt);
|
||||||
|
usleep(5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (char const *str) {
|
} catch (char const *str) {
|
||||||
|
8
net.cpp
8
net.cpp
@ -62,13 +62,13 @@ int main(void){
|
|||||||
CPart::addArg<int>(&args, 6);
|
CPart::addArg<int>(&args, 6);
|
||||||
CPart::addArg<double>(&args, 8.2);
|
CPart::addArg<double>(&args, 8.2);
|
||||||
// 输入过程
|
// 输入过程
|
||||||
struct compute_result cpur = {"Test",&args,&args,&fargs,&fargs};
|
/* struct compute_result cpur = {"Test",&args,&args,&fargs,&fargs};
|
||||||
packet pkt = srvr.CPURS2Packet(cpur);
|
packet pkt = srvr.CPURS2Packet(cpur);
|
||||||
raw_data rwd = srvr.Packet2Rawdata(pkt);
|
raw_data rwd = srvr.Packet2Rawdata(pkt);*/
|
||||||
|
|
||||||
// 输出过程
|
// 输出过程
|
||||||
srvr.Rawdata2Packet(rwd);
|
//srvr.Rawdata2Packet(rwd);
|
||||||
|
srvr.Deamon();
|
||||||
while(1){
|
while(1){
|
||||||
sleep(100);
|
sleep(100);
|
||||||
}
|
}
|
||||||
|
37
net.h
37
net.h
@ -74,6 +74,12 @@ public:
|
|||||||
socklen_t size(void){
|
socklen_t size(void){
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
void SetPort(int port){
|
||||||
|
address.sin_port = htons(port);
|
||||||
|
}
|
||||||
|
void SetIP(string ip_addr){
|
||||||
|
address.sin_addr.s_addr = inet_addr(ip_addr.data());
|
||||||
|
}
|
||||||
void setSize(void){
|
void setSize(void){
|
||||||
len = sizeof(address);
|
len = sizeof(address);
|
||||||
}
|
}
|
||||||
@ -88,6 +94,8 @@ public:
|
|||||||
Addr addr;
|
Addr addr;
|
||||||
int nsfd,sfd,port;
|
int nsfd,sfd,port;
|
||||||
bool server,tcp,ipv4,set_fcntl = false;
|
bool server,tcp,ipv4,set_fcntl = false;
|
||||||
|
// 缓冲区
|
||||||
|
char buff[BUFSIZ];
|
||||||
void (*func)(class Socket &,int ,Addr);
|
void (*func)(class Socket &,int ,Addr);
|
||||||
Socket(string ip_addr, int port, bool server = false, bool tcp = true, bool ipv4 = true){
|
Socket(string ip_addr, int port, bool server = false, bool tcp = true, bool ipv4 = true){
|
||||||
if(ipv4)
|
if(ipv4)
|
||||||
@ -154,6 +162,19 @@ public:
|
|||||||
sendto(sfd, buff.data(), buff.size(), 0, addr.obj(), addr.size());
|
sendto(sfd, buff.data(), buff.size(), 0, addr.obj(), addr.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 重新设置发送模式的端口
|
||||||
|
void SetSendPort(int port){
|
||||||
|
if(!server){
|
||||||
|
addr.SetPort(port);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 重新设置发送模式的IP地址
|
||||||
|
void SetSendIP(string ip_addr){
|
||||||
|
if(!server){
|
||||||
|
addr.SetIP(ip_addr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 发送一段二进制信息
|
// 发送一段二进制信息
|
||||||
void PacketSendRAW(char *buff, unsigned long size){
|
void PacketSendRAW(char *buff, unsigned long size){
|
||||||
if(!tcp)
|
if(!tcp)
|
||||||
@ -163,7 +184,6 @@ public:
|
|||||||
// 接受储存字符串信息的UDP包
|
// 接受储存字符串信息的UDP包
|
||||||
int PacketRecv(Addr &t_addr, string &str){
|
int PacketRecv(Addr &t_addr, string &str){
|
||||||
if(!tcp){
|
if(!tcp){
|
||||||
char buff[BUFSIZ];
|
|
||||||
ssize_t tlen;
|
ssize_t tlen;
|
||||||
// 非阻塞输入
|
// 非阻塞输入
|
||||||
if(set_fcntl){
|
if(set_fcntl){
|
||||||
@ -203,39 +223,38 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 接受储存二进制信息的UDP包
|
// 接受储存二进制信息的UDP包
|
||||||
ssize_t PacketRecvRAW(Addr &t_addr, char *p_rdt){
|
ssize_t PacketRecvRAW(Addr &t_addr, char **p_rdt){
|
||||||
if(!tcp){
|
if(!tcp){
|
||||||
char buff[BUFSIZ];
|
|
||||||
ssize_t tlen;
|
ssize_t tlen;
|
||||||
// 非阻塞输入
|
// 非阻塞输入
|
||||||
if(set_fcntl){
|
if(set_fcntl){
|
||||||
tlen = recvfrom(sfd, (void *)buff, BUFSIZ, 0, t_addr.obj(), t_addr.sizep());
|
tlen = recvfrom(sfd, (void *)buff, BUFSIZ, 0, t_addr.obj(), t_addr.sizep());
|
||||||
// 读取错误
|
// 读取错误
|
||||||
if(tlen == -1 && errno != EAGAIN){
|
if(tlen == -1 && errno != EAGAIN){
|
||||||
p_rdt = nullptr;
|
*p_rdt = nullptr;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
// 缓冲区没有信息
|
// 缓冲区没有信息
|
||||||
else if(tlen == 0 || (tlen == -1 && errno == EAGAIN)){
|
else if(tlen == 0 || (tlen == -1 && errno == EAGAIN)){
|
||||||
p_rdt = nullptr;
|
*p_rdt = nullptr;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
// 成功读取信息
|
// 成功读取信息
|
||||||
else{
|
else{
|
||||||
p_rdt = (char *)malloc(tlen);
|
*p_rdt = (char *)malloc(tlen);
|
||||||
memcpy(p_rdt, buff, tlen);
|
memcpy(*p_rdt, buff, tlen);
|
||||||
return tlen;
|
return tlen;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
tlen = recvfrom(sfd, (void *)buff, BUFSIZ, 0, t_addr.obj(), t_addr.sizep());
|
tlen = recvfrom(sfd, (void *)buff, BUFSIZ, 0, t_addr.obj(), t_addr.sizep());
|
||||||
if(~tlen){
|
if(~tlen){
|
||||||
p_rdt = (char *)malloc(tlen);
|
*p_rdt = (char *)malloc(tlen);
|
||||||
memcpy(p_rdt, buff, tlen);
|
memcpy(p_rdt, buff, tlen);
|
||||||
return tlen;
|
return tlen;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
p_rdt = nullptr;
|
*p_rdt = nullptr;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
62
server.h
62
server.h
@ -41,6 +41,13 @@ struct raw_data{
|
|||||||
uint32_t info;
|
uint32_t info;
|
||||||
char *msg = NULL;
|
char *msg = NULL;
|
||||||
unsigned long msg_size = 0;
|
unsigned long msg_size = 0;
|
||||||
|
|
||||||
|
void setData(string str){
|
||||||
|
data = (char *)malloc(str.size()+1);
|
||||||
|
size = str.size()+1;
|
||||||
|
memcpy(data, str.data(),str.size());
|
||||||
|
data[str.size()+1] = '\0';
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//设置服务器守护程序的时钟
|
//设置服务器守护程序的时钟
|
||||||
@ -51,28 +58,44 @@ public:
|
|||||||
vector<compute_result> cpurs_in;
|
vector<compute_result> cpurs_in;
|
||||||
vector<packet> packets_in;
|
vector<packet> packets_in;
|
||||||
vector<string> rawstr_in;
|
vector<string> rawstr_in;
|
||||||
Socket socket;
|
Socket socket, send_socket;
|
||||||
int packet_max = 30;
|
int packet_max = 30;
|
||||||
Server(string ip_addr):socket(ip_addr,9048,true,false){
|
Server(string ip_addr, int port = 9048, string send_ip_addr = "127.0.0.1",int send_port = 9049):socket(ip_addr,port,true,false),send_socket(send_ip_addr,send_port,false,false){
|
||||||
socket.UDPSetFCNTL();
|
socket.UDPSetFCNTL();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 重新设置服务器的发送端口
|
||||||
|
void SetSendPort(int port){
|
||||||
|
send_socket.SetSendPort(port);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重新设置服务器的发送IP地址
|
||||||
|
void SetSendIP(string ip_addr){
|
||||||
|
send_socket.SetSendIP(ip_addr);
|
||||||
|
}
|
||||||
|
|
||||||
void Deamon(void){
|
void Deamon(void){
|
||||||
//cout<<"Server Deamon Checked."<<endl;
|
//cout<<"Server Deamon Checked."<<endl;
|
||||||
Addr f_addr;
|
Addr f_addr;
|
||||||
|
|
||||||
int rtn, prm = packet_max;
|
int prm = packet_max;
|
||||||
string str;
|
ssize_t tlen;
|
||||||
|
char *str = nullptr;
|
||||||
printf("Checking Packet.\n");
|
printf("Checking Packet.\n");
|
||||||
do{
|
do{
|
||||||
rtn = socket.PacketRecv(f_addr,str);
|
tlen = socket.PacketRecvRAW(f_addr,&str);
|
||||||
if(rtn == 1){
|
if(tlen > 0){
|
||||||
cout<<str<<endl;
|
cout<<"Get."<<endl;
|
||||||
|
if(CheckRawMsg(str, tlen)){
|
||||||
|
cout<<"Signed Raw Data."<<endl;
|
||||||
|
ProcessSignedRawMsg(str, tlen);
|
||||||
|
}
|
||||||
rawstr_in.push_back(str);
|
rawstr_in.push_back(str);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
|
||||||
}
|
}
|
||||||
}while (rtn && prm-- > 0);
|
}while (tlen && prm-- > 0);
|
||||||
setServerClock(this, 2);
|
setServerClock(this, 2);
|
||||||
}
|
}
|
||||||
// 将计算结果包转化为结构数据包
|
// 将计算结果包转化为结构数据包
|
||||||
@ -211,11 +234,11 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 为原始二进制串打上信息标签
|
// 为原始二进制串打上信息标签
|
||||||
void RawdataAddInfo(struct raw_data *trdt,char info[]){
|
void SignedRawdata(struct raw_data *trdt,string info){
|
||||||
// 填充标签信息
|
// 填充标签信息
|
||||||
memcpy(&trdt->head, "NETC", sizeof(uint32_t));
|
memcpy(&trdt->head, "NETC", sizeof(uint32_t));
|
||||||
memcpy(&trdt->tail, "CTEN", sizeof(uint32_t));
|
memcpy(&trdt->tail, "CTEN", sizeof(uint32_t));
|
||||||
memcpy(&trdt->head, info, sizeof(uint32_t));
|
memcpy(&trdt->info, info.data(), sizeof(uint32_t));
|
||||||
// 整合信息
|
// 整合信息
|
||||||
char *msg = (char *)malloc(sizeof(uint32_t) * 3 + trdt->size);
|
char *msg = (char *)malloc(sizeof(uint32_t) * 3 + trdt->size);
|
||||||
trdt->msg_size = sizeof(uint32_t) * 3 + trdt->size;
|
trdt->msg_size = sizeof(uint32_t) * 3 + trdt->size;
|
||||||
@ -224,14 +247,15 @@ public:
|
|||||||
idx += sizeof(uint32_t);
|
idx += sizeof(uint32_t);
|
||||||
memcpy(idx, &trdt->info, sizeof(uint32_t));
|
memcpy(idx, &trdt->info, sizeof(uint32_t));
|
||||||
idx += sizeof(uint32_t);
|
idx += sizeof(uint32_t);
|
||||||
memcpy(idx, &trdt->data, trdt->size);
|
memcpy(idx, trdt->data, trdt->size);
|
||||||
idx += trdt->size;
|
idx += trdt->size;
|
||||||
memcpy(idx, &trdt->tail, sizeof(uint32_t));
|
memcpy(idx, &trdt->tail, sizeof(uint32_t));
|
||||||
|
trdt->msg = msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送已经打上标签的原始二进制串
|
// 发送已经打上标签的原始二进制串
|
||||||
int SentRawdata(struct raw_data *trdt){
|
int SentRawdata(struct raw_data *trdt){
|
||||||
socket.PacketSendRAW(trdt->msg, trdt->msg_size);
|
send_socket.PacketSendRAW(trdt->msg, trdt->msg_size);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -241,14 +265,24 @@ public:
|
|||||||
char *idx = p_rdt;
|
char *idx = p_rdt;
|
||||||
memcpy(&head, "NETC", sizeof(uint32_t));
|
memcpy(&head, "NETC", sizeof(uint32_t));
|
||||||
memcpy(&tail, "CTEN", sizeof(uint32_t));
|
memcpy(&tail, "CTEN", sizeof(uint32_t));
|
||||||
if(memcmp(idx, &head, sizeof(uint32_t))){
|
if(!memcmp(idx, &head, sizeof(uint32_t))){
|
||||||
idx += size-sizeof(uint32_t);
|
idx += size-sizeof(uint32_t);
|
||||||
if(memcmp(idx, &tail, sizeof(uint32_t))) return true;
|
if(!memcmp(idx, &tail, sizeof(uint32_t))) return true;
|
||||||
else return false;
|
else return false;
|
||||||
}
|
}
|
||||||
else return false;
|
else return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理一个已打上标签的原始二进制串,获得其中储存的信息
|
||||||
|
raw_data ProcessSignedRawMsg(char *p_rdt, ssize_t size){
|
||||||
|
raw_data trdt;
|
||||||
|
trdt.data = (char *)malloc(size-3*sizeof(uint32_t));
|
||||||
|
memcpy(&trdt.info, p_rdt+sizeof(uint32_t), sizeof(uint32_t));
|
||||||
|
memcpy(trdt.data, p_rdt+sizeof(uint32_t)*2, size-3*sizeof(uint32_t));
|
||||||
|
printf("Data:%s\n",trdt.data);
|
||||||
|
return trdt;
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* server_h */
|
#endif /* server_h */
|
||||||
|
Loading…
Reference in New Issue
Block a user