Changeset 5501

Show
Ignore:
Timestamp:
05/06/08 16:33:20 (1 week ago)
Author:
morris
Message:

Perform SSL handshake in separate thread

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • wired/trunk/wired/commands.c

    r5361 r5501  
    216216 
    217217 
    218 void wd_control_thread(wi_runtime_instance_t *argument) { 
     218void wd_command_loop_for_user(wd_user_t *user) { 
    219219        wi_pool_t                       *pool; 
    220         wd_user_t                       *user = argument; 
    221220        wi_socket_t                     *socket; 
    222221        wi_string_t                     *string; 
     
    225224         
    226225        pool = wi_pool_init(wi_pool_alloc()); 
    227  
    228         wd_users_add_user(user); 
    229         wd_users_set_user_for_thread(user); 
    230226         
    231227        socket = wd_user_socket(user); 
  • wired/trunk/wired/commands.h

    r4508 r5501  
    3232#include <wired/wired.h> 
    3333 
    34 void                                                    wd_control_thread(wi_runtime_instance_t *); 
     34#include "users.h" 
     35 
     36void                                                    wd_command_loop_for_user(wd_user_t *); 
    3537 
    3638#endif /* WD_COMMANDS_H */ 
  • wired/trunk/wired/server.c

    r5408 r5501  
    5656 
    5757static void                                                     wd_control_listen_thread(wi_runtime_instance_t *); 
     58static void                                                     wd_control_accept_thread(wi_runtime_instance_t *); 
    5859static void                                                     wd_transfer_listen_thread(wi_runtime_instance_t *); 
     60static void                                                     wd_transfer_accept_thread(wi_runtime_instance_t *); 
    5961 
    6062 
     
    342344        wi_socket_t                     *socket; 
    343345        wi_address_t            *address; 
     346         
     347        pool = wi_pool_init(wi_pool_alloc()); 
     348 
     349        while(wd_running) { 
     350                wi_pool_drain(pool); 
     351 
     352                socket = wi_socket_accept_multiple(wd_control_sockets, 30.0, &address); 
     353 
     354                if(!address) { 
     355                        wi_log_err(WI_STR("Could not accept a connection: %m")); 
     356                         
     357                        continue; 
     358                } 
     359                 
     360                if(!socket) { 
     361                        wi_log_err(WI_STR("Could not accept a connection for %@: %m"), 
     362                                wi_address_string(address)); 
     363                         
     364                        continue; 
     365                } 
     366                 
     367                if(!wi_thread_create_thread(wd_control_accept_thread, socket)) { 
     368                        wi_log_err(WI_STR("Could not create a thread for %@: %m"), 
     369                                wi_address_string(address)); 
     370                         
     371                        continue; 
     372                } 
     373        } 
     374         
     375        wi_release(pool); 
     376} 
     377 
     378 
     379 
     380static void wd_control_accept_thread(wi_runtime_instance_t *argument) { 
     381        wi_pool_t                       *pool; 
     382        wi_socket_t                     *socket = argument; 
    344383        wi_string_t                     *ip; 
    345384        wd_user_t                       *user; 
    346385         
    347386        pool = wi_pool_init(wi_pool_alloc()); 
    348  
    349         while(wd_running) { 
    350                 wi_pool_drain(pool); 
    351  
    352                 /* accept new user */ 
    353                 socket = wi_socket_accept_multiple(wd_control_sockets, 30.0, &address); 
    354  
    355                 if(!address) { 
    356                         wi_log_err(WI_STR("Could not accept a connection: %m")); 
    357                          
    358                         continue; 
    359                 } 
    360                  
    361                 ip = wi_address_string(address); 
    362                  
    363                 if(!socket) { 
    364                         wi_log_err(WI_STR("Could not accept a connection for %@: %m"), ip); 
    365                          
    366                         continue; 
    367                 } 
    368                  
    369                 if(!wi_socket_accept_tls(socket, wd_control_socket_tls, 30.0)) { 
    370                         wi_log_err(WI_STR("Could not accept a connection for %@: %m"), ip); 
    371                          
    372                         continue; 
    373                 } 
    374                  
    375                 wi_socket_set_direction(socket, WI_SOCKET_READ); 
    376                  
    377                 wi_log_info(WI_STR("Connect from %@"), ip); 
    378                  
    379                 /* spawn a user thread */ 
    380                 user = wd_user_with_socket(socket); 
    381  
    382                 if(!wi_thread_create_thread(wd_control_thread, user)) 
    383                         wi_log_err(WI_STR("Could not create a thread for %@: %m"), ip); 
    384         } 
    385          
     387         
     388        ip = wi_address_string(wi_socket_address(socket)); 
     389         
     390        if(!wi_socket_accept_tls(socket, wd_control_socket_tls, 30.0)) { 
     391                wi_log_err(WI_STR("Could not accept a connection for %@: %m"), ip); 
     392                 
     393                goto end; 
     394        } 
     395         
     396        wi_socket_set_direction(socket, WI_SOCKET_READ); 
     397 
     398        wi_log_info(WI_STR("Connect from %@"), ip); 
     399         
     400        user = wd_user_with_socket(socket); 
     401 
     402        wd_users_add_user(user); 
     403        wd_users_set_user_for_thread(user); 
     404         
     405        wd_command_loop_for_user(user); 
     406 
     407end: 
    386408        wi_release(pool); 
    387409} 
     
    393415        wi_socket_t                     *socket; 
    394416        wi_address_t            *address; 
     417         
     418        pool = wi_pool_init(wi_pool_alloc()); 
     419 
     420        while(wd_running) { 
     421                wi_pool_drain(pool); 
     422 
     423                socket = wi_socket_accept_multiple(wd_transfer_sockets, 30.0, &address); 
     424                 
     425                if(!address) { 
     426                        wi_log_err(WI_STR("Could not accept a connection: %m")); 
     427                         
     428                        continue; 
     429                } 
     430                 
     431                if(!socket) { 
     432                        wi_log_err(WI_STR("Could not accept a connection for %@: %m"), 
     433                                wi_address_string(address)); 
     434                         
     435                        continue; 
     436                } 
     437 
     438                if(!wi_thread_create_thread(wd_transfer_accept_thread, socket)) { 
     439                        wi_log_err(WI_STR("Could not create a thread for %@: %m"), 
     440                                wi_address_string(address)); 
     441                         
     442                        continue; 
     443                } 
     444        } 
     445         
     446        wi_release(pool); 
     447} 
     448 
     449 
     450 
     451static void wd_transfer_accept_thread(wi_runtime_instance_t *argument) { 
     452        wi_pool_t                       *pool; 
     453        wi_socket_t                     *socket = argument; 
    395454        wi_array_t                      *arguments; 
    396455        wi_string_t                     *ip, *string, *command; 
     
    398457         
    399458        pool = wi_pool_init(wi_pool_alloc()); 
    400  
    401         while(wd_running) { 
    402                 wi_pool_drain(pool); 
    403  
    404                 /* accept new connection */ 
    405                 socket = wi_socket_accept_multiple(wd_transfer_sockets, 30.0, &address); 
    406                  
    407                 if(!address) { 
    408                         wi_log_err(WI_STR("Could not accept a connection: %m")); 
    409                          
    410                         continue; 
    411                 } 
    412                  
    413                 ip = wi_address_string(address); 
    414                  
    415                 if(!socket) { 
    416                         wi_log_err(WI_STR("Could not accept a connection for %@: %m"), ip); 
    417                          
    418                         continue; 
    419                 } 
    420  
    421                 if(!wi_socket_accept_tls(socket, wd_transfer_socket_tls, 30.0)) { 
    422                         wi_log_err(WI_STR("Could not accept a connection for %@: %m"), ip); 
    423                          
    424                         continue; 
    425                 } 
    426  
    427                 string = wi_socket_read_to_string(socket, 5.0, WI_STR(WD_MESSAGE_SEPARATOR_STR)); 
    428                  
    429                 if(!string || wi_string_length(string) == 0) { 
    430                         if(!string) 
    431                                 wi_log_warn(WI_STR("Could not read from %@: %m"), ip); 
    432  
    433                         continue; 
    434                 } 
    435                  
    436                 /* parse command */ 
    437                 wi_parse_wired_command(string, &command, &arguments); 
    438                  
    439                 if(wi_is_equal(command, WI_STR("TRANSFER")) && wi_array_count(arguments) >= 1) { 
    440                         /* get transfer by identifier */ 
    441                         transfer = wd_transfers_transfer_with_hash(WI_ARRAY(arguments, 0)); 
    442                          
    443                         if(!transfer) 
    444                                 continue; 
    445                          
    446                         if(!wi_is_equal(ip, wd_user_ip(transfer->user))) 
    447                                 continue; 
    448                          
    449                         transfer->socket = wi_retain(socket); 
    450                          
    451                         /* spawn a transfer thread */ 
    452                         if(!wi_thread_create_thread(wd_transfer_thread, transfer)) 
    453                                 wi_log_err(WI_STR("Could not create a thread for %@: %m"), ip); 
    454                 } 
    455         } 
    456          
     459         
     460        ip = wi_address_string(wi_socket_address(socket)); 
     461         
     462        if(!wi_socket_accept_tls(socket, wd_transfer_socket_tls, 30.0)) { 
     463                wi_log_err(WI_STR("Could not accept a connection for %@: %m"), ip); 
     464                 
     465                goto end; 
     466        } 
     467         
     468        string = wi_socket_read_to_string(socket, 5.0, WI_STR(WD_MESSAGE_SEPARATOR_STR)); 
     469         
     470        if(!string || wi_string_length(string) == 0) { 
     471                if(!string) 
     472                        wi_log_warn(WI_STR("Could not read from %@: %m"), ip); 
     473                 
     474                goto end; 
     475        } 
     476         
     477        wi_parse_wired_command(string, &command, &arguments); 
     478         
     479        if(wi_is_equal(command, WI_STR("TRANSFER")) && wi_array_count(arguments) >= 1) { 
     480                transfer = wd_transfers_transfer_with_hash(WI_ARRAY(arguments, 0)); 
     481                 
     482                if(!transfer) 
     483                        goto end; 
     484                 
     485                if(!wi_is_equal(ip, wd_user_ip(transfer->user))) 
     486                        goto end; 
     487                 
     488                transfer->socket = wi_retain(socket); 
     489                 
     490                wd_transfer_loop(transfer); 
     491        } 
     492 
     493end: 
    457494        wi_release(pool); 
    458495} 
  • wired/trunk/wired/transfers.c

    r5361 r5501  
    555555#pragma mark - 
    556556 
    557 void wd_transfer_thread(wi_runtime_instance_t *argument) { 
    558         wi_pool_t                       *pool; 
    559         wd_transfer_t           *transfer = argument; 
    560          
    561         pool = wi_pool_init(wi_pool_alloc()); 
    562          
     557void wd_transfer_loop(wd_transfer_t *transfer) { 
    563558        wi_timer_invalidate(transfer->timer); 
    564559        wd_transfer_set_state(transfer, WD_TRANSFER_RUNNING); 
     
    578573 
    579574        wi_lock_unlock(wd_transfers_update_queue_lock); 
    580          
    581         wi_release(pool); 
    582575} 
    583576 
  • wired/trunk/wired/transfers.h

    r5303 r5501  
    8989wd_transfer_t *                                                 wd_transfers_transfer_with_hash(wi_string_t *); 
    9090 
    91 void                                                                    wd_transfer_thread(wi_runtime_instance_t *); 
     91void                                                                    wd_transfer_loop(wd_transfer_t *); 
    9292 
    9393