NamedPipe problems

Guys, I created a simple way to talk to an external process and this way hold my plugin process separated from the main plugin code. So if the external crashes I can recover. I can share the whole project if anyone wants, just need to clean the code a bit. :wink: But the problem is that in some situations the whole thing will fail, and I don’t know why. It just produces bad readouts and the likes. So the NamedPipe read and write just fail at some point. Just by looking at the code below can anyone spot a problem? Today I compiled JUCE with a much larger namedpipe buffer (10 Meg, instead of the 4k default value). Waiting now for users to report back. Sadly on OSX I couldn’t find a way to change the buffer. It seems to be defined in the Kernel, oh apple…

class WusikNamedPipe
{
public:
	// -------------------------------------------------------------------------------------------------------------------------------
	bool create(String name) { return pipe.createNewPipe(name); };
	bool create(String name, ChildProcess* _eProcess) { externalProcess = _eProcess; return pipe.createNewPipe(name); };
	bool open(String name) { return pipe.openExisting(name); };
	void close() { if (pipe.isOpen()) pipe.close(); };
	void resetData() { data.reset(); };
	//
	// -------------------------------------------------------------------------------------------------------------------------------
	bool sendWithSize() { return send(nullptr, -1, -1, true); };
	bool send() { return send((char)-1); };
	bool send(char theCommand) { return send(nullptr, -1, theCommand); };
	//
	bool send(void* otherData, int32 otherDataSize, char theCommand = -1, bool sendSize = false)
	{
		if (externalProcess != nullptr && !externalProcess->isRunning()) return false;
		//
		char* cdata = (char*) data.getData();
		int32 syzeBytes = data.getSize();
		//
		if (otherData != nullptr && otherDataSize != -1)
		{
			cdata = (char*) otherData;
			syzeBytes = otherDataSize;
		}
		//
		int theCounter = 0;
		int offSet = 0;
		//
		if (theCommand != -1)
		{
			if (!pipe.write(&theCommand, sizeof(char), 100)) return false;
			if (!pipe.write(&syzeBytes, sizeof(int32), 100)) return false;
			//
			if (syzeBytes == 0) return true;
		}
		else if (sendSize)
		{
			if (!pipe.write(&syzeBytes, sizeof(int32), 100)) return false;
			//
			if (syzeBytes == 0) return true;
		}
		//
		while (syzeBytes > 0)
		{
			if (externalProcess != nullptr && !externalProcess->isRunning()) break;
			//
			int sendBytes = pipe.write(&(cdata)[offSet], syzeBytes, 400);
			if (sendBytes > 0)
			{
				syzeBytes -= sendBytes;
				offSet += sendBytes;
			}
			//
			theCounter++;
			if (theCounter > 100) return false;
		}
		//
		if (syzeBytes > 0) return false;
		//
		return true;
	};
	//
	// -------------------------------------------------------------------------------------------------------------------------------
	bool receive()
	{
		int32 recBytes = 0;
		if (pipe.read(&recBytes, sizeof(int32), 400) == -1) return false;
		return receive(recBytes); 
	};
	//
	bool receive(int32 size)
	{
		if (externalProcess != nullptr && !externalProcess->isRunning()) return false;
		// 
		int32 syzeBytes = size;
		int theCounter = 0;
		int offSet = 0;
		data.setSize(syzeBytes, false);
		//
		while (syzeBytes > 0)
		{
			if (externalProcess != nullptr && !externalProcess->isRunning()) break;
			//
			int recBytes = pipe.read(&((char*)data.getData())[offSet], syzeBytes, 400);
			if (recBytes > 0)
			{
				syzeBytes -= recBytes;
				offSet += recBytes;
			}
			//
			theCounter++;
			if (theCounter > 100) return false;
		}
		//
		if (syzeBytes > 0) return false;
		//
		return true;
	};
	//
	ChildProcess *externalProcess = nullptr;
	MemoryBlock data;
	NamedPipe pipe;
};

In any event, here’s the whole code. Requires some trimming and changes to work with other plugins, but you get the idea. :wink: