Branch
Hash :
94046104
Author :
Thomas de Grivel
Date :
2020-02-24T15:19:27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
/*
This is part of Patchwork13! the real-time data synthesis toolkit.
http://patchwork13.sourceforge.net/
pw13_cluster - the patchwork13 cluster
Copyright (C) 2005 Thomas de Grivel <billitch@yahoo.fr>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
tcp.c - mode tcp
*/
#ifdef WIN32
# include "../common/win32.h"
#else
# include <sys/types.h>
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# include <unistd.h>
#endif
#include <pw13/pw13.h>
#include "server.h"
#include "tcp.h"
#include "output_server.h"
#include "../common/transmit.h"
void *TCP_mode(void *param)
{
Pw13_Patchwork *pw;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sockTCP;
s_server *server_structure;
pw = malloc ( sizeof(Pw13_Patchwork));
pw13_patchwork_init (pw);
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons (PW13_CLUSTER_SERVER_PORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
memset (&(my_addr.sin_zero), 0, 8);
if ((sockTCP = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
server_structure = create_init_s_server (sockTCP);
if (bind (sockTCP, (struct sockaddr *) &my_addr,
sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
if (listen (sockTCP, MAXBUFLEN) == -1) {
perror ("listen");
exit (1);
}
TCPaccept (sockTCP, &their_addr, pw, server_structure);
return param;
}
/* boucle d'acceptation */
int TCPaccept (int sockTCP, struct sockaddr_in *their_addr,
Pw13_Patchwork *pw,
s_server *server_structure)
{
int sin_size, new_socket;
int num;
while (1) { /* main accept() loop */
sin_size = sizeof(struct sockaddr_in);
if ((new_socket = accept (sockTCP, (struct sockaddr *) their_addr,
(socklen_t*) &sin_size)) == -1) {
printf("error : accept \n");
continue;
}
printf ("serveur: Recu connection de : %s\n",
inet_ntoa (their_addr->sin_addr));
/* num sert a differentier client/output */
waitsock_data (new_socket, sizeof(int));
printf("buffer received \n");
recv (new_socket, (void*) &num, sizeof (num), 0);
if (num) { /* is client */
send_int (new_socket, (int) pw);
send_int (new_socket, (int) pw->master);
printf("TCP_loop_receive_client_of_server \n");
TCP_loop_receive_client_of_server (pw, new_socket, server_structure);
} else { /* is server */
/* il faudrait regler le remplissable correct de pw, pb si plusieurs pw */
printf ("\n\n\n TCP_loop_connect_from_server_to_server\n\n\n");
TCP_loop_receive_server (pw, new_socket);
}
/* pw13_msleep (100); */
}
return 0;
}
/* creation du thread pour recevoir les msg venant du CLIENT */
void TCP_loop_receive_client_of_server (Pw13_Patchwork *pw,
int sockfd,
s_server *server_structure)
{
pthread_t thread_receive;
client_of_server *cos;
/* cree le client associe et l'ajoute dans la liste des clients du server */
cos = create_init_s_client_of_server (pw, sockfd, server_structure);
pthread_create (&thread_receive, NULL,
boucle_message_reseau_client,
cos);
cos->thread_rcv = &thread_receive;
pw13_list_insert_tail (&server_structure->clients,
cos);
}
/* creation du thread pour recevoir les msg venant du SERVER */
void TCP_loop_receive_server (Pw13_Patchwork *pw,
int sockfd)
{
pthread_t thread_receive;
output_server *os;
pw=pw;
os = malloc (sizeof (output_server));
os->socket = sockfd;
pthread_create(&thread_receive, NULL,
boucle_output_server_traitement_message,
os);
os->thread_rcv = &thread_receive;
}