Changeset 414

Show
Ignore:
Timestamp:
07/13/07 19:52:55 (1 year ago)
Author:
blackhedd
Message:

Added EventMachine::Connection#send_file_data, contributed by Kirk Haines.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • version_0/ext/cmain.cpp

    r384 r414  
    356356 
    357357 
    358  
    359  
    360  
    361  
     358/********************************* 
     359evma_send_file_data_to_connection 
     360*********************************/ 
     361 
     362extern "C" int evma_send_file_data_to_connection (const char *binding, const char *filename) 
     363
     364        /* This is a sugaring over send_data_to_connection that reads a file into a 
     365         * locally-allocated buffer, and sends the file data to the remote peer. 
     366         * Return the number of bytes written to the caller. 
     367         * TODO, needs to impose a limit on the file size. This is intended only for 
     368         * small files. (I don't know, maybe 8K or less.) For larger files, use interleaved 
     369         * I/O to avoid slowing the rest of the system down. 
     370         * TODO: we should return a code rather than barf, in case of file-not-found. 
     371         * TODO, does this compile on Windows? 
     372         * TODO, given that we want this to work only with small files, how about allocating 
     373         * the buffer on the stack rather than the heap? 
     374        /* Contributed by Kirk Haines. 
     375         */ 
     376 
     377        char *data; 
     378 
     379        if (!EventMachine) 
     380                throw std::runtime_error("not initialized"); 
     381 
     382        int Fd = open (filename, O_RDONLY); 
     383        if (Fd < 0) 
     384                throw runtime_error (strerror (errno)); 
     385 
     386        struct stat st; 
     387        if (fstat (Fd, &st)) 
     388                throw runtime_error (strerror (errno)); 
     389 
     390        int filesize = st.st_size; 
     391        if (filesize <= 0) { 
     392                close (Fd); 
     393                return 0; 
     394        } 
     395 
     396        data = (char*) malloc (filesize); 
     397        if (!data) 
     398                throw runtime_error ("No allocation"); 
     399        int r = read (Fd, data, filesize); 
     400        if (r < filesize) 
     401                throw runtime_error (strerror (errno)); 
     402        int rd = evma_send_data_to_connection (binding, data, r); 
     403        close (Fd); 
     404        free (data); 
     405 
     406        return rd; 
     407
     408 
     409 
     410 
     411 
  • version_0/ext/eventmachine.h

    r384 r414  
    5151        int evma_set_comm_inactivity_timeout (const char *binding, /*in,out*/int *value); 
    5252        int evma_get_outbound_data_size (const char *binding); 
     53        int evma_send_file_data_to_connection (const char *binding, const char *filename); 
    5354 
    5455        void evma_close_connection (const char *binding, int after_writing); 
  • version_0/ext/rubymain.cpp

    r384 r414  
    361361        evma__epoll(); 
    362362        return Qnil; 
     363} 
     364 
     365 
     366/**************** 
     367t_send_file_data 
     368****************/ 
     369 
     370static VALUE t_send_file_data (VALUE self, VALUE signature, VALUE filename) 
     371{ 
     372        int b = evma_send_file_data_to_connection (StringValuePtr(signature), StringValuePtr(filename)); 
     373        return INT2NUM (b); 
    363374} 
    364375 
     
    425436        rb_define_module_function (EmModule, "setuid_string", (VALUE(*)(...))t_setuid_string, 1); 
    426437        rb_define_module_function (EmModule, "invoke_popen", (VALUE(*)(...))t_invoke_popen, 1); 
     438        rb_define_module_function (EmModule, "send_file_data", (VALUE(*)(...))t_send_file_data, 2); 
    427439 
    428440        // Provisional: 
  • version_0/lib/eventmachine.rb

    r400 r414  
    12271227 
    12281228 
     1229        # Like EventMachine::Connection#send_data, this sends data to the remote end of 
     1230        # the network connection.  EventMachine::Connection@send_file_data takes a 
     1231        # filename as an argument, though, and sends the contents of the file, in one 
     1232        # chunk. Contributed by Kirk Haines. 
     1233        # 
     1234        def send_file_data filename 
     1235                EventMachine::send_file_data @signature, filename 
     1236        end 
     1237 
    12291238 
    12301239