extern "C" int evma_send_file_data_to_connection (const char *binding, const char *filename) { /* This is a sugaring over send_data_to_connection that reads a file into a * locally-allocated buffer, and sends the file data to the remote peer. * Return the number of bytes written to the caller. * TODO, needs to impose a limit on the file size. This is intended only for * small files. (I don't know, maybe 8K or less.) For larger files, use interleaved * I/O to avoid slowing the rest of the system down. * TODO: we should return a code rather than barf, in case of file-not-found. * TODO, does this compile on Windows? * TODO, given that we want this to work only with small files, how about allocating * the buffer on the stack rather than the heap? * * Modified 25Jul07. This now returns -1 on file-too-large; 0 for success, and a positive * errno in case of other errors. * /* Contributed by Kirk Haines. */ char data[32*1024]; int r; if (!EventMachine) throw std::runtime_error("not initialized"); int Fd = open (filename, O_RDONLY); if (Fd < 0) return errno; // From here on, all early returns MUST close Fd. struct stat st; if (fstat (Fd, &st)) { int e = errno; close (Fd); return e; } int filesize = st.st_size; if (filesize <= 0) { close (Fd); return 0; } else if (filesize > sizeof(data)) { close (Fd); return -1; } r = read (Fd, data, filesize); if (r != filesize) { int e = errno; close (Fd); return e; } evma_send_data_to_connection (binding, data, r); close (Fd); return 0; }
static VALUE t_send_data (VALUE self, VALUE signature, VALUE data, VALUE data_length) { int b = evma_send_data_to_connection (NUM2ULONG (signature), StringValuePtr (data), FIX2INT (data_length)); return INT2NUM (b); }
void EM::Connection::SendData (const char *data, int length) { evma_send_data_to_connection (Signature.c_str(), data, length); }