19 #include <ndb_global.h>
21 #include <OutputStream.hpp>
22 #include <socket_io.h>
24 FileOutputStream::FileOutputStream(FILE *
file){
29 FileOutputStream::print(
const char *
fmt, ...){
32 const int ret = vfprintf(f, fmt, ap);
38 FileOutputStream::println(
const char *
fmt, ...){
41 const int ret = vfprintf(f, fmt, ap);
43 return ret + fprintf(f,
"\n");
46 SocketOutputStream::SocketOutputStream(NDB_SOCKET_TYPE socket,
47 unsigned write_timeout_ms) :
49 m_timeout_ms(write_timeout_ms),
51 m_timeout_remain(write_timeout_ms)
56 SocketOutputStream::print(
const char *
fmt, ...){
64 int ret = vprint_socket(m_socket, m_timeout_ms, &time, fmt, ap);
68 m_timeout_remain-=time;
69 if((ret < 0 && errno==SOCKET_ETIMEDOUT) || m_timeout_remain<=0)
78 SocketOutputStream::println(
const char * fmt, ...){
86 int ret = vprintln_socket(m_socket, m_timeout_ms, &time, fmt, ap);
90 m_timeout_remain-=time;
91 if ((ret < 0 && errno==SOCKET_ETIMEDOUT) || m_timeout_remain<=0)
100 #include <UtilBuffer.hpp>
101 #include <BaseString.hpp>
103 BufferedSockOutputStream::BufferedSockOutputStream(NDB_SOCKET_TYPE socket,
104 unsigned write_timeout_ms) :
110 BufferedSockOutputStream::~BufferedSockOutputStream()
116 BufferedSockOutputStream::print(
const char * fmt, ...){
124 len = BaseString::vsnprintf(buf,
sizeof(buf), fmt, ap);
129 if (tmp.append(len+1) == 0)
134 len = BaseString::vsnprintf((
char*)tmp.get_data(), len+1,
fmt, ap);
138 if ((pos= (
char*)m_buffer.append(len)) == 0)
142 memcpy(pos, tmp.get_data(), tmp.length()-1);
148 BufferedSockOutputStream::println(
const char * fmt, ...){
156 len = BaseString::vsnprintf(buf,
sizeof(buf), fmt, ap);
160 if ((pos= (
char*)m_buffer.append(len+1)) == 0)
165 len = BaseString::vsnprintf((
char*)pos, len+1, fmt, ap);
175 void BufferedSockOutputStream::flush(){
177 if (write_socket(m_socket, m_timeout_ms, &elapsed,
178 (
const char*)m_buffer.get_data(), m_buffer.length()) != 0)
180 fprintf(stderr,
"Failed to flush buffer to socket, errno: %d\n", errno);