projects
/
my-code
/
api.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
minor fixes + upgrades
[my-code/api.git]
/
network
/
network.c
diff --git
a/network/network.c
b/network/network.c
index
1160e7c
..
1cdd38c
100644
(file)
--- a/
network/network.c
+++ b/
network/network.c
@@
-7,17
+7,22
@@
#include "network.h"
int network_init(t_net *net,int outfd) {
#include "network.h"
int network_init(t_net *net,int outfd) {
-
- struct sockaddr_in addr;
- int true;
-
- dprintf(outfd,"[network] initializing network ...\n");
+
+ dprintf(net->outfd,"[network] initializing network api ...\n");
memset(net->connection,0,MAX_CONNECTIONS*sizeof(t_connection));
net->c_count=0;
net->sendmask=0;
net->outfd=outfd;
memset(net->connection,0,MAX_CONNECTIONS*sizeof(t_connection));
net->c_count=0;
net->sendmask=0;
net->outfd=outfd;
+ return N_SUCCESS;
+}
+
+int network_listen(t_net *net) {
+
+ struct sockaddr_in addr;
+ int true;
+
if((net->l_fd=socket(AF_INET,SOCK_STREAM,0))==-1) {
perror("[network] socket call");
return N_ERROR;
if((net->l_fd=socket(AF_INET,SOCK_STREAM,0))==-1) {
perror("[network] socket call");
return N_ERROR;
@@
-36,7
+41,7
@@
int network_init(t_net *net,int outfd) {
perror("[network] setsockopt call");
return N_ERROR;
}
perror("[network] setsockopt call");
return N_ERROR;
}
- dprintf(
even
t->outfd,"[network] reused address\n");
+ dprintf(
ne
t->outfd,"[network] reused address\n");
}
if(listen(net->l_fd,MAX_LISTEN_QUEUE)==-1) {
}
if(listen(net->l_fd,MAX_LISTEN_QUEUE)==-1) {
@@
-44,7
+49,7
@@
int network_init(t_net *net,int outfd) {
return N_ERROR;
}
return N_ERROR;
}
- dprintf(
even
t->outfd,"[network] listen on %s port %d\n",
+ dprintf(
ne
t->outfd,"[network] listen on %s port %d\n",
inet_ntoa(addr.sin_addr),net->l_port);
return N_SUCCESS;
inet_ntoa(addr.sin_addr),net->l_port);
return N_SUCCESS;
@@
-63,7
+68,7
@@
int network_shutdown(t_net *net) {
return N_ERROR;
}
return N_ERROR;
}
- dprintf(
even
t->outfd,"[network] shutdown\n");
+ dprintf(
ne
t->outfd,"[network] shutdown\n");
return N_SUCCESS;
}
return N_SUCCESS;
}
@@
-89,7
+94,7
@@
int network_manage_connection(t_net *net) {
perror("[network] close call");
return N_E_CLOSE;
}
perror("[network] close call");
return N_E_CLOSE;
}
- dprintf(
even
t->outfd,"[network] connection %d closed\n",i);
+ dprintf(
ne
t->outfd,"[network] connection %d closed\n",i);
net->connection[i].status=0;
}
net->connection[i].status=0;
}
@@
-120,8
+125,8
@@
int network_manage_connection(t_net *net) {
return N_E_CONNECT;
}
return N_E_CONNECT;
}
- dprintf(
even
t->outfd,"[network] established connection to ");
- dprintf(
even
t->outfd,"%s port %d on channel %d\n",
+ dprintf(
ne
t->outfd,"[network] established connection to ");
+ dprintf(
ne
t->outfd,"%s port %d on channel %d\n",
net->connection[i].ip,net->connection[i].port,i);
net->connection[i].status|=C_ESTABL;
net->connection[i].ip,net->connection[i].port,i);
net->connection[i].status|=C_ESTABL;
@@
-139,12
+144,12
@@
int network_manage_connection(t_net *net) {
int network_connect(t_net *net,int channel) {
if(net->connection[channel].status&C_IN_USE) {
int network_connect(t_net *net,int channel) {
if(net->connection[channel].status&C_IN_USE) {
- dprintf(
even
t->outfd,"[network] connect failed, channel %02d in use\n",
+ dprintf(
ne
t->outfd,"[network] connect failed, channel %02d in use\n",
channel);
return N_E_IN_USE;
}
if(!(net->connection[channel].status&C_INFO_A)) {
channel);
return N_E_IN_USE;
}
if(!(net->connection[channel].status&C_INFO_A)) {
- dprintf(
even
t->outfd,
+ dprintf(
ne
t->outfd,
"[network] connect failed, missing config for chan %02d\n",channel);
return N_E_NO_INFO;
}
"[network] connect failed, missing config for chan %02d\n",channel);
return N_E_NO_INFO;
}
@@
-157,7
+162,7
@@
int network_connect(t_net *net,int channel) {
int network_close(t_net *net,int channel) {
if(!(net->connection[channel].status&C_ESTABL)) {
int network_close(t_net *net,int channel) {
if(!(net->connection[channel].status&C_ESTABL)) {
- dprintf(
even
t->outfd,"[network] close failed, channel %02d not active\n",
+ dprintf(
ne
t->outfd,"[network] close failed, channel %02d not active\n",
channel);
return N_E_NC;
}
channel);
return N_E_NC;
}
@@
-179,7
+184,7
@@
int network_close_all(t_net *net) {
int network_set_connection_info(t_net *net,int channel,char *ip,int port) {
if(net->connection[channel].status&C_IN_USE) {
int network_set_connection_info(t_net *net,int channel,char *ip,int port) {
if(net->connection[channel].status&C_IN_USE) {
- dprintf(
even
t->outfd,
+ dprintf(
ne
t->outfd,
"[network] set connection failed, channel %02d in use\n",channel);
return N_E_IN_USE;
}
"[network] set connection failed, channel %02d in use\n",channel);
return N_E_IN_USE;
}
@@
-230,7
+235,7
@@
int network_manage_incoming(t_net *net) {
strncpy(net->connection[channel].ip,inet_ntoa(addr.sin_addr),IP_DIGITS);
net->connection[channel].port=ntohs(addr.sin_port);
net->connection[channel].status=C_IN_USE|C_INFO_A|C_SOCKET|C_ESTABL;
strncpy(net->connection[channel].ip,inet_ntoa(addr.sin_addr),IP_DIGITS);
net->connection[channel].port=ntohs(addr.sin_port);
net->connection[channel].status=C_IN_USE|C_INFO_A|C_SOCKET|C_ESTABL;
- dprintf(
even
t->outfd,
+ dprintf(
ne
t->outfd,
"[network] established connection from %s:%d, channel %d\n",
net->connection[channel].ip,net->connection[channel].port,
channel);
"[network] established connection from %s:%d, channel %d\n",
net->connection[channel].ip,net->connection[channel].port,
channel);
@@
-238,7
+243,7
@@
int network_manage_incoming(t_net *net) {
}
}
}
}
- dprintf(
even
t->outfd,"[network] maximum connections reached\n");
+ dprintf(
ne
t->outfd,"[network] maximum connections reached\n");
return N_E_MAXC;
}
return N_E_MAXC;
}
@@
-260,16
+265,32
@@
int network_send(int fd,unsigned char *data,int datasize) {
return N_SUCCESS;
}
return N_SUCCESS;
}
+int network_send_chan(t_net *net,int chan,unsigned char *data,int datasize) {
+
+ return(network_send(net->connection[chan].fd,data,datasize));
+}
+
int network_receive(int fd,unsigned char *data,int datasize) {
int network_receive(int fd,unsigned char *data,int datasize) {
- int count;
+ int count
,left
;
- if((count=read(fd,data,datasize))==-1) {
- perror("[network] read call");
- return N_ERROR;
+ count=0;
+ left=datasize;
+
+ while(left) {
+ if((count=read(fd,data,datasize))==-1) {
+ perror("[network] read call");
+ return N_ERROR;
+ }
+ left-=count;
}
}
- return count;
+ return datasize;
+}
+
+int network_receive_chan(t_net *net,int chan,unsigned char *data,int datasize) {
+
+ return(network_receive(net->connection[chan].fd,data,datasize));
}
int network_udp_listen_init(t_net *net) {
}
int network_udp_listen_init(t_net *net) {
@@
-294,10
+315,10
@@
int network_udp_listen_init(t_net *net) {
perror("[network] setsockopt call (udp)");
return N_ERROR;
}
perror("[network] setsockopt call (udp)");
return N_ERROR;
}
- dprintf(
even
t->outfd,"[network] reused address (udp)\n");
+ dprintf(
ne
t->outfd,"[network] reused address (udp)\n");
}
}
- dprintf(
even
t->outfd,"[network] listening on port %d (udp)\n",
+ dprintf(
ne
t->outfd,"[network] listening on port %d (udp)\n",
net->l_udp_port);
if((net->s_udp_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
net->l_udp_port);
if((net->s_udp_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
@@
-320,7
+341,7
@@
int network_udp_receive(t_net *net,int channel, unsigned char *data,int count) {
}
if(strncmp(net->connection[channel].ip,inet_ntoa(addr.sin_addr),IP_DIGITS)) {
}
if(strncmp(net->connection[channel].ip,inet_ntoa(addr.sin_addr),IP_DIGITS)) {
- dprintf(
even
t->outfd,"[network] packet from unknown: %s\n",
+ dprintf(
ne
t->outfd,"[network] packet from unknown: %s\n",
inet_ntoa(addr.sin_addr));
return N_UDP_WRONG_SENDER;
}
inet_ntoa(addr.sin_addr));
return N_UDP_WRONG_SENDER;
}