2 Basic socket buffered IO
6 #include "memcached_io.h"
7 #include <sys/select.h>
9 ssize_t
memcached_io_read(memcached_st
*ptr
, unsigned int server_key
,
10 char *buffer
, size_t length
)
17 for (x
= 0, buffer_ptr
= buffer
;
20 if (!ptr
->read_buffer_length
)
26 data_read
= recv(ptr
->hosts
[server_key
].fd
,
45 if (ptr
->flags
& MEM_NO_BLOCK
)
47 struct timeval local_tv
;
50 memset(&local_tv
, 0, sizeof(struct timeval
));
53 local_tv
.tv_usec
= 300;
56 FD_SET(ptr
->hosts
[server_key
].fd
, &set
);
58 select(1, &set
, NULL
, NULL
, &local_tv
);
61 data_read
= recv(ptr
->hosts
[server_key
].fd
,
63 MEMCACHED_MAX_BUFFER
, 0);
70 ptr
->read_buffer_length
= data_read
;
71 ptr
->read_ptr
= ptr
->read_buffer
;
74 if (ptr
->read_buffer_length
== -1)
76 if (ptr
->read_buffer_length
== 0)
79 *buffer_ptr
= *ptr
->read_ptr
;
82 ptr
->read_buffer_length
--;
88 ssize_t
memcached_io_write(memcached_st
*ptr
, unsigned int server_key
,
89 char *buffer
, size_t length
, char with_flush
)
93 for (x
= 0; x
< length
; x
++)
95 ptr
->write_buffer
[ptr
->write_buffer_offset
]= buffer
[x
];
96 ptr
->write_buffer_offset
++;
98 if (ptr
->write_buffer_offset
== MEMCACHED_MAX_BUFFER
)
102 sent_length
= memcached_io_flush(ptr
, server_key
);
104 assert(sent_length
== MEMCACHED_MAX_BUFFER
);
105 ptr
->write_buffer_offset
= 0;
110 memcached_io_flush(ptr
, server_key
);
115 ssize_t
memcached_io_flush(memcached_st
*ptr
, unsigned int server_key
)
119 if (ptr
->write_buffer_offset
== 0)
122 if (ptr
->flags
& MEM_NO_BLOCK
)
124 struct timeval local_tv
;
128 local_tv
.tv_usec
= 300;
131 FD_SET(ptr
->hosts
[server_key
].fd
, &set
);
133 select(1, NULL
, &set
, NULL
, &local_tv
);
135 if ((sent_length
= send(ptr
->hosts
[server_key
].fd
, ptr
->write_buffer
,
136 ptr
->write_buffer_offset
, 0)) == -1)
141 assert(sent_length
== ptr
->write_buffer_offset
);
143 ptr
->write_buffer_offset
= 0;
149 Eventually we will just kill off the server with the problem.
151 void memcached_io_reset(memcached_st
*ptr
, unsigned int server_key
)
153 ptr
->write_buffer_offset
= 0;