Changeset 414
- Timestamp:
- 07/13/07 19:52:55 (1 year ago)
- Files:
-
- version_0/ext/cmain.cpp (modified) (1 diff)
- version_0/ext/eventmachine.h (modified) (1 diff)
- version_0/ext/rubymain.cpp (modified) (2 diffs)
- version_0/lib/eventmachine.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
version_0/ext/cmain.cpp
r384 r414 356 356 357 357 358 359 360 361 358 /********************************* 359 evma_send_file_data_to_connection 360 *********************************/ 361 362 extern "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 51 51 int evma_set_comm_inactivity_timeout (const char *binding, /*in,out*/int *value); 52 52 int evma_get_outbound_data_size (const char *binding); 53 int evma_send_file_data_to_connection (const char *binding, const char *filename); 53 54 54 55 void evma_close_connection (const char *binding, int after_writing); version_0/ext/rubymain.cpp
r384 r414 361 361 evma__epoll(); 362 362 return Qnil; 363 } 364 365 366 /**************** 367 t_send_file_data 368 ****************/ 369 370 static 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); 363 374 } 364 375 … … 425 436 rb_define_module_function (EmModule, "setuid_string", (VALUE(*)(...))t_setuid_string, 1); 426 437 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); 427 439 428 440 // Provisional: version_0/lib/eventmachine.rb
r400 r414 1227 1227 1228 1228 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 1229 1238 1230 1239