Skip to content

Instantly share code, notes, and snippets.

@IlyaM
Created June 5, 2013 16:11
Show Gist options
  • Save IlyaM/5715139 to your computer and use it in GitHub Desktop.
Save IlyaM/5715139 to your computer and use it in GitHub Desktop.
Tarantool select reply processing code
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include "tp.h"
int main() {
int sock;
struct sockaddr_in tt;
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
printf("Failed to create socket\n");
return 1;
}
memset(&tt, 0, sizeof(tt));
tt.sin_family = AF_INET;
tt.sin_addr.s_addr = inet_addr("127.0.0.1");
tt.sin_port = htons(33013);
if (connect(sock, (struct sockaddr *) &tt, sizeof(tt)) < 0) {
printf("Failed to connect\n");
}
{
struct tp req;
tp_init(&req, NULL, 0, tp_realloc, NULL);
tp_select(&req, 0, 0, 0, 1);
tp_tuple(&req);
tp_sz(&req, "0e72ae1a-d0be-4e49-aeb9-aebea074363c");
write(sock, tp_buf(&req), tp_used(&req));
tp_free(&req);
}
{
struct tp rep;
tp_init(&rep, NULL, 0, tp_realloc, NULL);
while (1) {
ssize_t to_read = tp_req(&rep);
if (to_read <= 0)
break;
ssize_t new_size = tp_ensure(&rep, to_read);
if (new_size == -1) {
// no memory (?)
return 1;
}
ssize_t res = read(sock, rep.p, to_read);
if (res == 0) {
// eof
return 1;
} else if (res < 0) {
// error
return 1;
}
tp_use(&rep, res);
}
ssize_t server_code = tp_reply(&rep);
printf("op: %d\n", tp_replyop(&rep));
printf("count: %d\n", tp_replycount(&rep));
printf("code: %zu\n", server_code);
if (server_code != 0) {
printf("error: %-.*s\n", tp_replyerrorlen(&rep),
tp_replyerror(&rep));
}
tp_free(&rep);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment