communication à revoir : problème de taille de message
This commit is contained in:
parent
6a851b2e66
commit
5ac3285d9b
56
core-test/app/communication-client.c
Normal file
56
core-test/app/communication-client.c
Normal file
@ -0,0 +1,56 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../../core/communication.h"
|
||||
|
||||
#define handle_err(fun,msg)\
|
||||
fprintf (stderr, "%s: file %s line %d %s\n", fun, __FILE__, __LINE__, msg);
|
||||
|
||||
#define MSG "coucou"
|
||||
#define SERVICE_NAME "test"
|
||||
|
||||
int main (int argc, char *argv[], char *env[])
|
||||
{
|
||||
|
||||
size_t msize = BUFSIZ;
|
||||
char *buf = NULL;
|
||||
|
||||
if ( (buf = malloc (BUFSIZ)) == NULL) {
|
||||
handle_err ("main", "malloc");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
memset (buf, 0, BUFSIZ);
|
||||
|
||||
struct service srv;
|
||||
memset(&srv, 0, sizeof (struct service));
|
||||
|
||||
// index and version should be filled
|
||||
srv.index = 0;
|
||||
srv.version = 0;
|
||||
|
||||
// init service
|
||||
if (app_connection (argc, argv, env, &srv, SERVICE_NAME, NULL, 0) < 0) {
|
||||
handle_err("main", "srv_init < 0");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (app_write (&srv, MSG, strlen(MSG)) < 0) {
|
||||
handle_err("main", "app_write < 0");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (app_read (&srv, &buf, &msize) < 0) {
|
||||
handle_err("main", "app_read < 0");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
printf ("msg recv: %s\n", buf);
|
||||
|
||||
if (app_close (&srv) < 0) {
|
||||
handle_err("main", "app_close < 0");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
68
core-test/app/communication-server.c
Normal file
68
core-test/app/communication-server.c
Normal file
@ -0,0 +1,68 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../../core/communication.h"
|
||||
|
||||
#define handle_err(fun,msg)\
|
||||
fprintf (stderr, "%s: file %s line %d %s\n", fun, __FILE__, __LINE__, msg);
|
||||
|
||||
#define MSG "coucou"
|
||||
#define SERVICE_NAME "test"
|
||||
|
||||
int main (int argc, char *argv[], char *env[])
|
||||
{
|
||||
|
||||
size_t msize = BUFSIZ;
|
||||
char *buf = NULL;
|
||||
|
||||
if ( (buf = malloc (BUFSIZ)) == NULL) {
|
||||
handle_err ("main", "malloc");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
memset (buf, 0, BUFSIZ);
|
||||
|
||||
struct service srv;
|
||||
memset(&srv, 0, sizeof (struct service));
|
||||
|
||||
// index and version should be filled
|
||||
srv.index = 0;
|
||||
srv.version = 0;
|
||||
|
||||
struct process p;
|
||||
|
||||
// init service
|
||||
if (srv_init (argc, argv, env, &srv, SERVICE_NAME) < 0) {
|
||||
handle_err("main", "srv_init < 0");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (srv_accept (&srv, &p) < 0) {
|
||||
handle_err("main", "srv_accept < 0");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (srv_read (&p, &buf, &msize) < 0) {
|
||||
handle_err("main", "srv_read < 0");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
printf ("msg recv: %s\n", buf);
|
||||
|
||||
if (srv_write (&p, buf, msize) < 0) {
|
||||
handle_err("main", "srv_write < 0");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (srv_close_proc (&p) < 0) {
|
||||
handle_err("main", "srv_close_proc < 0");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (srv_close (&srv) < 0) {
|
||||
handle_err("main", "srv_close < 0");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
@ -1,14 +1,17 @@
|
||||
#include "communication.h"
|
||||
#include "usocket.h"
|
||||
#include "msg-format.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define handle_error(msg) \
|
||||
do { perror(msg); exit(EXIT_FAILURE); } while (0)
|
||||
|
||||
#define handle_err(fun,msg)\
|
||||
fprintf (stderr, "%s: file %s line %d %s\n", fun, __FILE__, __LINE__, msg);
|
||||
|
||||
void service_path (char *path, const char *sname, int index, int version)
|
||||
{
|
||||
memset (path, 0, PATH_MAX);
|
||||
@ -39,25 +42,47 @@ int srv_init (int argc, char **argv, char **env
|
||||
return 0;
|
||||
}
|
||||
|
||||
// TODO
|
||||
int srv_accept (struct service *srv, struct process *p)
|
||||
{
|
||||
usock_accept (srv->service_fd, &p->proc_fd);
|
||||
|
||||
char buf[3];
|
||||
msg_format_ack (buf);
|
||||
|
||||
srv_write (p, buf, 3);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int srv_close (struct service *srv)
|
||||
{
|
||||
usock_close (srv->service_fd);
|
||||
return usock_remove (srv->spath);
|
||||
}
|
||||
|
||||
int srv_read (const struct service *srv, char ** buf, size_t *msize)
|
||||
int srv_close_proc (struct process *p)
|
||||
{
|
||||
return usock_recv (srv->service_fd, buf, msize);
|
||||
return usock_close (p->proc_fd);
|
||||
}
|
||||
|
||||
int srv_write (const struct service *srv, const char * buf, size_t msize)
|
||||
int srv_read (const struct process *p, char **buf, size_t *msize)
|
||||
{
|
||||
return usock_send (srv->service_fd, buf, msize);
|
||||
return usock_recv (p->proc_fd, buf, msize);
|
||||
}
|
||||
|
||||
int app_connection (struct service *srv, const char *sname
|
||||
int srv_write (const struct process *p, const char * buf, size_t msize)
|
||||
{
|
||||
return usock_send (p->proc_fd, buf, msize);
|
||||
}
|
||||
|
||||
int app_connection (int argc, char **argv, char **env
|
||||
, struct service *srv, const char *sname
|
||||
, const char *connectionstr, size_t msize)
|
||||
{
|
||||
argc = argc;
|
||||
argv = argv;
|
||||
env = env;
|
||||
|
||||
assert (srv != NULL);
|
||||
assert (sname != NULL);
|
||||
@ -73,7 +98,20 @@ int app_connection (struct service *srv, const char *sname
|
||||
|
||||
// TODO: connection algorithm
|
||||
// send connection string and receive acknowledgement
|
||||
srv_write(srv, connectionstr, msize);
|
||||
char send_buffer [BUFSIZ];
|
||||
if (msg_format_con (send_buffer, connectionstr, &msize) < 0) {
|
||||
handle_err ("app_connection", "msg_format_con");
|
||||
return -1;
|
||||
}
|
||||
app_write (srv, send_buffer, msize);
|
||||
|
||||
char *buffer;
|
||||
size_t read_msg_size;
|
||||
|
||||
app_read (srv, &buffer, &read_msg_size);
|
||||
|
||||
assert (read_msg_size == 3);
|
||||
assert (buffer[0] == MSG_TYPE_ACK);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -4,16 +4,10 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
//#include <cbor.h>
|
||||
|
||||
#include <unistd.h> // unlink
|
||||
|
||||
#include <sys/types.h> // mkfifo
|
||||
#include <sys/stat.h> // mkfifo
|
||||
#include <fcntl.h> // open
|
||||
|
||||
#include <errno.h> // error numbers
|
||||
|
||||
#include "process.h"
|
||||
|
||||
#define COMMUNICATION_VERSION 1
|
||||
|
||||
#define ER_FILE_OPEN 1
|
||||
@ -44,16 +38,19 @@ struct service {
|
||||
int srv_init (int argc, char **argv, char **env
|
||||
, struct service *srv, const char *sname);
|
||||
int srv_close (struct service *srv);
|
||||
int srv_close_proc (struct process *p);
|
||||
int srv_accept (struct service *srv, struct process *p);
|
||||
|
||||
int srv_read (const struct service *srv, char ** buf, size_t *msize);
|
||||
int srv_write (const struct service *, const char * buf, size_t);
|
||||
int srv_read (const struct process *, char **buf, size_t *msize);
|
||||
int srv_write (const struct process *, const char * buf, size_t);
|
||||
|
||||
// APPLICATION
|
||||
|
||||
// Initialize connection with unix socket
|
||||
// send the connection string to $TMP/<service>
|
||||
// fill srv->spath && srv->service_fd
|
||||
int app_connection (struct service *, const char *, const char *, size_t);
|
||||
int app_connection (int argc, char **argv, char **env
|
||||
, struct service *, const char *, const char *, size_t);
|
||||
int app_close (struct service *);
|
||||
|
||||
int app_read (struct service *srv, char ** buf, size_t *msize);
|
||||
|
38
core/msg-format.c
Normal file
38
core/msg-format.c
Normal file
@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "msg-format.h"
|
||||
|
||||
#define handle_err(fun,msg)\
|
||||
fprintf (stderr, "%s: file %s line %d %s\n", fun, __FILE__, __LINE__, msg);
|
||||
|
||||
// [type] [len] [val]
|
||||
// 3 valsize constr
|
||||
int msg_format_con (char *buf, const char *constr, size_t *msgsize)
|
||||
{
|
||||
assert (buf != NULL);
|
||||
assert (*msgsize + 3 <= BUFSIZ);
|
||||
|
||||
if (*msgsize + 3 > BUFSIZ) {
|
||||
handle_err ("msg_format_con", "msgsize > BUFSIZ");
|
||||
return 1;
|
||||
}
|
||||
|
||||
buf[0] = MSG_TYPE_CON;
|
||||
short final_size = (short) *msgsize;
|
||||
memcpy (buf + 1, &final_size, 2);
|
||||
memcpy (buf + 3, constr, *msgsize);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// [type] [len]
|
||||
// 4 0
|
||||
int msg_format_ack (char *buf)
|
||||
{
|
||||
assert (buf != NULL);
|
||||
memset(buf, 0, 3);
|
||||
buf[0] = MSG_TYPE_ACK;
|
||||
|
||||
return 0;
|
||||
}
|
13
core/msg-format.h
Normal file
13
core/msg-format.h
Normal file
@ -0,0 +1,13 @@
|
||||
#ifndef __MSG_FORMAT_H__
|
||||
#define __MSG_FORMAT_H__
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#define MSG_TYPE_CON 1
|
||||
#define MSG_TYPE_ERR 2
|
||||
#define MSG_TYPE_ACK 4
|
||||
|
||||
int msg_format_con (char *buf, const char *constr, size_t *msgsize);
|
||||
int msg_format_ack (char *buf);
|
||||
|
||||
#endif
|
@ -1,11 +1,17 @@
|
||||
#ifndef __PROCESS_H__
|
||||
#define __PROCESS_H__
|
||||
|
||||
#if 0
|
||||
struct process {
|
||||
unsigned int version;
|
||||
unsigned int index;
|
||||
int proc_fd;
|
||||
};
|
||||
|
||||
// TODO
|
||||
// tout revoir
|
||||
|
||||
#if 0
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
@ -13,11 +19,6 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
struct process {
|
||||
unsigned int version;
|
||||
unsigned int index;
|
||||
int proc_fd;
|
||||
};
|
||||
|
||||
struct process * srv_process_copy (const struct process *p);
|
||||
|
||||
|
Reference in New Issue
Block a user