sprintf(str_port, "%u", server->port);
memset(&hints, 0, sizeof(hints));
+
hints.ai_family= AF_INET;
- hints.ai_socktype= SOCK_STREAM;
- hints.ai_protocol= IPPROTO_TCP;
+ if (server->type == MEMCACHED_CONNECTION_UDP)
+ {
+ hints.ai_protocol= IPPROTO_UDP;
+ hints.ai_socktype= SOCK_DGRAM;
+ }
+ else
+ {
+ hints.ai_socktype= SOCK_STREAM;
+ hints.ai_protocol= IPPROTO_TCP;
+ }
e= getaddrinfo(server->hostname, str_port, &hints, &ai);
if (e != 0)
return MEMCACHED_SUCCESS;
}
-static memcached_return udp_connect(memcached_server_st *ptr)
-{
- if (ptr->fd == -1)
- {
- /* Old connection junk still is in the structure */
- WATCHPOINT_ASSERT(ptr->cursor_active == 0);
-
- /*
- If we have not allocated the hosts object.
- Or if the cache has not been set.
- */
- if (ptr->sockaddr_inited == MEMCACHED_NOT_ALLOCATED ||
- (!(ptr->root->flags & MEM_USE_CACHE_LOOKUPS)))
- {
- memcached_return rc;
-
- rc= set_hostinfo(ptr);
- if (rc != MEMCACHED_SUCCESS)
- return rc;
-
- ptr->sockaddr_inited= MEMCACHED_ALLOCATED;
- }
-
- /* Create the socket */
- if ((ptr->fd= socket(AF_INET, SOCK_DGRAM, 0)) < 0)
- {
- ptr->cached_errno= errno;
- return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
- }
- }
-
- return MEMCACHED_SUCCESS;
-}
-
-static memcached_return tcp_connect(memcached_server_st *ptr)
+static memcached_return network_connect(memcached_server_st *ptr)
{
if (ptr->fd == -1)
{
return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
}
+ if (ptr->type == MEMCACHED_CONNECTION_UDP)
+ return MEMCACHED_SUCCESS;
+
if (ptr->root->flags & MEM_NO_BLOCK)
{
int error;
rc= MEMCACHED_NOT_SUPPORTED;
break;
case MEMCACHED_CONNECTION_UDP:
- rc= udp_connect(ptr);
- break;
case MEMCACHED_CONNECTION_TCP:
- rc= tcp_connect(ptr);
+ rc= network_connect(ptr);
break;
case MEMCACHED_CONNECTION_UNIX_SOCKET:
rc= unix_socket_connect(ptr);
}
static ssize_t io_flush(memcached_server_st *ptr,
- memcached_return *error)
+ memcached_return *error)
{
size_t sent_length;
size_t return_length;
if (write_length == MEMCACHED_MAX_BUFFER)
WATCHPOINT_ASSERT(ptr->write_buffer == local_write_ptr);
WATCHPOINT_ASSERT((ptr->write_buffer + MEMCACHED_MAX_BUFFER) >= (local_write_ptr + write_length));
+
return_length= 0;
while (write_length)
{
+ WATCHPOINT_ASSERT(write_length > 0);
sent_length= 0;
if (ptr->type == MEMCACHED_CONNECTION_UDP)
{
- sent_length= sendto(ptr->fd, local_write_ptr, write_length, 0,
- (struct sockaddr *)&ptr->address_info->ai_addr,
- sizeof(struct sockaddr));
+ struct addrinfo *ai;
+
+ ai= ptr->address_info;
+
+ /* Crappy test code */
+ char buffer[HUGE_STRING_LEN + 8];
+ memset(buffer, 0, HUGE_STRING_LEN + 8);
+ memcpy (buffer+8, local_write_ptr, write_length);
+ buffer[0]= 0;
+ buffer[1]= 0;
+ buffer[2]= 0;
+ buffer[3]= 0;
+ buffer[4]= 0;
+ buffer[5]= 1;
+ buffer[6]= 0;
+ buffer[7]= 0;
+ sent_length= sendto(ptr->fd, buffer, write_length + 8, 0,
+ (struct sockaddr *)ai->ai_addr,
+ ai->ai_addrlen);
+ if (sent_length == -1)
+ {
+ WATCHPOINT_ERRNO(errno);
+ WATCHPOINT_ASSERT(0);
+ }
+ sent_length-= 8; /* We remove the header */
}
else
{