PracticeDev/study_clang/ipc_test/unix_socket/mult_udp/point.h

79 lines
2.8 KiB
C
Raw Normal View History

2022-12-20 17:31:11 +08:00
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <memory.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <pthread.h>
using namespace std;
#define server1_file "/tmp/criu/ipc_server1.sock"
#define client1_file "/tmp/criu/ipc_client1.sock"
#define server2_file "/tmp/criu/ipc_server2.sock"
#define client2_file "/tmp/criu/ipc_client2.sock"
// Global var for unix socket udp
int socket_fd_server_1;
int socket_fd_server_2;
int socket_fd_client_1;
int socket_fd_client_2;
struct sockaddr_un server_addr1;
struct sockaddr_un server_addr2;
struct sockaddr_un client_addr1;
struct sockaddr_un client_addr2;
socklen_t server_addr_len1;
socklen_t server_addr_len2;
socklen_t client_addr_len1;
socklen_t client_addr_len2;
void init_socket()
{
memset(&server_addr1,0,sizeof(server_addr1));
memset(&server_addr2,0,sizeof(server_addr2));
server_addr1.sun_family = AF_UNIX;
server_addr2.sun_family = AF_UNIX;
strcpy(server_addr1.sun_path,server1_file);
strcpy(server_addr2.sun_path,server2_file);
server_addr_len1 = sizeof(server_addr1);
server_addr_len2 = sizeof(server_addr2);
}
ssize_t ipc_udp_server_recv(int __fd, void *__restrict__ __buf, size_t __n, int __flags)
{
client_addr_len1 = sizeof(client_addr1);
client_addr_len2 = sizeof(client_addr2);
if(socket_fd_server_1 == __fd)
return recvfrom(__fd, __buf, __n, __flags, (sockaddr*)&client_addr1, &client_addr_len1);
else if(socket_fd_server_2 == __fd)
return recvfrom(__fd, __buf, __n, __flags, (sockaddr*)&client_addr2, &client_addr_len2);
else
return -1;
}
ssize_t ipc_udp_server_send(int __fd, void *__restrict__ __buf, size_t __n, int __flags)
{
if(socket_fd_server_1 == __fd)
return sendto(__fd, __buf, __n, __flags, (sockaddr*)&client_addr1, client_addr_len1);
else if(socket_fd_server_2 == __fd)
return sendto(__fd, __buf, __n, __flags, (sockaddr*)&client_addr2, client_addr_len2);
else
return -1;
}
ssize_t ipc_udp_client_send(int __fd, void *__restrict__ __buf, size_t __n, int __flags)
{
if(socket_fd_client_1 == __fd)
return sendto(__fd, __buf, __n, __flags, (sockaddr*)&server_addr1, server_addr_len1);
else if(socket_fd_client_2 == __fd)
return sendto(__fd, __buf, __n, __flags, (sockaddr*)&server_addr2, server_addr_len2);
else
return -1;
}
ssize_t ipc_udp_client_recv(int __fd, void *__restrict__ __buf, size_t __n, int __flags)
{
if(socket_fd_client_1 == __fd)
return recvfrom(__fd, __buf, __n, __flags, (sockaddr*)&server_addr1, &server_addr_len1);
else if(socket_fd_client_2 == __fd)
return recvfrom(__fd, __buf, __n, __flags, (sockaddr*)&server_addr2, &server_addr_len2);
else
return -1;
}