chiark / gitweb /
dgram sock references fixed. can close
authorian <ian>
Sat, 7 Sep 2002 01:12:30 +0000 (01:12 +0000)
committerian <ian>
Sat, 7 Sep 2002 01:12:30 +0000 (01:12 +0000)
base/tables-examples.tct
dgram/dgram.c

index 581433499be1f53c54dc1b7c0189a3f1a83a7dac..6f8755a8495d88d182bb358081c80372660458d0 100644 (file)
@@ -89,5 +89,7 @@ Table dgram_socket DgramSocket_SubCommand
        create
                local   sockaddr
                =>      sockid
+       close
+               sock    sockid
 #      transmit
 #              
index ecdd2f7355173ca677f860630a12004b4bac6777..e6df127ab79bae33afa345d9268193f17f70c7eb 100644 (file)
 #include "tables.h"
 #include "hbytes.h"
 
+typedef struct DgramSocket {
+  int fd;
+} DgramSocket;
+
+static int n_socks;
+static DgramSocket *socks;
+
 static int sockfail(Tcl_Interp *ip, int fd, const char *m) {
   int e;
   e= errno;
@@ -19,10 +26,19 @@ static int sockfail(Tcl_Interp *ip, int fd, const char *m) {
 }
 
 int do_dgram_socket_create(ClientData cd, Tcl_Interp *ip,
-                         SockAddr_Value local, int *result) {
-  int fd, al, r;
+                         SockAddr_Value local, int *sock_r) {
+  int fd, al, r, sock;
   const struct sockaddr *sa;
 
+  for (sock=0; sock<n_socks && socks[sock].fd>=0; sock++);
+  if (sock>=n_socks) {
+    n_socks += 2;
+    n_socks *= 2;
+    socks= (void*)Tcl_Realloc((void*)socks, n_socks*sizeof(*socks));
+    while (sock<n_socks) socks[sock++].fd=-1;
+    sock--;
+  }
+
   sa= sockaddr_addr(&local);
   al= sockaddr_len(&local);
 
@@ -30,19 +46,31 @@ int do_dgram_socket_create(ClientData cd, Tcl_Interp *ip,
   if (fd<0) return posixerr(ip,errno,"socket");
   r= bind(fd, sa, al);  if (r) return sockfail(ip,fd,"bind");
   r= setnonblock(fd, 1);  if (r) return sockfail(ip,fd,"setnonblock");
-  *result= fd;
+
+  socks[sock].fd= fd;
+  *sock_r= sock;
+  return TCL_OK;
+}
+
+int do_dgram_socket_close(ClientData cd, Tcl_Interp *ip, int sock) {
+  close(socks[sock].fd); /* nothing useful to be done with errors */
+  socks[sock].fd= -1;
   return TCL_OK;
 }
 
 /* Arg parsing */
 
 int pat_sockid(Tcl_Interp *ip, Tcl_Obj *o, int *val) {
-  int rc;
+  int rc, sock;
   
   rc= Tcl_ConvertToType(ip,o,&sockid_type);
   if (rc) return rc;
 
-  *val= o->internalRep.longValue;
+  sock= o->internalRep.longValue;
+  if (sock >= n_socks || socks[sock].fd==-1)
+    return staticerr(ip,"dgram socket not open");
+
+  *val= sock;
   return TCL_OK;
 }