Add and fix.
This commit is contained in:
parent
cb4c1ac1d4
commit
092d6fe6c7
13
net.cpp
13
net.cpp
@ -8,16 +8,19 @@
|
||||
|
||||
#include "net.hpp"
|
||||
|
||||
void process(class Socket &server,int nsfd,struct sockaddr_in c_addr){
|
||||
server.Send(nsfd, "ORDER");
|
||||
void process(void *arg){
|
||||
|
||||
}
|
||||
|
||||
int main(void){
|
||||
char ip[] = "127.0.0.1";
|
||||
Socket server("127.0.0.1",9048,true);
|
||||
server.Listen(10,process);
|
||||
Socket server("127.0.0.1",9048,true,false);
|
||||
printf("Start to listen\n");
|
||||
server.Accept();
|
||||
Addr t_addr;
|
||||
string str = server.PacketRecv(t_addr);
|
||||
if(str == "request token"){
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
139
net.hpp
139
net.hpp
@ -10,6 +10,7 @@
|
||||
#define net_hpp
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
|
||||
#include <sys/types.h>
|
||||
@ -21,33 +22,77 @@
|
||||
|
||||
using std::string;
|
||||
|
||||
class Addr{
|
||||
public:
|
||||
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 setSize(void){
|
||||
len = sizeof(address);
|
||||
}
|
||||
struct sockaddr *obj(void){
|
||||
return (struct sockaddr *) &address;
|
||||
}
|
||||
};
|
||||
|
||||
class Socket{
|
||||
public:
|
||||
struct sockaddr_in c_addr, addr;
|
||||
struct sockaddr_in c_addr;
|
||||
Addr addr;
|
||||
int nsfd,sfd,port;
|
||||
bool server;
|
||||
void (*func)(class Socket &,int ,struct sockaddr_in);
|
||||
bool server,tcp,ipv4;
|
||||
void (*func)(class Socket &,int ,Addr);
|
||||
Socket(string ip_addr, int port, bool server = false, bool tcp = true, bool ipv4 = true){
|
||||
memset(&addr, 0, sizeof(struct sockaddr_in));
|
||||
if(ipv4)
|
||||
addr.sin_family = AF_INET;
|
||||
addr.address.sin_family = AF_INET;
|
||||
else
|
||||
addr.sin_family = AF_INET6;
|
||||
addr.sin_port = htons(port);
|
||||
addr.address.sin_family = AF_INET6;
|
||||
addr.address.sin_port = htons(port);
|
||||
this->port = port;
|
||||
addr.sin_addr.s_addr = inet_addr(ip_addr.data());
|
||||
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,SOCK_STREAM,0);
|
||||
else sfd = socket(AF_INET6,SOCK_STREAM,0);
|
||||
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, (struct sockaddr *) &addr, sizeof(struct sockaddr))) throw "fail to bind";
|
||||
if(!~bind(sfd, addr.obj(), addr.size())) throw "fail to bind";
|
||||
}
|
||||
else{
|
||||
if(ipv4) sfd = socket(PF_INET,SOCK_STREAM,0);
|
||||
else sfd = socket(PF_INET6,SOCK_STREAM,0);
|
||||
if(!~connect(sfd,(struct sockaddr *)&addr,sizeof(struct sockaddr)))
|
||||
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";
|
||||
|
||||
}
|
||||
@ -57,41 +102,73 @@ public:
|
||||
close(sfd);
|
||||
}
|
||||
|
||||
void Listen(int connection, void (*func)(class Socket &,int ,struct sockaddr_in) = NULL){
|
||||
if(server){
|
||||
void Listen(int connection, void (*func)(class Socket &,int ,Addr) = NULL){
|
||||
if(server && tcp){
|
||||
listen(sfd, 10);
|
||||
this->func = func;
|
||||
}
|
||||
}
|
||||
|
||||
void Accept(void){
|
||||
if(server){
|
||||
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,c_addr);
|
||||
if(func != NULL) func(*this,nsfd,addr);
|
||||
close(nsfd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Send(int t_nsfd, string buff){
|
||||
ssize_t len = send(t_nsfd,buff.data(),buff.size(),0);
|
||||
if(len != buff.size()) throw "size unmatch";
|
||||
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());
|
||||
}
|
||||
|
||||
string PacketRecv(Addr t_addr){
|
||||
if(!tcp){
|
||||
char buff[BUFSIZ];
|
||||
ssize_t tlen = recvfrom(sfd, (void *)buff, BUFSIZ, 0, t_addr.obj(), t_addr.sizep());
|
||||
if(tlen > 0){
|
||||
buff[tlen] = '\0';
|
||||
string str = buff;
|
||||
return str;
|
||||
}
|
||||
else throw "packet receive fail";
|
||||
}
|
||||
throw "connection is tcp";
|
||||
}
|
||||
|
||||
string Recv(int t_nsfd){
|
||||
char buff[BUFSIZ];
|
||||
ssize_t len=recv(t_nsfd,buff,BUFSIZ,0);
|
||||
if(len > 0){
|
||||
buff[len] = '\0';
|
||||
string str = buff;
|
||||
return str;
|
||||
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";
|
||||
}
|
||||
else throw "receive fail";
|
||||
|
||||
throw "connection is udp";
|
||||
}
|
||||
};
|
||||
|
||||
class UProcess{
|
||||
public:
|
||||
Addr addr;
|
||||
int token;
|
||||
UProcess(Addr t_addr, int token){
|
||||
addr = t_addr;
|
||||
this->token = token;
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user