Added and fixed.

This commit is contained in:
Saturneic 2019-01-17 22:55:52 +08:00
parent 608352b366
commit d5c255aca6
15 changed files with 746 additions and 567 deletions

View File

@ -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 = "<group>"; };
9221DA1021EB5FB8007310A7 /* net.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = net.cpp; sourceTree = "<group>"; };
9221DA1021EB5FB8007310A7 /* main.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
9221DA1421EB62F6007310A7 /* cpart.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cpart.h; sourceTree = "<group>"; };
9221DA1621EB8C02007310A7 /* pcs.map */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; name = pcs.map; path = build/Debug/PCS/pcs.map; sourceTree = "<group>"; };
925A13A421EC67C900CBD427 /* cpart.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = cpart.cpp; sourceTree = "<group>"; };
@ -37,6 +40,11 @@
925A13A821EC973000CBD427 /* cmap.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cmap.h; sourceTree = "<group>"; };
925A13AB21EC9DB900CBD427 /* cthread.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = cthread.cpp; sourceTree = "<group>"; };
925A13AC21EC9DB900CBD427 /* cthread.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cthread.h; sourceTree = "<group>"; };
92A1F29721F0C19500340EFA /* socket.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = socket.cpp; sourceTree = "<group>"; };
92A1F29921F0C5CC00340EFA /* clock.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = clock.cpp; sourceTree = "<group>"; };
92A1F29A21F0C5CC00340EFA /* clock.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = clock.h; sourceTree = "<group>"; };
92A1F29C21F0C67600340EFA /* type.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = type.h; sourceTree = "<group>"; };
92A1F29D21F0C72C00340EFA /* addr.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = addr.cpp; sourceTree = "<group>"; };
92D6CE6721EE4920005AEF3B /* server.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = server.cpp; sourceTree = "<group>"; };
92D6CE6821EE4920005AEF3B /* server.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = server.h; sourceTree = "<group>"; };
/* 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 = "<group>";
@ -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 = "<group>";
@ -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;

57
addr.cpp Normal file
View File

@ -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;
}

View File

@ -15,12 +15,19 @@ int main(int argc, char *argv[])
try {
Server client("127.0.0.1",9049,"127.0.0.1",9048);
vector<int> fargs = {1,0,0,1};
vector<void *>args;
CPart::addArg<double>(&args, 12.63);
CPart::addArg<int>(&args, 10);
CPart::addArg<int>(&args, 6);
CPart::addArg<double>(&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) {

66
clock.cpp Normal file
View File

@ -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<clock_register> 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--;
}
}
}

29
clock.h Normal file
View File

@ -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 */

9
cmap.h
View File

@ -9,16 +9,11 @@
#ifndef cmap_h
#define cmap_h
#include "type.h"
#include "cpart.h"
#include <map>
#include <fstream>
#include <sstream>
using std::map;
using std::ifstream;
using std::pair;
//计算任务无环有向图管理
//计算模块图类
class CMap{
public:
// 图中包含的的计算模块列表

16
cpart.h
View File

@ -9,13 +9,7 @@
#ifndef cpart_h
#define cpart_h
#include <unistd.h>
#include <dlfcn.h>
#include <string>
#include <vector>
using std::vector;
using std::string;
#include "type.h"
//声明计算模块的传入与传出参数列表
#define ARGS_DECLAER(name) vector<void *> __##name##_args_in, __##name##_args_out
@ -25,13 +19,14 @@ using std::string;
#define GET_ARGS(name,type) CPart::popArg<type>(&__##name##_args_in)
#define ADD_ARGS(name,type,value) CPart::addArg<type>(&__##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<int> args;
};
//计算模块管理对象
//计算模块
class CPart{
public:
// 参数形式信息列表

View File

@ -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);
}

View File

@ -9,19 +9,13 @@
#ifndef cthread_h
#define cthread_h
#include "type.h"
#include "cpart.h"
#include "cmap.h"
#include "server.h"
#include <pthread.h>
#include <list>
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;
// 已经释放的子线程
list<struct thread_args *>child_finished;
// 子线程管理状态记录
@ -48,10 +42,10 @@ struct line_process{
list<CPart *> line;
};
//计算进程管理结构
//计算任务类
class CThread{
public:
// 对应的图结构管理结构
// 计算任务图纸
const CMap * const p_map;
// 此计算进程中计算模块的传入参数数据列表
map<string,vector<void *>> rargs;
@ -74,6 +68,7 @@ public:
// 使用图结构管理结构来构造计算进程管理结构
CThread(CMap *tp_map, int thdnum = 4);
~CThread();
template<class T>
// 添加相关计算模块的传入参数
void AddArgs(string name, T value){

View File

@ -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<CThread *> daemon_list;
extern list<server_clock> 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<int> fargs = {1,0,0,1};
@ -68,7 +32,7 @@ int main(void){
// 输出过程
//srvr.Rawdata2Packet(rwd);
srvr.Deamon();
//srvr.Deamon(NULL);
while(1){
sleep(100);
}

324
net.h
View File

@ -9,300 +9,70 @@
#ifndef net_hpp
#define net_hpp
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <vector>
#include <list>
#include <string>
#include <map>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/time.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#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 */

View File

@ -9,7 +9,229 @@
#include "server.h"
list<server_clock> server_list;
list<server_clock> process_list;
extern list<clock_register> 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<int> &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<int> &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<void *>();
tcpur.args_out = new vector<void *>();
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."<<endl;
Addr f_addr;
int prm = svr.packet_max;
ssize_t tlen;
char *str = nullptr;
printf("Checking Packet.\n");
do{
tlen = svr.socket.PacketRecvRAW(f_addr,&str);
if(tlen > 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);
}

246
server.h
View File

@ -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<compute_result> cpurs_in;
vector<packet> packets_in;
vector<string> rawstr_in;
vector<raw_data> 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."<<endl;
Addr f_addr;
int prm = packet_max;
ssize_t tlen;
char *str = nullptr;
printf("Checking Packet.\n");
do{
tlen = socket.PacketRecvRAW(f_addr,&str);
if(tlen > 0){
cout<<"Get."<<endl;
if(CheckRawMsg(str, tlen)){
cout<<"Signed Raw Data."<<endl;
ProcessSignedRawMsg(str, tlen);
}
rawstr_in.push_back(str);
}
else{
}
}while (tlen && prm-- > 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<int> &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<int> &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<void *>();
tcpur.args_out = new vector<void *>();
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 */

193
socket.cpp Normal file
View File

@ -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";
}

44
type.h Normal file
View File

@ -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 <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <vector>
#include <list>
#include <string>
#include <map>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/time.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.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;
#endif /* type_h */