lib communication : fonctionne (mémoire à vérifier)
This commit is contained in:
parent
5ac3285d9b
commit
af3c9395ff
@ -35,6 +35,7 @@ int main (int argc, char *argv[], char *env[])
|
|||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf ("msg to send: %s\n", MSG);
|
||||||
if (app_write (&srv, MSG, strlen(MSG)) < 0) {
|
if (app_write (&srv, MSG, strlen(MSG)) < 0) {
|
||||||
handle_err("main", "app_write < 0");
|
handle_err("main", "app_write < 0");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "communication.h"
|
#include "communication.h"
|
||||||
#include "usocket.h"
|
#include "usocket.h"
|
||||||
#include "msg-format.h"
|
#include "msg-format.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -46,11 +47,15 @@ int srv_init (int argc, char **argv, char **env
|
|||||||
int srv_accept (struct service *srv, struct process *p)
|
int srv_accept (struct service *srv, struct process *p)
|
||||||
{
|
{
|
||||||
usock_accept (srv->service_fd, &p->proc_fd);
|
usock_accept (srv->service_fd, &p->proc_fd);
|
||||||
|
char *buf;
|
||||||
|
size_t msgsize = BUFSIZ;
|
||||||
|
|
||||||
char buf[3];
|
srv_read (p, &buf, &msgsize);
|
||||||
msg_format_ack (buf);
|
|
||||||
|
|
||||||
srv_write (p, buf, 3);
|
msgsize = 0;
|
||||||
|
msg_format_ack (buf, NULL, &msgsize);
|
||||||
|
|
||||||
|
srv_write (p, buf, msgsize);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -106,12 +111,12 @@ int app_connection (int argc, char **argv, char **env
|
|||||||
app_write (srv, send_buffer, msize);
|
app_write (srv, send_buffer, msize);
|
||||||
|
|
||||||
char *buffer;
|
char *buffer;
|
||||||
size_t read_msg_size;
|
size_t read_msg_size = BUFSIZ;
|
||||||
|
|
||||||
app_read (srv, &buffer, &read_msg_size);
|
app_read (srv, &buffer, &read_msg_size);
|
||||||
|
|
||||||
assert (read_msg_size == 3);
|
|
||||||
assert (buffer[0] == MSG_TYPE_ACK);
|
assert (buffer[0] == MSG_TYPE_ACK);
|
||||||
|
assert (read_msg_size == 3);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -22,17 +22,18 @@ int msg_format_con (char *buf, const char *constr, size_t *msgsize)
|
|||||||
short final_size = (short) *msgsize;
|
short final_size = (short) *msgsize;
|
||||||
memcpy (buf + 1, &final_size, 2);
|
memcpy (buf + 1, &final_size, 2);
|
||||||
memcpy (buf + 3, constr, *msgsize);
|
memcpy (buf + 3, constr, *msgsize);
|
||||||
|
*msgsize += 3;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// [type] [len]
|
// [type] [len]
|
||||||
// 4 0
|
// 4 0
|
||||||
int msg_format_ack (char *buf)
|
int msg_format_ack (char *buf, const char *constr, size_t *msgsize)
|
||||||
{
|
{
|
||||||
assert (buf != NULL);
|
assert (buf != NULL);
|
||||||
memset(buf, 0, 3);
|
memset(buf, 0, 3);
|
||||||
buf[0] = MSG_TYPE_ACK;
|
buf[0] = MSG_TYPE_ACK;
|
||||||
|
|
||||||
|
*msgsize += 3;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,6 @@
|
|||||||
#define MSG_TYPE_ACK 4
|
#define MSG_TYPE_ACK 4
|
||||||
|
|
||||||
int msg_format_con (char *buf, const char *constr, size_t *msgsize);
|
int msg_format_con (char *buf, const char *constr, size_t *msgsize);
|
||||||
int msg_format_ack (char *buf);
|
int msg_format_ack (char *buf, const char *constr, size_t *msgsize);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#include "usocket.h"
|
#include "usocket.h"
|
||||||
|
#include "utils.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
@ -10,13 +11,15 @@
|
|||||||
|
|
||||||
int usock_send (const int fd, const char *buf, const int msize)
|
int usock_send (const int fd, const char *buf, const int msize)
|
||||||
{
|
{
|
||||||
|
print_hexa ("msg send", (unsigned char *)buf, msize);
|
||||||
|
printf ("msg to send: (%d) \n", msize);
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
//printf ("%ld bytes to write\n", msize);
|
//printf ("%ld bytes to write\n", msize);
|
||||||
ret = send (fd, buf, msize, 0);
|
ret = send (fd, buf, msize, 0);
|
||||||
if (ret <= 0) {
|
if (ret <= 0)
|
||||||
fprintf (stderr, "usock_send: file %s line %d send ret <= 0\n"
|
handle_err ("usock_send", "send ret <= 0");
|
||||||
, __FILE__, __LINE__);
|
|
||||||
}
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,6 +40,8 @@ int usock_recv (const int fd, char **buf, size_t *msize)
|
|||||||
|
|
||||||
if (*buf == NULL) {
|
if (*buf == NULL) {
|
||||||
// do not allocate too much memory
|
// do not allocate too much memory
|
||||||
|
if (*msize > BUFSIZ)
|
||||||
|
handle_err ("usock_recv", "msize > BUFSIZ");
|
||||||
*buf = malloc ((*msize < BUFSIZ) ? *msize : BUFSIZ);
|
*buf = malloc ((*msize < BUFSIZ) ? *msize : BUFSIZ);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,6 +53,7 @@ int usock_recv (const int fd, char **buf, size_t *msize)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
*msize = (size_t) ret;
|
*msize = (size_t) ret;
|
||||||
|
print_hexa ("msg recv", (unsigned char *)*buf, *msize);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
22
core/utils.c
Normal file
22
core/utils.c
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
void print_hexa (const char *prefix, unsigned char *val, size_t size)
|
||||||
|
{
|
||||||
|
if (! val)
|
||||||
|
return ;
|
||||||
|
|
||||||
|
size_t i;
|
||||||
|
for(i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
if(! (i % 4))
|
||||||
|
printf("\n%s (%ld) ", prefix, size);
|
||||||
|
printf("%2x ", val[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void mprint_hexa (char *prefix, unsigned char *buf, size_t length)
|
||||||
|
{
|
||||||
|
print_hexa (prefix, buf, length);
|
||||||
|
}
|
11
core/utils.h
Normal file
11
core/utils.h
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#ifndef __UTIL_H__
|
||||||
|
#define __UTIL_H__
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
void print_hexa (const char *prefix, unsigned char *val, size_t size);
|
||||||
|
void mprint_hexa (char *prefix, unsigned char *buf, size_t length);
|
||||||
|
|
||||||
|
#endif
|
Reference in New Issue
Block a user