コード例 #1
0
ファイル: Server.cpp プロジェクト: cmdpdl/MassTransit
void Server::run(int port) throw (SocketException) {
	listening(port);
	std::cout << "server awaiting connections..." << std::endl;

	while (true) {
		SOCKET client = accept(listen, NULL, NULL);
		std::cout << "accepted client" << std::endl;

		SocketStream stream(client);

		// read the dynaTrace tag (if present) and set it
		unsigned char length = stream.getByte();
        // if length != 0, the message contains a dynaTrace tag
        if (length != 0) {
			unsigned char tag[256];
			stream.get(tag, length);
			DYNATRACE_SET_TAG(tag);
        }

		// reading "payload"
		long long a = stream.getInt64();
		long long b = stream.getInt64();

        // no dynaTrace tag was sent => we assume that the data itself were used as "custom tag"
        if (length == 0) {
			unsigned char customTag[CTAG_SIZE];
            MultiAppNative::getCustomTagFromData(customTag, a, b);
			DYNATRACE_SET_CUSTOMTAG(customTag, sizeof(customTag));
        }
		// start server-side PurePath
		DYNATRACE_START_SERVER_PUREPATH();

		long long r = multiply(a, b);
		std::cout << a << "*" << b << "=" << r << std::endl;
		stream.putInt64(r);

		// end server-side PurePath
		DYNATRACE_END_SERVER_PUREPATH();
	}
}
コード例 #2
0
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;
}