phase=999; //not going further
}
break;
- case 1 : //waiting from contact
+ case 1 : //properly closing remote contact
if (shutdown(contact->channel,SHUT_RDWR)<0) {
switch (errno) {
case ENOTCONN : //already disconnect by other side!
break;
}
}
- (void) usleep(10000);
- (void) close(contact->channel);
+ if (close(contact->channel)<0) {
+ (void) rou_alert(0,"%s unable to close channel properly (errno=<%s>)",
+ OPEP,strerror(errno));
+ }
break;
case 2 : //freeing contact memory
(void) free(contact);
#define TESTL 4
CONTYP *contact;
-int client_fd;
pid_t child;
int phase;
_Bool proceed;
(void) prc_settitle("Waiting contact on %s:%s",binding->ip,binding->port);
break;
case 1 : //do we have a contact
- client_fd=accept(binding->handle,NULL,NULL);
- printf("New client connected: %d\n",client_fd);
+ if ((contact=tcp_getcontact(binding))==(CONTYP *)0) {
+ phase=999; //No contact!
+ }
break;
case 2 : //forking process
+ printf("New client connected: %d\n",contact->channel);
if ((child=fork())!=(pid_t)0) {
- (void) close(client_fd);
- (void) fprintf(stdout,"JMPDBG retart\n");
- phase=0;
+ (void) close(contact->channel);
+ (void) free(contact);
+ contact=(CONTYP *)0;
+ phase=999; //no need to go further
}
break;
case 3 : //do contact
(void) close(binding->handle);
+ printf("Client channel=%d (pid=%d)\n",contact->channel,getpid());
for (int i=0;i<TESTL;i++) {
- (void) dprintf(client_fd,"Remote pid=%d iter=%d/%d\n",getpid(),i,TESTL);
+ (void) dprintf(contact->channel,"Remote pid=%d iter=%d/%d\n",
+ getpid(),i,TESTL);
(void) sleep(1);
}
break;
- case 4 : //close contact
- if (shutdown(client_fd,SHUT_RDWR)<0) {
- (void) printf("Unable to shutdown properly (error=<%s>)\n",strerror(errno));
- }
- if (close(client_fd)<0) {
- (void) printf("Unable to close properly (error=<%s>(\n",strerror(errno));
- }
- (void) exit(0);
+ case 4 : //connection terminated
+ (void) rou_alert(0,"Contact IP/PORT=<%s/%s> Exiting",
+ binding->ip,binding->port);
+ contact=tcp_dropcontact(contact);
+ (void) exit(0); //exiting sub process
break;
default : //SAFE guard
proceed=false;
break;
case 1 : //dispating need process
(void) docontact(binding); //waiting, handling remote contact
- (void) rou_alert(0,"Contact Exiting: %s-%s",appname,rou_getversion());
break;
default : //SAFE Guard
proceed=false;
while (proceed==true) {
switch (phase) {
case 0 : //lets get a socket
- if ((handle=socket(AF_INET, SOCK_STREAM,0))<0) {
- //if ((handle=socket(ai->ai_family,ai->ai_socktype,ai->ai_protocol))<0) {
+ if ((handle=socket(ai->ai_family,ai->ai_socktype,ai->ai_protocol))<0) {
(void) rou_alert(0,"%s Unable to open socket for <%s> (error='%s')",
OPEP,ai->ai_canonname,strerror(errno));
phase=999; //trouble trouble
server_addr.sin_family = AF_INET;
inet_pton(AF_INET,"127.0.0.1", &(server_addr.sin_addr));
server_addr.sin_port = htons(2525);
- if (bind(handle,(struct sockaddr*) &server_addr, sizeof(server_addr))<0) {
- //if (bind(handle,ai->ai_addr,ai->ai_addrlen)<0) {
+ if (bind(handle,ai->ai_addr,ai->ai_addrlen)<0) {
(void) rou_alert(0,"%s Unable to bind on IP/port <%s/%s> (error='%s')",
OPEP,ai->ai_canonname,binding->port,strerror(errno));
phase=999; //trouble cleanup phase