#include "XNPipe.h"
#include "XcPipe.h"

/*@ 
@class XNamedPipeServer
@parent XIO
@type overview
@symbol _
*/


/*@ 
@class XNamedPipeClient
@parent XIO
@type overview
@symbol _
*/


/*@ XNamedPipeServer::GetState(void)
@group misc
@remarks Query the state of a pipeserver
@returns <t 'ø' c=2>
øLONG state   øpossible values:
					<t 'ø' c=1>
                        øXNPIPE_STATE_DISCONNECTED
                        øXNPIPE_STATE_LISTENING
                        øXNPIPE_STATE_CONNECTED
                        øXNPIPE_STATE_CLOSING
					</t>
</t>
*/
LONG XNamedPipeServer::GetState(void)
{
	ULONG state, buffer, buffer2;
	AVAILDATA avail;

	DosPeekNPipe(handle, &buffer, 4, &buffer2, &avail, &state);
	return state;
}


LONG XNamedPipeClient::GetState(void)
{
	ULONG state, buffer, buffer2;
	AVAILDATA avail;

	DosPeekNPipe(handle, &buffer, 4, &buffer2, &avail, &state);
	return state;
}


/*@ XNamedPipeServer::Open(char *name, ULONG openMode, ULONG pipeMode, char pipeCount, LONG outSize, long inSize, ULONG timeOut)
@group open/close
@remarks Open a pipe, after you have opened it call Connect()
@parameters <t 'ø' c=2>
				øchar * name       øpipename, without leading '\\PIPE\\'
            øULONG openMode    ømode to open (see OS/2 docs):
										<t 'ø' c=1>
                                 øXNPIPE_ACCESS_INBOUND
                                 øXNPIPE_ACCESS_OUTBOUND
                                 øXNPIPE_ACCESS_DUPLEX
                                 øXNPIPE_INHERIT
                                 øXNPIPE_NOINHERIT
                                 øXNPIPE_WRITEBEHIND
                                 øXNPIPE_NOWRITEBEHIND
										</t>
            øULONG pipeMode    ømode of data-transfer:
										<t 'ø' c=1>
                                 øXNPIPE_READMODE_BYTE
                                 øXNPIPE_READMODE_MESSAGE
                                 øXNPIPE_TYPE_BYTE
                                 øXNPIPE_TYPE_MESSAGE
                                 øXNPIPE_WAIT
                                 øXNPIPE_NOWAIT
										</t>
            øchar pipeCount    ømaximum instances of pipe-servers, -1 = unlimited (default is 1)
            øULONG outSize     øsize of write-buffer
            øULONG inSize      øsize of read-buffer
            øULONG timeOut     øtime to wait for clients
			</t>
*/
LONG XNamedPipeServer::Open(char *name, ULONG openMode, ULONG pipeMode, char pipeCount, LONG outSize, long inSize, ULONG timeOut)
{
   XString n = "\\PIPE\\";
	n += name;
	pipeMode |= pipeCount;
	ULONG rc = DosCreateNPipe((PSZ) n, &handle, openMode, pipeMode, outSize, inSize, timeOut);

	if (rc == 0)
		isOpen = TRUE;
	return rc;
}


/*@ XNamedPipeServer::Connect(void)
@group open/close
@remarks Wait for a client
*/
LONG XNamedPipeServer::Connect(void)
{
	return DosConnectNPipe(handle);
}


/*@ XNamedPipeServer::DisConnect(void)
@group open/close
@remarks Frees a pipe when a client has stoped data-transfer ( if GetState() returns XNPIPE_STATE_CLOSING).
After DisConnect() you can call Connect() to wait for the next client or Close().
*/
LONG XNamedPipeServer::DisConnect(void)
{
	ULONG res = 0;

	if (isOpen)
		res = DosDisConnectNPipe(handle);
	return res;
}


/*@ XNamedPipeClient::WaitForServer(char *name, ULONG timeOut)
@group misc
@remarks Wait for a server
@parameters <t 'ø' c=2>
				øchar * name    øname of the requested pipe (without leading '\\PIPE\\')
				øULONG timeOut  øhow long to wait
				øchar * server	 øname of the server-maschine if the pipe is not on the local maschine<BR>(default is NULL)
				</t>
*/
LONG XNamedPipeClient::WaitForServer(char *name, ULONG timeOut, char * server)
{
	XString n;
	if(server)
	{
		n += "\\";
		n += server;
	}
	n += "\\PIPE\\";
	n += name;	
	return DosWaitNPipe((PSZ) n, timeOut);
}


/*@ XNamedPipeClient::Open(char *name, ULONG modeopen)
@group open/close
@remarks Opens a connection, after it is open call WaitForServer()
@parameters <t 'ø' c=2>
				øchar * name    øname of the requested pipe (without leading '\\PIPE\\'
				øULONG modeOpen ømode to open (see XFile::Open() for possible modes
				øchar * server	øname of the server-maschine if the pipe is not on the local maschine<BR>(default is NULL)
				</t>
*/
LONG XNamedPipeClient::Open(char *name, ULONG modeopen, char * server)
{
	ULONG aktion;
	XString n;
	if(server)
	{
		n += "\\";
		n += server;
	}
	n += "\\PIPE\\";
	n += name;	
	return DosOpen((PSZ) n, &handle, &aktion, 0, FILE_NORMAL, FILE_OPEN, modeopen, NULL);
}

