introduced special network return codes, added {de,}select, connect, close, set functions
authorhackbard <hackbard>
Sun, 9 May 2004 16:24:33 +0000 (16:24 +0000)
committerhackbard <hackbard>
Sun, 9 May 2004 16:24:33 +0000 (16:24 +0000)
src/network.c
src/network.h

index 66314f9..3293a57 100644 (file)
@@ -88,7 +88,7 @@ int network_manage_connection(t_net *net) {
       if(net->connection[i].status&C_HANGUP) {
         if(close(net->connection[i].fd)==-1) {
           perror("[network] close call");
-          return N_ERROR;
+          return N_E_CLOSE;
         }
         printf("[network] connection %d closed\n",i);
         net->connection[i].status=0;
@@ -118,11 +118,12 @@ int network_manage_connection(t_net *net) {
           if(connect(net->connection[i].fd,(struct sockaddr *)&addr,
                      sizeof(struct sockaddr))==-1) {
             perror("[network] connect call");
-            return N_ERROR;
+            return N_E_CONNECT;
           }
 
-          printf("[network] established connection to %s port %d on channel %d\n",
-                 net->connection[i].ip,net->connection[i].port,i);
+          printf("[network] established connection to ");
+          printf("%s port %d on channel %d\n",net->connection[i].ip,
+                                              net->connection[i].port,i);
           net->connection[i].status|=C_ESTABL;
 
         }
@@ -136,6 +137,70 @@ int network_manage_connection(t_net *net) {
   return N_SUCCESS;
 }
 
+int network_connect(t_net *net,int channel) {
+
+  if(net->connection[channel].status&C_IN_USE) {
+    printf("[network] connect failed, channel %02d in use\n",channel);
+    return N_E_IN_USE;
+  }
+  if(!(net->connection[channel].status&C_INFO_A)) {
+    printf("[network] connect failed, missing configuration for channel %02d",
+           channel);
+    return N_E_NO_INFO;
+  }
+
+  /* try connect & return result */
+  net->connection[channel].status|=C_IN_USE;
+  return(network_manage_connection(net)); /* could be other channel too */
+}
+
+int network_close(t_net *net,int channel) {
+
+  if(!(net->connection[channel].status&C_ESTABL)) {
+    printf("[network] close failed, channel %02d not active",channel);
+    return N_E_NC;
+  }
+  
+  net->connection[channel].status|=C_HANGUP;
+  return(network_manage_connection(net)); /* could be other channel too */
+}
+
+int network_set_connection_info(t_net *net,int channel,char *ip,int port) {
+
+  if(net->connection[channel].status&C_IN_USE) {
+    printf("[network] set connection failed, channel %02d in use\n",channel);
+    return N_E_IN_USE;
+  }
+
+  strncpy(net->connection[channel].ip,ip,IP_DIGITS);
+  net->connection[channel].port=port;
+  net->connection[channel].status|=C_INFO_A;
+
+  return N_SUCCESS;
+}
+
+int network_select(t_net *net,int channel) {
+
+  int mask;
+
+  if(channel==MAX_CONNECTIONS) mask=0xffffffff;
+  else mask=(1<<channel);
+  net->sendmask|=mask;
+
+  return N_SUCCESS;
+}
+
+int network_deselect(t_net *net,int channel) {
+
+  int mask;
+
+  if(channel==MAX_CONNECTIONS) mask=0;
+  else mask=~(1<<channel);
+  net->sendmask&=mask;
+
+  return N_SUCCESS;
+}
+
 int network_manage_incoming(t_net *net) {
 
   int channel;
@@ -148,7 +213,7 @@ int network_manage_incoming(t_net *net) {
                                        (struct sockaddr *)&addr,
                                        &len))==-1) {
         perror("[network] accept call");
-        return N_ERROR;
+        return N_E_ACCEPT;
       }
       strncpy(net->connection[channel].ip,inet_ntoa(addr.sin_addr),IP_DIGITS);
       net->connection[channel].port=ntohs(addr.sin_port);
@@ -160,7 +225,7 @@ int network_manage_incoming(t_net *net) {
   }
 
   puts("[network] maximum connections reached");
-  return N_ERROR;
+  return N_E_MAXC;
 }
  
 int network_send(int fd,unsigned char *data,int datasize) {
index 31ca335..14168bf 100644 (file)
 
 #define N_SUCCESS 1
 #define N_ERROR -1
+#define N_E_IN_USE -2
+#define N_E_NO_INFO -3
+#define N_E_CLOSE -4
+#define N_E_CONNECT -5
+#define N_E_NC -6
+#define N_E_ACCEPT -7
+#define N_E_MAXC -8
 
 #define MAX_LISTEN_QUEUE 32
 
@@ -56,6 +63,11 @@ int network_init(t_net *net);
 int network_shutdown(t_net *net);
 int network_set_listen_port(t_net *net,in_port_t port);
 int network_manage_connection(t_net *net);
+int network_connect(t_net *net,int channel);
+int network_close(t_net *net,int channel);
+int network_set_connection_info(t_net *net,int channel,char *ip,int port);
+int network_select(t_net *net,int channel);
+int network_deselect(t_net *net,int channel);
 int network_manage_incoming(t_net *net);
 int network_send(int fd,unsigned char *data,int datasize);
 int network_receive(int fd,unsigned char *data,int datasize);