From d5c255aca644435d14384ae28e64aa5b8b8772ce Mon Sep 17 00:00:00 2001 From: Saturneic Date: Thu, 17 Jan 2019 22:55:52 +0800 Subject: [PATCH] Added and fixed. --- Net.xcodeproj/project.pbxproj | 28 ++- addr.cpp | 57 ++++++ client.cpp | 17 +- clock.cpp | 66 +++++++ clock.h | 29 +++ cmap.h | 9 +- cpart.h | 16 +- cthread.cpp | 1 - cthread.h | 15 +- net.cpp => main.cpp | 44 +---- net.h | 324 +++++----------------------------- server.cpp | 224 ++++++++++++++++++++++- server.h | 246 ++++---------------------- socket.cpp | 193 ++++++++++++++++++++ type.h | 44 +++++ 15 files changed, 746 insertions(+), 567 deletions(-) create mode 100644 addr.cpp create mode 100644 clock.cpp create mode 100644 clock.h rename net.cpp => main.cpp (51%) create mode 100644 socket.cpp create mode 100644 type.h diff --git a/Net.xcodeproj/project.pbxproj b/Net.xcodeproj/project.pbxproj index 8a9aac2..74537c6 100644 --- a/Net.xcodeproj/project.pbxproj +++ b/Net.xcodeproj/project.pbxproj @@ -7,10 +7,13 @@ objects = { /* Begin PBXBuildFile section */ - 9221DA1121EB5FB8007310A7 /* net.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9221DA1021EB5FB8007310A7 /* net.cpp */; }; + 9221DA1121EB5FB8007310A7 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9221DA1021EB5FB8007310A7 /* main.cpp */; }; 925A13A621EC68D500CBD427 /* cpart.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 925A13A421EC67C900CBD427 /* cpart.cpp */; }; 925A13A921EC973000CBD427 /* cmap.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 925A13A721EC973000CBD427 /* cmap.cpp */; }; 925A13AD21EC9DB900CBD427 /* cthread.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 925A13AB21EC9DB900CBD427 /* cthread.cpp */; }; + 92A1F29821F0C19500340EFA /* socket.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92A1F29721F0C19500340EFA /* socket.cpp */; }; + 92A1F29B21F0C5CC00340EFA /* clock.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92A1F29921F0C5CC00340EFA /* clock.cpp */; }; + 92A1F29E21F0C72C00340EFA /* addr.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92A1F29D21F0C72C00340EFA /* addr.cpp */; }; 92D6CE6921EE4920005AEF3B /* server.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92D6CE6721EE4920005AEF3B /* server.cpp */; }; /* End PBXBuildFile section */ @@ -29,7 +32,7 @@ /* Begin PBXFileReference section */ 9221D9EB21EA5142007310A7 /* Net */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Net; sourceTree = BUILT_PRODUCTS_DIR; }; 9221DA0F21EB5FB8007310A7 /* net.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = net.h; sourceTree = ""; }; - 9221DA1021EB5FB8007310A7 /* net.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = net.cpp; sourceTree = ""; }; + 9221DA1021EB5FB8007310A7 /* main.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = ""; }; 9221DA1421EB62F6007310A7 /* cpart.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cpart.h; sourceTree = ""; }; 9221DA1621EB8C02007310A7 /* pcs.map */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; name = pcs.map; path = build/Debug/PCS/pcs.map; sourceTree = ""; }; 925A13A421EC67C900CBD427 /* cpart.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = cpart.cpp; sourceTree = ""; }; @@ -37,6 +40,11 @@ 925A13A821EC973000CBD427 /* cmap.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cmap.h; sourceTree = ""; }; 925A13AB21EC9DB900CBD427 /* cthread.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = cthread.cpp; sourceTree = ""; }; 925A13AC21EC9DB900CBD427 /* cthread.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cthread.h; sourceTree = ""; }; + 92A1F29721F0C19500340EFA /* socket.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = socket.cpp; sourceTree = ""; }; + 92A1F29921F0C5CC00340EFA /* clock.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = clock.cpp; sourceTree = ""; }; + 92A1F29A21F0C5CC00340EFA /* clock.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = clock.h; sourceTree = ""; }; + 92A1F29C21F0C67600340EFA /* type.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = type.h; sourceTree = ""; }; + 92A1F29D21F0C72C00340EFA /* addr.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = addr.cpp; sourceTree = ""; }; 92D6CE6721EE4920005AEF3B /* server.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = server.cpp; sourceTree = ""; }; 92D6CE6821EE4920005AEF3B /* server.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = server.h; sourceTree = ""; }; /* End PBXFileReference section */ @@ -72,12 +80,15 @@ isa = PBXGroup; children = ( 925A13AA21EC989500CBD427 /* include */, - 9221DA1021EB5FB8007310A7 /* net.cpp */, + 9221DA1021EB5FB8007310A7 /* main.cpp */, + 92A1F29721F0C19500340EFA /* socket.cpp */, 9221DA1621EB8C02007310A7 /* pcs.map */, 925A13A421EC67C900CBD427 /* cpart.cpp */, 925A13A721EC973000CBD427 /* cmap.cpp */, 925A13AB21EC9DB900CBD427 /* cthread.cpp */, 92D6CE6721EE4920005AEF3B /* server.cpp */, + 92A1F29921F0C5CC00340EFA /* clock.cpp */, + 92A1F29D21F0C72C00340EFA /* addr.cpp */, ); name = Net; sourceTree = ""; @@ -85,11 +96,13 @@ 925A13AA21EC989500CBD427 /* include */ = { isa = PBXGroup; children = ( + 92A1F29A21F0C5CC00340EFA /* clock.h */, + 925A13A821EC973000CBD427 /* cmap.h */, + 9221DA1421EB62F6007310A7 /* cpart.h */, 925A13AC21EC9DB900CBD427 /* cthread.h */, 9221DA0F21EB5FB8007310A7 /* net.h */, - 9221DA1421EB62F6007310A7 /* cpart.h */, - 925A13A821EC973000CBD427 /* cmap.h */, 92D6CE6821EE4920005AEF3B /* server.h */, + 92A1F29C21F0C67600340EFA /* type.h */, ); name = include; sourceTree = ""; @@ -150,10 +163,13 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 9221DA1121EB5FB8007310A7 /* net.cpp in Sources */, + 9221DA1121EB5FB8007310A7 /* main.cpp in Sources */, 925A13A621EC68D500CBD427 /* cpart.cpp in Sources */, + 92A1F29B21F0C5CC00340EFA /* clock.cpp in Sources */, 925A13A921EC973000CBD427 /* cmap.cpp in Sources */, + 92A1F29E21F0C72C00340EFA /* addr.cpp in Sources */, 92D6CE6921EE4920005AEF3B /* server.cpp in Sources */, + 92A1F29821F0C19500340EFA /* socket.cpp in Sources */, 925A13AD21EC9DB900CBD427 /* cthread.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/addr.cpp b/addr.cpp new file mode 100644 index 0000000..28e9f32 --- /dev/null +++ b/addr.cpp @@ -0,0 +1,57 @@ +// +// addr.cpp +// Net +// +// Created by 胡一兵 on 2019/1/17. +// Copyright © 2019年 Bakantu. All rights reserved. +// + +#include "net.h" + +Addr::Addr(string ip_addr, int port, bool ipv4){ + memset(&address, 0, sizeof(struct sockaddr_in)); + if(ipv4) + address.sin_family = AF_INET; + else + address.sin_family = AF_INET6; + address.sin_port = htons(port); + address.sin_addr.s_addr = inet_addr(ip_addr.data()); + addr_size = sizeof(address); +} + +Addr::Addr(struct sockaddr_in saddri){ + memset(&address, 0, sizeof(struct sockaddr_in)); + address = saddri; +} + +Addr::Addr(){ + memset(&address, 0, sizeof(struct sockaddr_in)); +} + +Addr::Addr(const Addr &t_addr){ + address = t_addr.address; + addr_size = t_addr.addr_size; +} + + +socklen_t *Addr::sizep(void){ + return &addr_size; +} + +socklen_t Addr::size(void){ + return addr_size; +} + +void Addr::SetPort(int port){ + address.sin_port = htons(port); + addr_size = sizeof(address); +} + +void Addr::SetIP(string ip_addr){ + address.sin_addr.s_addr = inet_addr(ip_addr.data()); + addr_size = sizeof(address); +} + +struct sockaddr *Addr::obj(void){ + return (struct sockaddr *) &address; +} diff --git a/client.cpp b/client.cpp index 18659b1..2283c2b 100644 --- a/client.cpp +++ b/client.cpp @@ -15,12 +15,19 @@ int main(int argc, char *argv[]) try { Server client("127.0.0.1",9049,"127.0.0.1",9048); + vector fargs = {1,0,0,1}; + vectorargs; + CPart::addArg(&args, 12.63); + CPart::addArg(&args, 10); + CPart::addArg(&args, 6); + CPart::addArg(&args, 8.2); + struct compute_result cpur = {"Test",&args,&args,&fargs,&fargs}; + packet pkt = client.CPURS2Packet(cpur); + raw_data rwd = client.Packet2Rawdata(pkt); + client.SignedRawdata(&rwd, "RSTR"); while (1) { - raw_data trdt; - trdt.setData("Hello"); - client.SignedRawdata(&trdt, "RSTR"); - client.SentRawdata(&trdt); - usleep(5000); + client.SentRawdata(&rwd); + usleep(1000); } } catch (char const *str) { diff --git a/clock.cpp b/clock.cpp new file mode 100644 index 0000000..86443eb --- /dev/null +++ b/clock.cpp @@ -0,0 +1,66 @@ +// +// clock.cpp +// Net +// +// Created by 胡一兵 on 2019/1/17. +// Copyright © 2019年 Bakantu. All rights reserved. +// + +#include "type.h" +#include "clock.h" + +#define CLOCKESE 15 + +list clocks_list; +static struct itimerval oitrl, itrl; + +static uint64_t clock_erase = CLOCKESE; + + +void initClock(void){ + signal(SIGALRM, threadsClock); + setThreadsClock(); +} + +//设置全局线程时钟 +void setThreadsClock(void){ + itrl.it_interval.tv_sec = 0; + itrl.it_interval.tv_usec = 50000; + itrl.it_value.tv_sec = 0; + itrl.it_value.tv_usec = 50000; + setitimer(ITIMER_REAL, &itrl, &oitrl); +} + +//时钟滴答调用函数 +void threadsClock(int n){ +// 删除到期时钟 + if(clock_erase == 0){ + for(auto i = clocks_list.begin(); i != clocks_list.end();){ + if(i->click == 0) i = clocks_list.erase(i); + else i++; + } +// 重设总滴答数 + clock_erase = CLOCKESE; + } + else clock_erase--; +// 处理时钟列表 + for(auto &clock : clocks_list){ + if(clock.click == 0){ + if(clock.if_thread){ + pthread_t ptd = 0; + pthread_create(&ptd, NULL, clock.func, NULL); + } + else{ + clock.func(NULL); + } +// 标记时钟到期 + clock.click = -1; + } + else if(clock.click > 0){ + clock.click--; + } + } + +} + + diff --git a/clock.h b/clock.h new file mode 100644 index 0000000..c2079b3 --- /dev/null +++ b/clock.h @@ -0,0 +1,29 @@ +// +// clock.hpp +// Net +// +// Created by 胡一兵 on 2019/1/17. +// Copyright © 2019年 Bakantu. All rights reserved. +// + +#ifndef clock_h +#define clock_h + +#include "type.h" + +//时钟管理结构 +struct clock_register{ + void *(*func)(void *); + bool if_thread; + int click; +}; + +//初始化全局时钟 +void initClock(void) +//设置全局线程时钟 +void setThreadsClock(void); +//时钟滴答调用函数 +void threadsClock(int); + + +#endif /* clock_h */ diff --git a/cmap.h b/cmap.h index 77a96c2..0a4b8fb 100644 --- a/cmap.h +++ b/cmap.h @@ -9,16 +9,11 @@ #ifndef cmap_h #define cmap_h +#include "type.h" #include "cpart.h" -#include -#include -#include -using std::map; -using std::ifstream; -using std::pair; -//计算任务无环有向图管理 +//计算模块图类 class CMap{ public: // 图中包含的的计算模块列表 diff --git a/cpart.h b/cpart.h index 19f8f95..1804a82 100644 --- a/cpart.h +++ b/cpart.h @@ -9,13 +9,7 @@ #ifndef cpart_h #define cpart_h -#include -#include -#include -#include - -using std::vector; -using std::string; +#include "type.h" //声明计算模块的传入与传出参数列表 #define ARGS_DECLAER(name) vector __##name##_args_in, __##name##_args_out @@ -25,13 +19,14 @@ using std::string; #define GET_ARGS(name,type) CPart::popArg(&__##name##_args_in) #define ADD_ARGS(name,type,value) CPart::addArg(&__##name##_args_out, value); - //从传入参数列表的第一个值,并删除该值 #define POP_ARGS(name,type) GET_ARGS( name ,type) //向传出参数列表中添加值 #define PUSH_ARGS(name,type,value) ADD_ARGS( name ,type,value) +//整型 #define INT 0 +//浮点型 #define DOUBLE 1 //调用计算模块成功的返回 @@ -39,11 +34,12 @@ using std::string; //调用计算模块失败的返回 #define FAIL -1 +//计算模块入口函数类型 typedef int(*PCSFUNC)(void); class CPart; -//计算模块管理对象间的依赖关系 +//计算模块管理对象间的依赖关系管理结构 class Depends{ public: // 指向依赖的计算模块管理对象的指针 @@ -52,7 +48,7 @@ public: vector args; }; -//计算模块管理对象 +//计算模块类 class CPart{ public: // 参数形式信息列表 diff --git a/cthread.cpp b/cthread.cpp index 1c1b73d..1200396 100644 --- a/cthread.cpp +++ b/cthread.cpp @@ -313,7 +313,6 @@ void CThread::SignedCpart(void *args, CPart *pcp){ pifsolved->find(pcp->name)->second = true; } -//注册任务进程时钟调用 void setTrdClock(CThread *ptd){ daemon_list.push_back(ptd); } diff --git a/cthread.h b/cthread.h index 9eec979..9b489d5 100644 --- a/cthread.h +++ b/cthread.h @@ -9,19 +9,13 @@ #ifndef cthread_h #define cthread_h +#include "type.h" #include "cpart.h" #include "cmap.h" #include "server.h" -#include -#include - -using std::list; - - class CThread; - //线程信息记录结构体 struct thread_args{ // 子线程编号 @@ -37,7 +31,7 @@ struct thread_args{ //并行任务处理进程信息结构体 struct line_process{ // 是否开启并行任务管理 - bool if_als; + bool if_als = false; // 已经释放的子线程 listchild_finished; // 子线程管理状态记录 @@ -48,10 +42,10 @@ struct line_process{ list line; }; -//计算进程管理结构 +//计算任务类 class CThread{ public: -// 对应的图结构管理结构 +// 计算任务图纸 const CMap * const p_map; // 此计算进程中计算模块的传入参数数据列表 map> rargs; @@ -74,6 +68,7 @@ public: // 使用图结构管理结构来构造计算进程管理结构 CThread(CMap *tp_map, int thdnum = 4); ~CThread(); + template // 添加相关计算模块的传入参数 void AddArgs(string name, T value){ diff --git a/net.cpp b/main.cpp similarity index 51% rename from net.cpp rename to main.cpp index 195dcb6..3267b0d 100644 --- a/net.cpp +++ b/main.cpp @@ -6,53 +6,17 @@ // Copyright © 2019年 Bakantu. All rights reserved. // +#include "type.h" +#include "clock.h" #include "net.h" #include "cpart.h" #include "cmap.h" #include "cthread.h" -extern list daemon_list; -extern list server_list; -static struct itimerval oitrl, itrl; -void init(void){ - signal(SIGALRM, threadsClock); - setThreadsClock(); -} - -//设置全局线程时钟 -void setThreadsClock(void){ - itrl.it_interval.tv_sec = 0; - itrl.it_interval.tv_usec = 500000; - itrl.it_value.tv_sec = 0; - itrl.it_value.tv_usec = 500000; - setitimer(ITIMER_REAL, &itrl, &oitrl); -} -//时钟滴答调用函数 -void threadsClock(int n){ - //printf("Clock click.\n"); - for(auto i = daemon_list.begin(); i != daemon_list.end(); i++){ - (*i)->Daemon(); - } -// 服务器守护程序 - for(auto i = server_list.begin(); i != server_list.end(); i++){ - - if(--(*i).click == 0){ - (*i).psvr->Deamon(); - } - } - for(auto i = server_list.begin(); i != server_list.end();){ - if((*i).click == 0){ - i = server_list.erase(i); - } - else i++; - } - - daemon_list.clear(); -} int main(void){ - init(); + initClock(); Server srvr("127.0.0.1"); //srvr.Deamon(); vector fargs = {1,0,0,1}; @@ -68,7 +32,7 @@ int main(void){ // 输出过程 //srvr.Rawdata2Packet(rwd); - srvr.Deamon(); + //srvr.Deamon(NULL); while(1){ sleep(100); } diff --git a/net.h b/net.h index 9660908..c8b2ec3 100644 --- a/net.h +++ b/net.h @@ -9,300 +9,70 @@ #ifndef net_hpp #define net_hpp -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "cpart.h" - -using std::string; -using std::vector; -using std::map; -using std::pair; -using std::list; -using std::ifstream; -using std::cout; -using std::endl; - +#include "type.h" +//IP地址管理类 class Addr{ -public: +// IP地址管理结构 struct sockaddr_in address; - socklen_t len; - Addr(string ip_addr, int port = 0, bool ipv4 = true){ - memset(&address, 0, sizeof(struct sockaddr_in)); - if(ipv4) - address.sin_family = AF_INET; - else - address.sin_family = AF_INET6; - address.sin_port = htons(port); - address.sin_addr.s_addr = inet_addr(ip_addr.data()); - len = sizeof(address); - } - Addr(struct sockaddr_in saddri){ - memset(&address, 0, sizeof(struct sockaddr_in)); - address = saddri; - } - Addr(){ - memset(&address, 0, sizeof(struct sockaddr_in)); - } - Addr(const Addr &t_addr){ - address = t_addr.address; - len = t_addr.len; - } - socklen_t *sizep(void){ - return &len; - } - socklen_t size(void){ - 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){ - len = sizeof(address); - } - struct sockaddr *obj(void){ - return (struct sockaddr *) &address; - } +// IP地址管理结构的大小 + socklen_t addr_size = 0; +public: + Addr(string ip_addr, int port = 0, bool ipv4 = true); + Addr(struct sockaddr_in saddri); + Addr(); + Addr(const Addr &t_addr); +// 获得记录IP地址管理结构的大小的变量本身 + socklen_t *sizep(void); +// 获得IP地址管理结构大小 + socklen_t size(void); +// 重新设置IP地址管理结构所对应的端口 + void SetPort(int port); +// 重新设置IP地址管理结构所对应的IP地址 + void SetIP(string ip_addr); +// 获得指向IP地址管理结构的指针 + struct sockaddr *obj(void); }; +//套接字管理类 class Socket{ -public: struct sockaddr_in c_addr; Addr addr; int nsfd,sfd,port; +// 属性记录变量 bool server,tcp,ipv4,set_fcntl = false; // 缓冲区 char buff[BUFSIZ]; void (*func)(class Socket &,int ,Addr); - Socket(string ip_addr, int port, bool server = false, bool tcp = true, bool ipv4 = true){ - if(ipv4) - addr.address.sin_family = AF_INET; - else - addr.address.sin_family = AF_INET6; - addr.address.sin_port = htons(port); - this->port = port; - addr.address.sin_addr.s_addr = inet_addr(ip_addr.data()); - addr.setSize(); - this->server = server; - this->tcp = tcp; - this->ipv4 = ipv4; - int TAU = SOCK_STREAM; - if(!tcp) TAU = SOCK_DGRAM; - //如果是服务端 - if(server){ - if(ipv4) sfd = socket(AF_INET,TAU,0); - else sfd = socket(AF_INET6,TAU,0); - if(!~sfd) throw "fail to get sfd"; - if(!~bind(sfd, addr.obj(), addr.size())) throw "fail to bind"; - } - else{ - if(ipv4) sfd = socket(PF_INET,TAU,0); - else sfd = socket(PF_INET6,TAU,0); - if(tcp && !~connect(sfd,addr.obj(),addr.size())) - throw "connection fail"; - - } - } +public: + Socket(string ip_addr, int port, bool server = false, bool tcp = true, bool ipv4 = true); + ~Socket(); +// TCP连接模式下进行端口监听 + void Listen(int connection, void (*func)(class Socket &,int ,Addr) = NULL); +// TCP链接模式下接受连接 + void Accept(void); +// TCP模式下发送简单字符串数据 + void Send(int t_nsfd, string buff); +// TCP模式下接收简单字符串数据 + string Recv(int t_nsfd); - ~Socket(){ - close(sfd); - } +// UDP模式下发送简单字符串数据 + void PacketSend(string buff); +// UDP模式下发送一个带有二进制原始信息的数据包 + void PacketSendRAW(char *buff, unsigned long size); +// UDP模式下接受储存简单字符串信息的数据包 + int PacketRecv(Addr &t_addr, string &str); +// UDP模式下接受储存二进制信息的数据包 + ssize_t PacketRecvRAW(Addr &t_addr, char **p_rdt); +// UDP模式下设置非阻塞模式 + void UDPSetFCNTL(void); - void Listen(int connection, void (*func)(class Socket &,int ,Addr) = NULL){ - if(server && tcp){ - listen(sfd, 10); - this->func = func; - } - } +// 重新设置客户端模式下的发送目的地的端口 + void SetSendPort(int port); +// 重新设置客户端模式下的发送目的地的IP地址 + void SetSendIP(string ip_addr); - void Accept(void){ - if(server && tcp){ - socklen_t scaddr = sizeof(struct sockaddr); - nsfd = accept(sfd,(struct sockaddr *) &c_addr, &scaddr); - Addr addr(c_addr); - if(~nsfd){ - if(func != NULL) func(*this,nsfd,addr); - close(nsfd); - } - } - } - - void Send(int t_nsfd, string buff){ - if(tcp){ - ssize_t len = send(t_nsfd,buff.data(),buff.size(),0); - if(len != buff.size()) throw "size unmatch"; - } - } - - void PacketSend(string buff){ - if(!tcp) - 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){ - if(!tcp) - sendto(sfd, buff, size, 0, addr.obj(), addr.size()); - } - -// 接受储存字符串信息的UDP包 - int PacketRecv(Addr &t_addr, string &str){ - if(!tcp){ - ssize_t tlen; -// 非阻塞输入 - if(set_fcntl){ - tlen = recvfrom(sfd, (void *)buff, BUFSIZ, 0, t_addr.obj(), t_addr.sizep()); - // 读取错误 - if(tlen == -1 && errno != EAGAIN){ - str = ""; - return -1; - } - // 缓冲区没有信息 - else if(tlen == 0 || (tlen == -1 && errno == EAGAIN)){ - str = ""; - return 0; - } - // 成功读取信息 - else{ - str = buff; - buff[tlen] = '\0'; - return 1; - } - } - else{ - tlen = recvfrom(sfd, (void *)buff, BUFSIZ, 0, t_addr.obj(), t_addr.sizep()); - if(~tlen){ - str = buff; - buff[tlen] = '\0'; - return 1; - } - else{ - str = ""; - return -1; - } - - } - } - throw "connection is tcp"; - } - -// 接受储存二进制信息的UDP包 - ssize_t PacketRecvRAW(Addr &t_addr, char **p_rdt){ - if(!tcp){ - ssize_t tlen; - // 非阻塞输入 - if(set_fcntl){ - tlen = recvfrom(sfd, (void *)buff, BUFSIZ, 0, t_addr.obj(), t_addr.sizep()); - // 读取错误 - if(tlen == -1 && errno != EAGAIN){ - *p_rdt = nullptr; - return -1; - } - // 缓冲区没有信息 - else if(tlen == 0 || (tlen == -1 && errno == EAGAIN)){ - *p_rdt = nullptr; - return 0; - } - // 成功读取信息 - else{ - *p_rdt = (char *)malloc(tlen); - memcpy(*p_rdt, buff, tlen); - return tlen; - } - } - else{ - tlen = recvfrom(sfd, (void *)buff, BUFSIZ, 0, t_addr.obj(), t_addr.sizep()); - if(~tlen){ - *p_rdt = (char *)malloc(tlen); - memcpy(p_rdt, buff, tlen); - return tlen; - } - else{ - *p_rdt = nullptr; - return -1; - } - - } - } - throw "connection is tcp"; - } - - unsigned long IfHasPacket(void){ - - return 0; - } - - void UDPSetFCNTL(void){ - if(!tcp){ - int flags = fcntl(sfd, F_GETFL, 0); - fcntl(sfd, F_SETFL, flags | O_NONBLOCK); - set_fcntl = true; - } - } - - string Recv(int t_nsfd){ - if(tcp){ - char buff[BUFSIZ]; - ssize_t len=recv(t_nsfd,buff,BUFSIZ,0); - if(len > 0){ - buff[len] = '\0'; - string str = buff; - return str; - } - else throw "receive fail"; - } - throw "connection is udp"; - } }; -struct pcs_result{ - char *name[16]; - uint32_t in_size; - char *in_buff; - uint32_t out_size; - char *out_buff; -}; - -//设置全局线程时钟 -void setThreadsClock(void); -//时钟滴答调用函数 -void threadsClock(int); - #endif /* net_hpp */ diff --git a/server.cpp b/server.cpp index 491f6f0..16241e2 100644 --- a/server.cpp +++ b/server.cpp @@ -9,7 +9,229 @@ #include "server.h" list server_list; +list process_list; + +extern list clocks_list; void setServerClock(Server *psvr, int clicks){ - server_list.push_back({psvr,clicks}); + clock_register ncr; + ncr.if_thread = true; + ncr.click = 2; + ncr.func = serverDeamon; + clocks_list.push_back(ncr); +} + +Server::Server(string ip_addr, int port, string send_ip_addr,int send_port):socket(ip_addr,port,true,false),send_socket(send_ip_addr,send_port,false,false){ + socket.UDPSetFCNTL(); +} + +void Server::SetSendPort(int port){ + send_socket.SetSendPort(port); +} + +void Server::SetSendIP(string ip_addr){ + send_socket.SetSendIP(ip_addr); +} + +// 将计算结果包转化为结构数据包 +packet Server::CPURS2Packet(compute_result tcpur){ + packet rawpkt; + rawpkt.type = 0; + int count = 0; + // 写入计算模块名字 + rawpkt.buffs.push_back({tcpur.name.size(),(void *)tcpur.name.data()}); + + // 写入输入参数个数 + int *p_value = (int *)malloc(sizeof(uint32_t)); + *p_value = (int)tcpur.args_in->size(); + rawpkt.buffs.push_back({sizeof(uint32_t),p_value}); + // 写入输入参数 + vector &fargs_in = *(tcpur.fargs_in); + for(auto i = tcpur.args_in->begin(); i != tcpur.args_in->end(); i++,count++){ + if(fargs_in[count] == INT){ + int *p_value = (int *)malloc(sizeof(int)); + *p_value = *((int *)(*i)); + rawpkt.buffs.push_back({sizeof(int),p_value}); + } + else if(fargs_in[count] == DOUBLE){ + double *p_value = (double *)malloc(sizeof(double)); + *p_value = *((double *)(*i)); + rawpkt.buffs.push_back({sizeof(double),p_value}); + } + } + // 写入输入参数个数 + p_value = (int *)malloc(sizeof(uint32_t)); + *p_value = (int)tcpur.args_out->size(); + rawpkt.buffs.push_back({sizeof(uint32_t),p_value}); + // 写入输出参数 + count = 0; + vector &fargs_out = *(tcpur.fargs_out); + for(auto i = tcpur.args_out->begin(); i != tcpur.args_out->end(); i++,count++){ + if(fargs_out[count] == INT){ + int *p_value = (int *)malloc(sizeof(int)); + *p_value = *((int *)(*i)); + rawpkt.buffs.push_back({sizeof(int),p_value}); + } + else if(fargs_out[count] == DOUBLE){ + double *p_value = (double *)malloc(sizeof(double)); + *p_value = *((double *)(*i)); + rawpkt.buffs.push_back({sizeof(double),p_value}); + } + } + return rawpkt; +} + +raw_data Server::Packet2Rawdata(packet tpkt){ + raw_data rdta; + char *data = (char *)malloc(BUFSIZ); + memset(data, 0, BUFSIZ); + rdta.data = data; + char *idx = data; + string fdata; +// 写入包ID信息 + memcpy(idx, &tpkt.type, sizeof(uint32_t)); + idx += sizeof(uint32_t); + for(auto i = tpkt.buffs.begin(); i != tpkt.buffs.end(); i++){ +// 写入数据块大小信息 + memcpy(idx, &(*i).first, sizeof(uint32_t)); + idx += sizeof(uint32_t); +// 写入数据块信息 + memcpy(idx, (*i).second, (*i).first); + idx += (*i).first; + } + rdta.size = idx - data; + return rdta; +} + +packet Server::Rawdata2Packet(raw_data trdta){ + packet pkt; + char *idx = trdta.data; +// 数据包ID + uint32_t uint; + memcpy(&pkt.type, idx, sizeof(uint32_t)); + idx += sizeof(uint32_t); +// 数据包主体 + while(idx - trdta.data < trdta.size){ + memcpy(&uint, idx, sizeof(uint32_t)); + idx += sizeof(uint32_t); + void *data = malloc(uint); + memcpy(data, idx, uint); + idx += uint; + pkt.buffs.push_back({uint,data}); + } + return pkt; +} + +compute_result Server::Packet2CPUR(packet *tpkt){ + compute_result tcpur; + tcpur.args_in = new vector(); + tcpur.args_out = new vector(); + if(tpkt->type == 0){ + int nargs_in = *(int *)(tpkt->buffs[0].second); + int nargs_out = *(int *)(tpkt->buffs[nargs_in+1].second); + // 转化输入参数 + for(int i = 0; i < nargs_in; i++){ + (*tcpur.args_in)[i] = malloc(tpkt->buffs[i+1].first); + memcpy((*tcpur.args_in)[i], tpkt->buffs[i+1].second, tpkt->buffs[i+1].first); + } + for(int i = nargs_in+1; i < nargs_in+nargs_out+2; i++){ + (*tcpur.args_out)[i] = malloc(tpkt->buffs[i+1].first); + memcpy((*tcpur.args_out)[i], tpkt->buffs[i+1].second, tpkt->buffs[i+1].first); + } + } + return tcpur; +} + + +void Server::freeRawdataServer(struct raw_data trdt){ + free(trdt.data); + if(trdt.msg != NULL) free(trdt.msg); +} + +void Server::freePcaketServer(struct packet tpkt){ + for(auto i = tpkt.buffs.begin(); i != tpkt.buffs.end(); i++) + free(i->second); + delete &tpkt.buffs; +} + +void Server::freeCPURServer(struct compute_result tcpur){ + // 释放输入参数容器所占用的所有内存 + for(auto i = tcpur.args_in->begin(); i != tcpur.args_in->end(); i++) + free(*i); + delete tcpur.args_in; + + // 释放输出参数容器所占用的所有内存 + for(auto i = tcpur.args_out->begin(); i != tcpur.args_out->end(); i++) + free(*i); + delete tcpur.args_out; +} + +void Server::SignedRawdata(struct raw_data *trdt,string info){ + // 填充标签信息 + memcpy(&trdt->head, "NETC", sizeof(uint32_t)); + memcpy(&trdt->tail, "CTEN", sizeof(uint32_t)); + memcpy(&trdt->info, info.data(), sizeof(uint32_t)); + // 整合信息 + char *msg = (char *)malloc(sizeof(uint32_t) * 3 + trdt->size); + trdt->msg_size = sizeof(uint32_t) * 3 + trdt->size; + char *idx = msg; + memcpy(idx, &trdt->head, sizeof(uint32_t)); + idx += sizeof(uint32_t); + memcpy(idx, &trdt->info, sizeof(uint32_t)); + idx += sizeof(uint32_t); + memcpy(idx, trdt->data, trdt->size); + idx += trdt->size; + memcpy(idx, &trdt->tail, sizeof(uint32_t)); + trdt->msg = msg; +} + +int Server::SentRawdata(struct raw_data *trdt){ + send_socket.PacketSendRAW(trdt->msg, trdt->msg_size); + return 0; +} + +bool Server::CheckRawMsg(char *p_rdt, ssize_t size){ + uint32_t head, tail; + char *idx = p_rdt; + memcpy(&head, "NETC", sizeof(uint32_t)); + memcpy(&tail, "CTEN", sizeof(uint32_t)); + if(!memcmp(idx, &head, sizeof(uint32_t))){ + idx += size-sizeof(uint32_t); + if(!memcmp(idx, &tail, sizeof(uint32_t))) return true; + else return false; + } + else return false; +} + +raw_data Server::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)); + trdt.size = size-3*sizeof(uint32_t); + return trdt; +} + +void *serverDeamon(void *psvr){ + Server svr = *((Server *) psvr); + //cout<<"Server Deamon Checked."< 0){ + // 记录有效数据包 + if(Server::CheckRawMsg(str, tlen)){ + raw_data trdt = Server::ProcessSignedRawMsg(str, tlen); + svr.rawdata_in.push_back(trdt); + } + } + free(str); + }while (tlen && prm-- > 0); + setServerClock(&svr, 2); + pthread_exit(NULL); } diff --git a/server.h b/server.h index bf96b6f..214da68 100644 --- a/server.h +++ b/server.h @@ -9,7 +9,10 @@ #ifndef server_h #define server_h +#include "clock.h" #include "net.h" +#include "cpart.h" +#include "cthread.h" class Server; @@ -50,239 +53,62 @@ struct raw_data{ } }; -//设置服务器守护程序的时钟 -void setServerClock(Server *psvr, int clicks); - +//通用服务器类 class Server{ public: vector cpurs_in; vector packets_in; - vector rawstr_in; + vector rawdata_in; +// 服务器类的接收套接字对象与发送套接字对象 Socket socket, send_socket; int packet_max = 30; - 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(); - } + Server(string ip_addr, int port = 9048, string send_ip_addr = "127.0.0.1",int send_port = 9049); // 重新设置服务器的发送端口 - void SetSendPort(int port){ - send_socket.SetSendPort(port); - } + void SetSendPort(int port); // 重新设置服务器的发送IP地址 - void SetSendIP(string ip_addr){ - send_socket.SetSendIP(ip_addr); - } + void SetSendIP(string ip_addr); - void Deamon(void){ - //cout<<"Server Deamon Checked."< 0){ - cout<<"Get."< 0); - setServerClock(this, 2); - } // 将计算结果包转化为结构数据包 - packet CPURS2Packet(compute_result tcpur){ - packet rawpkt; - rawpkt.type = 0; - int count = 0; -// 写入计算模块名字 - rawpkt.buffs.push_back({tcpur.name.size(),(void *)tcpur.name.data()}); - -// 写入输入参数个数 - int *p_value = (int *)malloc(sizeof(uint32_t)); - *p_value = (int)tcpur.args_in->size(); - rawpkt.buffs.push_back({sizeof(uint32_t),p_value}); -// 写入输入参数 - vector &fargs_in = *(tcpur.fargs_in); - for(auto i = tcpur.args_in->begin(); i != tcpur.args_in->end(); i++,count++){ - if(fargs_in[count] == INT){ - int *p_value = (int *)malloc(sizeof(int)); - *p_value = *((int *)(*i)); - rawpkt.buffs.push_back({sizeof(int),p_value}); - } - else if(fargs_in[count] == DOUBLE){ - double *p_value = (double *)malloc(sizeof(double)); - *p_value = *((double *)(*i)); - rawpkt.buffs.push_back({sizeof(double),p_value}); - } - } -// 写入输入参数个数 - p_value = (int *)malloc(sizeof(uint32_t)); - *p_value = (int)tcpur.args_out->size(); - rawpkt.buffs.push_back({sizeof(uint32_t),p_value}); -// 写入输出参数 - count = 0; - vector &fargs_out = *(tcpur.fargs_out); - for(auto i = tcpur.args_out->begin(); i != tcpur.args_out->end(); i++,count++){ - if(fargs_out[count] == INT){ - int *p_value = (int *)malloc(sizeof(int)); - *p_value = *((int *)(*i)); - rawpkt.buffs.push_back({sizeof(int),p_value}); - } - else if(fargs_out[count] == DOUBLE){ - double *p_value = (double *)malloc(sizeof(double)); - *p_value = *((double *)(*i)); - rawpkt.buffs.push_back({sizeof(double),p_value}); - } - } - return rawpkt; - } + static packet CPURS2Packet(compute_result tcpur); -// 将结构数据包转换成原始二进制串 - raw_data Packet2Rawdata(packet tpkt){ - raw_data rdta; - char *data = (char *)malloc(BUFSIZ); - memset(data, 0, BUFSIZ); - rdta.data = data; - char *idx = data; - string fdata; -// 写入包ID信息 - memcpy(idx, &tpkt.type, sizeof(uint32_t)); - idx += sizeof(uint32_t); - for(auto i = tpkt.buffs.begin(); i != tpkt.buffs.end(); i++){ -// 写入数据块大小信息 - memcpy(idx, &(*i).first, sizeof(uint32_t)); - idx += sizeof(uint32_t); -// 写入数据块信息 - memcpy(idx, &(*i).second, (*i).first); - idx += (*i).first; - } - rdta.size = idx - data; - return rdta; - } - -// 将二进制串信息转换为结构数据包 - packet Rawdata2Packet(raw_data trdta){ - packet pkt; - char *idx = trdta.data; -// 数据包ID - uint32_t uint; - memcpy(&pkt.type, idx, sizeof(uint32_t)); - idx += sizeof(uint32_t); -// 数据包主体 - while(idx - trdta.data < trdta.size){ - memcpy(&uint, idx, sizeof(uint32_t)); - idx += sizeof(uint32_t); - void *data = malloc(uint); - memcpy(data, idx, uint); - idx += uint; - pkt.buffs.push_back({uint,data}); - } - return pkt; - } +// 将结构数据包转换成二进制串 + static raw_data Packet2Rawdata(packet tpkt); +// 将通用二进制串转换为通用数据包 + static packet Rawdata2Packet(raw_data trdta); // 将结构数据包转化为计算结果包 - compute_result Packet2CPUR(packet *tpkt){ - compute_result tcpur; - tcpur.args_in = new vector(); - tcpur.args_out = new vector(); - if(tpkt->type == 0){ - int nargs_in = *(int *)(tpkt->buffs[0].second); - int nargs_out = *(int *)(tpkt->buffs[nargs_in+1].second); -// 转化输入参数 - for(int i = 0; i < nargs_in; i++){ - (*tcpur.args_in)[i] = malloc(tpkt->buffs[i+1].first); - memcpy((*tcpur.args_in)[i], tpkt->buffs[i+1].second, tpkt->buffs[i+1].first); - } - for(int i = nargs_in+1; i < nargs_in+nargs_out+2; i++){ - (*tcpur.args_out)[i] = malloc(tpkt->buffs[i+1].first); - memcpy((*tcpur.args_out)[i], tpkt->buffs[i+1].second, tpkt->buffs[i+1].first); - } - } - return tcpur; - } + static compute_result Packet2CPUR(packet *tpkt); - void freeRawdataServer(struct raw_data trdt){ - free(trdt.data); - if(trdt.msg != NULL) free(trdt.msg); - } +// 释放二进制串占用的空间 + static void freeRawdataServer(struct raw_data trdt); +// 释放通用数据包包占用 + static void freePcaketServer(struct packet tpkt); +// 释放计算结果包占用的空间 + static void freeCPURServer(struct compute_result tcpur); - void freePcaketServer(struct packet tpkt){ - for(auto i = tpkt.buffs.begin(); i != tpkt.buffs.end(); i++) - free(i->second); - delete &tpkt.buffs; - } +// 给二进制串贴上识别标签 + static void SignedRawdata(struct raw_data *trdt,string info); - void freeCPURServer(struct compute_result tcpur){ -// 释放输入参数容器所占用的所有内存 - for(auto i = tcpur.args_in->begin(); i != tcpur.args_in->end(); i++) - free(*i); - delete tcpur.args_in; - -// 释放输出参数容器所占用的所有内存 - for(auto i = tcpur.args_out->begin(); i != tcpur.args_out->end(); i++) - free(*i); - delete tcpur.args_out; - } +// 发送已经贴上标签的二进制串 + int SentRawdata(struct raw_data *trdt); -// 为原始二进制串打上信息标签 - void SignedRawdata(struct raw_data *trdt,string info){ -// 填充标签信息 - memcpy(&trdt->head, "NETC", sizeof(uint32_t)); - memcpy(&trdt->tail, "CTEN", sizeof(uint32_t)); - memcpy(&trdt->info, info.data(), sizeof(uint32_t)); -// 整合信息 - char *msg = (char *)malloc(sizeof(uint32_t) * 3 + trdt->size); - trdt->msg_size = sizeof(uint32_t) * 3 + trdt->size; - char *idx = msg; - memcpy(idx, &trdt->head, sizeof(uint32_t)); - idx += sizeof(uint32_t); - memcpy(idx, &trdt->info, sizeof(uint32_t)); - idx += sizeof(uint32_t); - memcpy(idx, trdt->data, trdt->size); - idx += trdt->size; - memcpy(idx, &trdt->tail, sizeof(uint32_t)); - trdt->msg = msg; - } +// 检查消息串是否为一个贴上标签的二进制串 + static bool CheckRawMsg(char *p_rdt, ssize_t size); -// 发送已经打上标签的原始二进制串 - int SentRawdata(struct raw_data *trdt){ - send_socket.PacketSendRAW(trdt->msg, trdt->msg_size); - return 0; - } +// 处理一个已贴上标签的原始二进制串,获得其包含的信息 + static raw_data ProcessSignedRawMsg(char *p_rdt, ssize_t size); -// 检查二进制信息是否为一个打上标签的原始二进制串 - bool CheckRawMsg(char *p_rdt, ssize_t size){ - uint32_t head, tail; - char *idx = p_rdt; - memcpy(&head, "NETC", sizeof(uint32_t)); - memcpy(&tail, "CTEN", sizeof(uint32_t)); - if(!memcmp(idx, &head, sizeof(uint32_t))){ - idx += size-sizeof(uint32_t); - if(!memcmp(idx, &tail, sizeof(uint32_t))) return true; - 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; - } }; +//设置服务器守护程序的时钟 +void setServerClock(Server *psvr, int clicks); +//服务器守护线程 +void *serverDeamon(void *psvr); + + + #endif /* server_h */ diff --git a/socket.cpp b/socket.cpp new file mode 100644 index 0000000..5963d7c --- /dev/null +++ b/socket.cpp @@ -0,0 +1,193 @@ +// +// socket.cpp +// Net +// +// Created by 胡一兵 on 2019/1/17. +// Copyright © 2019年 Bakantu. All rights reserved. +// + +#include "net.h" + + +Socket::Socket(string ip_addr, int port, bool server, bool tcp, bool ipv4){ + if(ipv4) + addr.address.sin_family = AF_INET; + else + addr.address.sin_family = AF_INET6; + addr.address.sin_port = htons(port); + this->port = port; + addr.address.sin_addr.s_addr = inet_addr(ip_addr.data()); + addr.setSize(); + this->server = server; + this->tcp = tcp; + this->ipv4 = ipv4; + int TAU = SOCK_STREAM; + if(!tcp) TAU = SOCK_DGRAM; + //如果是服务端 + if(server){ + if(ipv4) sfd = socket(AF_INET,TAU,0); + else sfd = socket(AF_INET6,TAU,0); + if(!~sfd) throw "fail to get sfd"; + if(!~bind(sfd, addr.obj(), addr.size())) throw "fail to bind"; + } + else{ + if(ipv4) sfd = socket(PF_INET,TAU,0); + else sfd = socket(PF_INET6,TAU,0); + if(tcp && !~connect(sfd,addr.obj(),addr.size())) + throw "connection fail"; + + } +} + +Socket::~Socket(){ + close(sfd); +} + +void Socket::Listen(int connection, void (*func)(class Socket &,int ,Addr)){ + if(server && tcp){ + listen(sfd, 10); + this->func = func; + } +} + +void Socket::Accept(void){ + if(server && tcp){ + socklen_t scaddr = sizeof(struct sockaddr); + nsfd = accept(sfd,(struct sockaddr *) &c_addr, &scaddr); + Addr addr(c_addr); + if(~nsfd){ + if(func != NULL) func(*this,nsfd,addr); + close(nsfd); + } + } +} + +void Socket::Send(int t_nsfd, string buff){ + if(tcp){ + ssize_t len = send(t_nsfd,buff.data(),buff.size(),0); + if(len != buff.size()) throw "size unmatch"; + } +} + +void Socket::PacketSend(string buff){ + if(!tcp) + sendto(sfd, buff.data(), buff.size(), 0, addr.obj(), addr.size()); +} + +void Socket::SetSendPort(int port){ + if(!server){ + addr.SetPort(port); + } +} + +void Socket::SetSendIP(string ip_addr){ + if(!server){ + addr.SetIP(ip_addr); + } +} +void Socket::PacketSendRAW(char *buff, unsigned long size){ + if(!tcp) + sendto(sfd, buff, size, 0, addr.obj(), addr.size()); +} + +int Socket::PacketRecv(Addr &t_addr, string &str){ + if(!tcp){ + ssize_t tlen; + // 非阻塞输入 + if(set_fcntl){ + tlen = recvfrom(sfd, (void *)buff, BUFSIZ, 0, t_addr.obj(), t_addr.sizep()); + // 读取错误 + if(tlen == -1 && errno != EAGAIN){ + str = ""; + return -1; + } + // 缓冲区没有信息 + else if(tlen == 0 || (tlen == -1 && errno == EAGAIN)){ + str = ""; + return 0; + } + // 成功读取信息 + else{ + str = buff; + buff[tlen] = '\0'; + return 1; + } + } + else{ + tlen = recvfrom(sfd, (void *)buff, BUFSIZ, 0, t_addr.obj(), t_addr.sizep()); + if(~tlen){ + str = buff; + buff[tlen] = '\0'; + return 1; + } + else{ + str = ""; + return -1; + } + + } + } + throw "connection is tcp"; +} + +ssize_t Socket::PacketRecvRAW(Addr &t_addr, char **p_rdt){ + if(!tcp){ + ssize_t tlen; + // 非阻塞输入 + if(set_fcntl){ + tlen = recvfrom(sfd, (void *)buff, BUFSIZ, 0, t_addr.obj(), t_addr.sizep()); + // 读取错误 + if(tlen == -1 && errno != EAGAIN){ + *p_rdt = nullptr; + return -1; + } + // 缓冲区没有信息 + else if(tlen == 0 || (tlen == -1 && errno == EAGAIN)){ + *p_rdt = nullptr; + return 0; + } + // 成功读取信息 + else{ + *p_rdt = (char *)malloc(tlen); + memcpy(*p_rdt, buff, tlen); + return tlen; + } + } + else{ + tlen = recvfrom(sfd, (void *)buff, BUFSIZ, 0, t_addr.obj(), t_addr.sizep()); + if(~tlen){ + *p_rdt = (char *)malloc(tlen); + memcpy(p_rdt, buff, tlen); + return tlen; + } + else{ + *p_rdt = nullptr; + return -1; + } + + } + } + throw "connection is tcp"; +} + +void Socket::UDPSetFCNTL(void){ + if(!tcp){ + int flags = fcntl(sfd, F_GETFL, 0); + fcntl(sfd, F_SETFL, flags | O_NONBLOCK); + set_fcntl = true; + } +} + +string Socket::Recv(int t_nsfd){ + if(tcp){ + char buff[BUFSIZ]; + ssize_t len=recv(t_nsfd,buff,BUFSIZ,0); + if(len > 0){ + buff[len] = '\0'; + string str = buff; + return str; + } + else throw "receive fail"; + } + throw "connection is udp"; +} diff --git a/type.h b/type.h new file mode 100644 index 0000000..c30d5b8 --- /dev/null +++ b/type.h @@ -0,0 +1,44 @@ +// +// type.h +// Net +// +// Created by 胡一兵 on 2019/1/17. +// Copyright © 2019年 Bakantu. All rights reserved. +// + +#ifndef type_h +#define type_h + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using std::string; +using std::vector; +using std::map; +using std::pair; +using std::list; +using std::ifstream; +using std::cout; +using std::endl; + +#endif /* type_h */