示例#1
0
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//
	void
		BitTrace::Set()
	{
		Check_Object(this);

		if (!traceUp++)
		{
			#if defined(USE_ACTIVE_PROFILE)
				SetLineImplementation(activeLine);
			#endif

			TraceManager::Instance->activeBits |= bitFlag;

			#if defined(USE_TIME_ANALYSIS) || defined(USE_TRACE_LOG)
				Time now = gos_GetHiResTime();
			#endif

			#if defined(USE_TIME_ANALYSIS)
				lastActivity = now;
			#endif

			#if defined(USE_TRACE_LOG)
				// Check_Object(traceManager);
				IncrementSampleCount();
				MemoryStream *log = GetTraceLog();
				if (log)
				{
					Check_Object(log);
					TraceSample *sample =
						Cast_Pointer(TraceSample*, log->GetPointer());
					sample->sampleLength = sizeof(*sample);
					sample->sampleType = TraceSample::GoingUp;
					sample->traceNumber = traceNumber;
					sample->sampleTime = now;
					log->AdvancePointer(sample->sampleLength);
				}
			#endif
		}
示例#2
0
void ClientThreadFunc(Client* client)
{
	cout << "Server in ClientThreadFunc, will now wait for handshake" << endl;
	
	// Read the handshake message
	char handshake_msg[1024];
	memset(handshake_msg, 0, sizeof(handshake_msg));
#if defined(WIN32) || defined(_WIN32)
	auto hsByteCount = recv(client->Socket, handshake_msg, sizeof(handshake_msg) - 1, 0);
#else
	auto hsByteCount = read(socket, handshake_msg, sizeof(handshake_msg) - 1);
#endif
	if (0 != strcmp(handshake_msg, "422 echo server handshake"))
	{
		// Not a valid client
		Kill(client->Socket);
		client->InUse = 0;
		cout << "Client handshake was incorrect -> connection refused" << endl;
		cout << "  Client sent:" << endl;
		cout << "  " << handshake_msg << endl;
		return;
	}

	cout << "New client connected!" << endl;
	auto Socket = client->Socket;
	int bufSize = sizeof(client->Buffer);
	
	// Send the welcome message
	cout << "Server about to open welcome file: " << client->Buffer << endl;
	FILE* welcome = fopen(client->Buffer, "rb");
	if (!welcome)
	{
		cout << "Could not open welcome file!" << endl;
		// Shut down the socket
		Kill(client->Socket);
		client->Socket = BAD_SOCKET;
		// Mark this client structure as available for use
		client->InUse = 0;
		cout << "Client disconnected" << endl;
		return;
	}
	fseek(welcome, 0, SEEK_END);
	auto welcomeFileSize = ftell(welcome);
	fseek(welcome, 0, SEEK_SET);
	memset(client->Buffer, 0, sizeof(client->Buffer));
	fread(client->Buffer, 1, welcomeFileSize, welcome);
	fclose(welcome);
#if defined(WIN32) || defined(_WIN32)
	send(Socket, client->Buffer, welcomeFileSize, 0);
#else
	write(Socket, client->Buffer, welcomeFileSize);
#endif

	// Now that we have a socket, enter the read loop
	while (true)
	{
		MemoryStream ms;		
		// Receive into the client buffer
#if defined(WIN32) || defined(_WIN32)
		int bytesRead = recv(Socket, client->Buffer, bufSize, 0);
#else
		int bytesRead = read(Socket, client->Buffer, bufSize);
#endif
		
		// Break the loop if we have an error or no data
		if (bytesRead <= 0) { break; }

		// Append to memory stream
		ms.Write(&client->Buffer[2], bytesRead - 2);
		// First 16-bits is unsigned short telling us total message size in bytes
		int msgSize = ((u16*)&client->Buffer[0])[0];
		// If we didn't receive all the data then do additional receives
		while (bytesRead < msgSize)
		{
#if defined(WIN32) || defined(_WIN32)
			int read = recv(Socket, client->Buffer, bufSize, 0);
#else
			int read += read(Socket, client->Buffer, bufSize);
#endif
			bytesRead += read;
			// Append to memory stream
			ms.Write(client->Buffer, read);
		}
		int zero = 0;
		ms.Write(&zero, 1);
		
		char* fullMsg = (char*)ms.GetPointer();
		cout << "Server received (and will echo back): " << fullMsg << endl;
		// Echo back
#if defined(WIN32) || defined(_WIN32)
		send(Socket, fullMsg, strlen(fullMsg), 0);
#else
		write(Socket, fullMsg, strlen(fullMsg));
#endif
		// Clear buffer
		memset(client->Buffer, 0, strlen(client->Buffer));
	}

	// Shut down the socket
	Kill(client->Socket);
	client->Socket = BAD_SOCKET;

	// Mark this client structure as available for use
	client->InUse = 0;

	cout << "Client disconnected" << endl;
}