This commit is contained in:
Saturneic 2019-01-13 20:48:58 +08:00
parent 092d6fe6c7
commit 570dc42bdb
5 changed files with 142 additions and 28 deletions

View File

@ -7,7 +7,8 @@
objects = {
/* Begin PBXBuildFile section */
9221D9EF21EA5142007310A7 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9221D9EE21EA5142007310A7 /* main.cpp */; };
9221DA1121EB5FB8007310A7 /* net.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9221DA1021EB5FB8007310A7 /* net.cpp */; };
9221DA1521EB6469007310A7 /* process.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9221DA1221EB5FC1007310A7 /* process.cpp */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
@ -24,7 +25,10 @@
/* Begin PBXFileReference section */
9221D9EB21EA5142007310A7 /* Net */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Net; sourceTree = BUILT_PRODUCTS_DIR; };
9221D9EE21EA5142007310A7 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
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>"; };
9221DA1221EB5FC1007310A7 /* process.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = process.cpp; sourceTree = "<group>"; };
9221DA1421EB62F6007310A7 /* cpart.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cpart.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -41,7 +45,7 @@
9221D9E221EA5142007310A7 = {
isa = PBXGroup;
children = (
9221D9ED21EA5142007310A7 /* Net */,
9221DA0E21EB5FAD007310A7 /* Net */,
9221D9EC21EA5142007310A7 /* Products */,
);
sourceTree = "<group>";
@ -54,12 +58,15 @@
name = Products;
sourceTree = "<group>";
};
9221D9ED21EA5142007310A7 /* Net */ = {
9221DA0E21EB5FAD007310A7 /* Net */ = {
isa = PBXGroup;
children = (
9221D9EE21EA5142007310A7 /* main.cpp */,
9221DA1221EB5FC1007310A7 /* process.cpp */,
9221DA1021EB5FB8007310A7 /* net.cpp */,
9221DA0F21EB5FB8007310A7 /* net.h */,
9221DA1421EB62F6007310A7 /* cpart.h */,
);
path = Net;
name = Net;
sourceTree = "<group>";
};
/* End PBXGroup section */
@ -118,7 +125,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
9221D9EF21EA5142007310A7 /* main.cpp in Sources */,
9221DA1121EB5FB8007310A7 /* net.cpp in Sources */,
9221DA1521EB6469007310A7 /* process.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

96
cpart.h Normal file
View File

@ -0,0 +1,96 @@
//
// cpart.h
// Net
//
// Created by 胡一兵 on 2019/1/13.
// Copyright © 2019年 Bakantu. All rights reserved.
//
#ifndef cpart_h
#define cpart_h
#include <dlfcn.h>
#include <string>
#include <vector>
using std::vector;
using std::string;
#define PCSFUNC_DEFINE(name) extern "C" int name(vector<void *> args_in,vector<void *> *args_out)
#define GET_ARGS(type) CPart::popArg<type>(args_in)
#define ADD_ARGS(type,value) CPart::addArg<type>(args_in, value);
#define SUCCESS 0
#define FAIL 1
typedef int(*PCSFUNC)(vector<void *>,vector<void *> *);
class CPart{
public:
vector<int> fargs_in, fargs_out;
vector<void *> args_in, args_out;
int (*func)(vector<void *>,vector<void *> *);
void *handle;
string src_path,name,libname;
CPart(PCSFUNC func):func(func),handle(nullptr){
}
CPart(string src_path,string name){
this->src_path = src_path;
this->name = name;
system(("g++ -fPIC -shared -std=c++11 -o lib"+name+".so "+src_path).data());
this->libname = "lib"+name+".so";
this->handle = dlopen(this->libname.data(), RTLD_LAZY | RTLD_GLOBAL);
this->func = (PCSFUNC) dlsym(this->handle, this->name.data());
}
CPart(string name){
this->name = name;
this->libname = "lib"+name+".so";
this->handle = dlopen(this->libname.data(), RTLD_LAZY | RTLD_GLOBAL);
this->func = (PCSFUNC) dlsym(this->handle, this->name.data());
}
~CPart(){
for(auto k = 0; k < args_in.size(); k++){
if(fargs_in[k] == 0) delete (int *)(args_in[k]);
else delete (double *)(args_in[k]);
}
for(auto k = 0; k < args_out.size(); k++){
if(fargs_in[k] == 0) delete (int *)(args_out[k]);
else delete (double *)(args_out[k]);
}
if(handle != nullptr)
dlclose(handle);
}
void setArgsType(vector<int> fargs_in, vector<int> fargs_out){
this->fargs_in = fargs_in;
this->fargs_out = fargs_out;
}
void addArgsInt(int value){
int *p_value = new int(value);
*p_value = value;
args_in.push_back(p_value);
}
int Run(void){
return func(args_in,&args_out);
}
template<class T>
static void addArg(vector<void *> &args,T value){
T *p_value = new T(value);
*p_value = value;
args.push_back(p_value);
}
template<class T>
static T popArg(vector<void *> &args){
T *p_value = (T *)args.back();
args.pop_back();
return *p_value;
}
};
#endif /* cpart_h */

22
net.cpp
View File

@ -6,21 +6,19 @@
// Copyright © 2019年 Bakantu. All rights reserved.
//
#include "net.hpp"
#include "net.h"
#include "cpart.h"
void process(void *arg){
}
int main(void){
char ip[] = "127.0.0.1";
Socket server("127.0.0.1",9048,true,false);
printf("Start to listen\n");
Addr t_addr;
string str = server.PacketRecv(t_addr);
if(str == "request token"){
}
CPart ncp("process");
ncp.setArgsType({0,0}, {0});
ncp.addArgsInt(2);
ncp.addArgsInt(5);
ncp.Run();
return 0;
}

View File

@ -10,7 +10,9 @@
#define net_hpp
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <sys/types.h>
@ -21,6 +23,7 @@
#include <netdb.h>
using std::string;
using std::vector;
class Addr{
public:
@ -162,14 +165,6 @@ public:
}
};
class UProcess{
public:
Addr addr;
int token;
UProcess(Addr t_addr, int token){
addr = t_addr;
this->token = token;
}
};
#endif /* net_hpp */

17
process.cpp Normal file
View File

@ -0,0 +1,17 @@
//
// process.cpp
// Net
//
// Created by 胡一兵 on 2019/1/13.
// Copyright © 2019年 Bakantu. All rights reserved.
//
#include "cpart.h"
PCSFUNC_DEFINE(process){
int a = GET_ARGS(int), b = GET_ARGS(int);
printf("%d\n",a+b);
ADD_ARGS(int, a+b);
return 0;
}