Beispiel #1
0
long long Server::multiply(long long a, long long b) {
	// create a PurePath node capturing arguments and return value
	DYNATRACE_API("Server");
	DYNATRACE_AUTO_NODE_CAPTURE(
		DYNATRACE_CAPTURE << a << b
	);
	DYNATRACE_RETURN_INT64(a * b);
}
void* SocketHandler(void* lp){
    int *csock = (int*)lp;

   char pchRequest[BUFSIZ];
   char pchReply[BUFSIZ];
   DWORD cbBytesRead = 0, cbReplyBytes = 0;

	/*char buffer[1024];
	int buffer_len = 1024;*/
	//int bytecount;

	memset(pchRequest, 0, BUFSIZ);
	if((cbBytesRead = recv(*csock, pchRequest, BUFSIZ, 0)) == (DWORD)-1){
		fprintf(stderr, "Error receiving data %d\n", errno);
		close(*csock);
		free(csock);
	    return 0;
	}
	printf("Received bytes %d\nReceived string \"%s\"\n", cbBytesRead, pchRequest);
	//strcat(buffer, " SERVER ECHO");

	pchRequest[cbBytesRead] = '\0';

	// check for Exit-Message
	if(0 == strcmp(pchRequest, szExitMessage)) {
		printf("Received Exit-Message, pinging Server to stop it.\n");
		stop = true;
		close(*csock);
		free(csock);

        // create a socket connection so that the server comes out of the accept() call		
		pingServerSocket();
		
		return 0;
	}
	
   // Print verbose messages. In production code, this should be for debugging only.
   //printf("InstanceThread created, receiving and processing messages, GLE=%d\n", GetLastError());


	  /*for(DWORD i = 0;i < cbBytesRead+1;i++) {
		  printf("%i/%c ", pchRequest[i], pchRequest[i]);
	  } printf("\n");*/

	char tag[BUFSIZE];

	// look for the tag in the sent data
	char* pos = strchr(pchRequest, '|');

	// if we do not find the pipe, there is no tag information
	if(pos != NULL) {
		strncpy(tag, pchRequest, pos-pchRequest);
		tag[pos-pchRequest] = '\0';
		strcpy(pchRequest, pos+1);
		
		pchRequest[cbBytesRead - (pos-pchRequest+1)] = '\0';

		//printf("Having %d, cutting down to %d: %s\n", cbBytesRead, cbBytesRead - (pos-pchRequest+1), pchRequest);
	}

	//printf("After tagging: %s\n", pchRequest);

	// only set the tag if we have one
	if(pos != NULL && pos > pchRequest) {
		_tprintf(TEXT("Found tag: %s, GLE=%d.\n"), tag, GetLastError());

		DYNATRACE_SET_TAG_FROM_STRING(tag);	// retrieve and set tag
		DYNATRACE_START_SERVER_PUREPATH();				// start server-side PurePath
	} /* does not work... else {
		// start normal purepath if we
		DYNATRACE_START_PUREPATH();
	}*/

	ssize_t cbWritten = 0; 
	{ // create a block for auto-node-capture to work correctly here
	  DYNATRACE_AUTO_NODE_CAPTURE(DYNATRACE_CAPTURE << REPLACEMENT << cbBytesRead);

      // Process the incoming message.
      GetAnswerToRequest(pchRequest, pchReply, &cbReplyBytes);

	  _tprintf( TEXT("Handled data, returning: %s (%d), GLE=%d\n"), pchReply, cbReplyBytes, GetLastError());


		if((cbWritten = send(*csock, pchReply, cbReplyBytes, 0))== -1){
			fprintf(stderr, "Error sending data %d\n", errno);
			close(*csock);
			free(csock);
			return 0;
		}
	
	}

	if(pos != NULL && pos > pchRequest) {
		DYNATRACE_END_SERVER_PUREPATH();				// end server-side PurePath
	} /* does not work... based on scoping!! else {
		DYNATRACE_EXIT();
	}*/

      if (/*!fSuccess ||*/ cbReplyBytes != (DWORD)cbWritten)
      {
          _tprintf(TEXT("InstanceThread WriteFile failed, GLE=%d.\n"), GetLastError());
		close(*csock);
		free(csock);
		return 0;
      }

	printf("Sent bytes %d\n", (int)cbWritten);

	close(*csock);
	free(csock);
	
	return 0;
}