sonata: add backend startup handshake reader

This commit is contained in:
Dmitry Simonenko 2016-11-09 16:18:16 +03:00
parent 46ac6dc4b2
commit f4b536574f
2 changed files with 67 additions and 0 deletions

47
lib/so_beread.c Normal file
View File

@ -0,0 +1,47 @@
/*
* sonata.
*
* Protocol-level PostgreSQL client library.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <so_macro.h>
#include <so_stream.h>
#include <so_header.h>
#include <so_beread.h>
int so_beread_startup(sobestartup_t *su, uint8_t *data, uint32_t size)
{
uint32_t pos_size = size;
uint8_t *pos = data;
int rc;
uint32_t version;
rc = so_stream_read32(&version, &pos, &pos_size);
if (so_unlikely(rc == -1))
return -1;
switch (version) {
/* StartupMessage */
case 196608:
su->is_cancel = 0;
break;
/* CancelRequest */
case 80877102: {
su->is_cancel = 1;
rc = so_stream_read32(&su->key_pid, &pos, &pos_size);
if (so_unlikely(rc == -1))
rc = so_stream_read32(&su->key, &pos, &pos_size);
if (so_unlikely(rc == -1))
return -1;
break;
}
default:
return -1;
}
return 0;
}

20
lib/so_beread.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef SO_BEREAD_H_
#define SO_BEREAD_H_
/*
* sonata.
*
* Protocol-level PostgreSQL client library.
*/
typedef struct sobestartup_t sobestartup_t;
struct sobestartup_t {
int is_cancel;
uint32_t key;
uint32_t key_pid;
};
int so_beread_startup(sobestartup_t*, uint8_t*, uint32_t);
#endif