Ejemplo n.º 1
0
bool CServer::Start(unsigned short port) {
	// CConnection or Queue
	if (pConnect == NULL 
		|| p_InMsgQueue == NULL 
		|| p_OutMsgQueue == NULL 
		|| isStarted)
		return false;

	Port = port;
	isStarted = true;	// u must start before create thread or thread will exit soon

	if (!pConnect->create(port)) {
		return false;
	}
	
	for (unsigned int i = 0; i < msgthread_num; i++) {
        int rc = iop_thread_create(&pthread_msg[i], MsgProc, (void *) this, 0);
		if( rc == 0 ){
			iop_usleep(10);
#ifdef _DEBUG_INFO_
			cout << "Msg Thread start successfully" << endl;
#endif
		} else {
			isStarted = false;
		}
	}
	
	for (unsigned int i = 0; i < sendthread_num; i++) {
        int rc = iop_thread_create(&pthread_send[i], SendProc, (void *) this, 0);
		if (0 == rc) {
			iop_usleep(10);
#ifdef _DEBUG_INFO_
			cout << "Send Thread start successfully " << endl;
#endif
		} else {
			isStarted = false;
		}
	}

	for (unsigned int i = 0; i < recvthread_num; i++) {
        int rc = iop_thread_create(&pthread_recv[i], RecvProc, (void *) this, 0);
		if (0 == rc) {
			iop_usleep(10);
#ifdef _DEBUG_INFO_
			cout << "Recv Thread start successfully " << endl;
#endif
		} else {
			isStarted = false;
		}
	}



	return isStarted;
}
Ejemplo n.º 2
0
void CClientNet::startupHeartBeat() {
    int rc = iop_thread_create(&pthread_hb, HBProc, (void *) this, 0);
	if (0 == rc) {
		iop_usleep(10);
#ifdef _DEBUG_INFO_
		cout << "Heart Beat Thread start successfully " << endl;
#endif
	} else {
		turnOff();
	}
}
Ejemplo n.º 3
0
void CClientNet::recvProc() {
	ULONG msglen = sizeof(ts_msg);
	TS_PEER_MESSAGE *pmsg = new TS_PEER_MESSAGE();
    memset(pmsg, 0, sizeof(TS_PEER_MESSAGE));
	
	while (isRunning()) {
        if (pConnect->recv(pmsg->msg.Body, msglen) > 0) {
            WriteIn(*pmsg);
		} else {
			iop_usleep(100);
		}
	}
	delete pmsg;
#ifdef _DEBUG_INFO_
	cout << "recv thread exit" << endl;
#endif
}
Ejemplo n.º 4
0
void CClientNet::sendProc() {
	TS_PEER_MESSAGE *pmsg = new TS_PEER_MESSAGE();
	memset(pmsg, 0, sizeof(TS_PEER_MESSAGE));

    iop_usleep(10);
	
	while (isRunning()) {
        ReadOut(*pmsg);
		if (getType(pmsg->msg) > PACKETCONTROL)
			m_agent->send(pmsg->msg.Body, packetSize(pmsg->msg));
		else
            m_Connect->send(pmsg->msg.Body, packetSize(pmsg->msg));
	}
	delete pmsg;
#ifdef _DEBUG_INFO_
	cout << "send thread exit" << endl;
#endif
}
Ejemplo n.º 5
0
bool CServer::Stop(void) {
    if (isRunning()) {
		isStarted = false;
        iop_usleep(100);
        for (unsigned int i = 0; i < sendthread_num; i++) {
            iop_thread_cancel(pthread_send[i]);
		}
        for (unsigned int i = 0; i < recvthread_num; i++) {
            iop_thread_cancel(pthread_recv[i]);
		}
        for (unsigned int i = 0; i < msgthread_num; i++) {
            iop_thread_cancel(pthread_msg[i]);
		}
		delete[] pthread_send;
		delete[] pthread_recv;
		delete[] pthread_msg;
    }
	return true;
}
Ejemplo n.º 6
0
void CClientNet::sendHeartBeat() {
   ts_msg msg;
    while (isRunning()) {
        iop_usleep(HeartBeatInterval);
        UP_HEARTBEAT* upcmd = (UP_HEARTBEAT*) &msg;

        upcmd->head.type = HEARTBEAT;
        upcmd->head.time = getClientTime();
        upcmd->head.size = sizeof(UP_HEARTBEAT);
        upcmd->head.sequence = 0;
        upcmd->head.UID = m_uid;
        upcmd->head.version = VersionNumber;
        upcmd->head.subSeq = 0;
        upcmd->maxSeq = m_seq - 1;

		m_agent->send(msg.Body, upcmd->head.size);
        qDebug() << "heart beat time" << upcmd->head.time;
#ifdef _DEBUG_INFO_
        cout << m_uid << "send heart beat at " << upcmd->head.time << endl;
#endif
    }
}
Ejemplo n.º 7
0
int main(int argc, char** argv){
  msg *messageIn = NULL, *messageOut = NULL;
  char *sender, *rest, *body, *cmd;
  int retval;
  if(argv == NULL){
    fprintf(stderr, "didn't understand: (argv == NULL)\n");
    exit(EXIT_FAILURE);
  }
  if(argc != 4){
    fprintf(stderr, "didn't understand: (argc != 4)\n");
    exit(EXIT_FAILURE);
  }

  self_debug_flag  = SOCKET_DEBUG;
  self = argv[0];
  sock = atoi(argv[1]);
  registry_fifo_in  = argv[2];
  registry_fifo_out = argv[3];

  if(socket_install_handler() != 0){
    perror("socket couldn't install handler\n");
    exit(EXIT_FAILURE);
  }

  while(1){
    requestNo++;
    if(messageIn  != NULL){ 
      freeMsg(messageIn);
      messageIn = NULL;
    }  
    if(messageOut != NULL){
      freeMsg(messageOut);
      messageOut = NULL;
    }
    announce("%s waiting to process request number %d\n", self, requestNo);
    messageIn = acceptMsg(STDIN_FILENO);
    if(messageIn == NULL){
      perror("socket acceptMsg failed");
      continue;
    }
    announce("%s processing request:\n\"%s\"\n", self, messageIn->data);
    retval = parseActorMsg(messageIn->data, &sender, &body);
    if(!retval){
      fprintf(stderr, "didn't understand: (parseActorMsg)\n\t \"%s\" \n", messageIn->data);
      continue;
    }
    if(getNextToken(body, &cmd, &rest) <= 0){
      fprintf(stderr, "didn't understand: (cmd)\n\t \"%s\" \n", body);
      continue;
    }
    if(!strcmp(cmd, "read")){
      if(closed){
        goto readfail;
      } else {
        char *bytes2read;
        int bytesDesired;
        if(getNextToken(rest, &bytes2read, &rest) <= 0){
          fprintf(stderr, 
                  "didn't understand: (bytes2read)\n\t \"%s\" \n", 
                  rest);
          continue;
        }
        bytesDesired = atoi(bytes2read);
        if(bytesDesired == 0){
          goto readfail;
        } else {
	  messageOut = readMsg(sock);
	  if(messageOut == NULL){
	    perror("socket readMsg failed");
	    goto readfail;
	  }
	  announce("%s\n%s\nreadOK\n%d\n%s\n", 
		   sender, 
		   self,
		   messageOut->bytesUsed,
		   messageOut->data);
	  sendFormattedMsgFP(stdout, 
			     "%s\n%s\nreadOK\n%d\n%s\n", 
			     sender, 
			     self,
			     messageOut->bytesUsed,
			     messageOut->data);
	}
      
	continue;
      
      readfail:
	announce("%s\n%s\nreadFailure\n", sender, self);
	sendFormattedMsgFP(stdout, "%s\n%s\nreadFailure\n", sender, self);
	continue;

      }
        
    } else if(!strcmp(cmd, "write")){
      if(closed){
	fprintf(stderr, "closed\n");
        goto writefail;
      } else {
        char *bytes2write;
        int bytesToSend, bytesSent;
        if(getNextToken(rest, &bytes2write, &rest) <= 0){
          fprintf(stderr, 
                  "didn't understand: (bytes2write)\n\t \"%s\" \n", 
                  rest);
          continue;
        }
        bytesToSend = atoi(bytes2write);
        if(bytesToSend == 0){
	  fprintf(stderr, "(bytesToSend == 0)\n");
	  goto writefail;
        } else {
	  int len = strlen(rest);
	  bytesToSend = (len < bytesToSend ) ? len : bytesToSend;
          if((bytesSent = write(sock, rest, bytesToSend)) != bytesToSend){
	    fprintf(stderr, "(bytesSent = write(sock, rest, bytesToSend)) != bytesToSend\n");
            goto writefail;
          } else {
            announce("%s\n%s\nwriteOK\n%d\n", 
		     sender, 
		     self,
		     bytesSent);
            sendFormattedMsgFP(stdout, 
			       "%s\n%s\nwriteOK\n%d\n", 
			       sender, 
			       self,
			       bytesSent);
          }
        }
      }
      
      continue;
      
    writefail:
      announce("%s\n%s\nwriteFailure\n", sender, self);
      sendFormattedMsgFP(stdout, "%s\n%s\nwriteFailure\n", sender, self);
    } else if(!strcmp(cmd, "close")){
      int slotNumber = -1;
      if(!closed){
        closed = 1;
	if(close(sock) < 0){
	  fprintf(stderr, "close failed in socket close case\n");
	}
        announce("%s\n%s\ncloseOK\n", sender, self);
        sendFormattedMsgFP(stdout, "%s\n%s\ncloseOK\n", sender, self);
	iop_usleep(1);
        announce("Socket called %s unregistering\n", self);
	slotNumber = deleteFromRegistry(self);
        announce("Socket called %s removed from slot %d, now exiting\n", 
		 self, slotNumber);
	exit(EXIT_SUCCESS);
      } else {
        announce("%s\n%s\ncloseFailure\n", sender, self);
        sendFormattedMsgFP(stdout, "%s\n%s\ncloseFailure\n", sender, self);
      }
    } else {
      fprintf(stderr, "didn't understand: (command)\n\t \"%s\" \n", messageIn->data);
      continue;
    }
  }
}