Changeset 538


Ignore:
Timestamp:
02/07/09 15:41:55 (3 years ago)
Author:
mooneer
Message:

Added socket|eof and removed timeouts from readline and read() (credit: Michael Edgar)

Location:
interpreter/trunk/modules
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • interpreter/trunk/modules/System/network/socket.c

    r521 r538  
    175175    kite_vm_return(thd, rval); 
    176176} 
     177/************************************************************************* 
     178 * Check if the socket has data. 
     179****************************************************************************/ 
     180KITE_CLASS_METHOD(Socket_eof) { 
     181  kite_object_t *timeout; 
     182  struct timeval timeout_struct; 
     183  fd_set socklist; 
     184   
     185  int can_read; 
     186 
     187  KITE_GET_METHOD_ARGUMENT(timeout, 1); 
     188  if (timeout == NULL || timeout->type == OBJ_NULL) { 
     189    timeout_struct.tv_sec = 1; 
     190    timeout_struct.tv_usec = 0; 
     191  } 
     192  else 
     193  { 
     194    timeout_struct.tv_sec = KITE_GET_INTEGER(timeout) / 1000; 
     195    timeout_struct.tv_usec = (float)(KITE_GET_INTEGER(timeout) % 1000) / 1000.0f; 
     196  } 
     197   
     198  CHECK_SOCKET_OPEN; 
     199   
     200  FD_ZERO(&socklist); 
     201  FD_SET(KITE_GET_INTEGER(this),&socklist); 
     202  can_read = select(FD_SETSIZE,&socklist,NULL,NULL,&timeout_struct); 
     203  if (can_read <= 0) { 
     204    kite_vm_return(thd, kite_new_boolean(thd,TRUE)); 
     205    return; 
     206  } 
     207  else { 
     208    kite_vm_return(thd, kite_new_boolean(thd,FALSE)); 
     209    return; 
     210  } 
     211}    
    177212/***************************************************************************** 
    178213 * Read up to the given number of bytes 
     
    183218    kite_object_t *rval; 
    184219    char *buf = calloc(bytes->builtin_data.intvalue + 1, 1); 
    185     int ret, can_read; 
    186     struct timeval timeout; 
    187     fd_set socklist; 
     220    int ret; 
    188221     
    189222    CHECK_SOCKET_OPEN; 
    190      
    191     timeout.tv_sec = 1; 
    192     timeout.tv_usec = 0; 
    193      
    194     FD_ZERO(&socklist); 
    195     FD_SET(KITE_GET_INTEGER(this),&socklist); 
    196     can_read = select(FD_SETSIZE,&socklist,NULL,NULL,&timeout); 
    197     if (can_read <= 0) { 
    198       free(buf); 
    199       kite_vm_return(thd, kite_new_null(thd)); 
    200       return; 
    201     } 
     223 
    202224    ret = read(KITE_GET_INTEGER(this), buf, KITE_GET_INTEGER(bytes)); 
    203225    if (ret < 0 && errno != EAGAIN) { 
     
    227249    int ret, idx = 0; 
    228250    kite_object_t *rval; 
    229     int can_read; 
    230     struct timeval timeout; 
    231     fd_set socklist; 
     251    // int can_read; 
     252    // struct timeval timeout; 
     253    // fd_set socklist; 
    232254 
    233255    CHECK_SOCKET_OPEN; 
     
    235257 
    236258    buf = calloc(1, 1); 
    237      
    238     timeout.tv_sec = 1; 
    239     timeout.tv_usec = 0; 
    240      
    241     FD_ZERO(&socklist); 
    242     FD_SET(KITE_GET_INTEGER(this),&socklist); 
    243     can_read = select(FD_SETSIZE,&socklist,NULL,NULL,&timeout); 
    244     if (can_read <= 0) { 
    245       free(buf); 
    246       kite_vm_return(thd, kite_new_null(thd)); 
    247       return; 
    248     } 
    249      
     259 
    250260    do { 
    251261        ret = read(KITE_GET_INTEGER(this), &buf[idx], 1); 
     
    554564                                  "Listen for connections on given socket.", 1, 
    555565                                  "queue", "The size of the listen queue.")); 
     566    kite_add_method(thread, newclass, "eof",  
     567        kite_new_method_compiled_with_docs(thread, Socket_eof,  
     568                                  "Checks if the socket has any more data to read. 1 second timeout.", 0)); 
     569    kite_add_method(thread, newclass, "eof",  
     570      kite_new_method_compiled_with_docs(thread, Socket_eof,  
     571                                "Checks if the socket has any more data to read. Timeout in parameter.", 1, 
     572                                "timeout","How long (in milliseconds) to wait before timing out and returning TRUE."));                                                                     
    556573    kite_add_method(thread, newclass, "read",  
    557574        kite_new_method_compiled_with_docs(thread, Socket_read,  
  • interpreter/trunk/modules/all_modules.kt

    r522 r538  
    3030import "System.boolean"; 
    3131import "System.collections"; 
     32import "System.collections.array"; 
    3233import "System.date"; 
    3334import "System.date.hires"; 
Note: See TracChangeset for help on using the changeset viewer.