Snapshot, replace tabs with spaces
This commit is contained in:
@@ -3,190 +3,190 @@
|
|||||||
#include <jni.h>
|
#include <jni.h>
|
||||||
#include "pipe.h"
|
#include "pipe.h"
|
||||||
|
|
||||||
#define DEBUG 0
|
#define DEBUG 0
|
||||||
|
|
||||||
JNIEXPORT jint JNICALL Java_pipe_Pipe_CreateNamedPipe(JNIEnv *env,
|
JNIEXPORT jint JNICALL Java_pipe_Pipe_CreateNamedPipe(JNIEnv *env,
|
||||||
jclass className, jstring sPipeName, jint dwOpenMode, jint dwPipeMode,
|
jclass className, jstring sPipeName, jint dwOpenMode, jint dwPipeMode,
|
||||||
jint nMaxInstances, jint nOutBufferSize, jint nInBufferSize,
|
jint nMaxInstances, jint nOutBufferSize, jint nInBufferSize,
|
||||||
jint nDefaultTimeOut, jint lpSecurityAttributes) {
|
jint nDefaultTimeOut, jint lpSecurityAttributes) {
|
||||||
HANDLE pipeHandler;
|
HANDLE pipeHandler;
|
||||||
LPCSTR pipeName;
|
LPCSTR pipeName;
|
||||||
pipeName = (*env)->GetStringUTFChars(env, sPipeName, NULL );
|
pipeName = (*env)->GetStringUTFChars(env, sPipeName, NULL );
|
||||||
if (pipeName == NULL )
|
if (pipeName == NULL )
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
printf("Native: Pipe Name %s\n", pipeName);
|
printf("Native: Pipe Name %s\n", pipeName);
|
||||||
printf("Native: dwOpenMode %d\n", dwOpenMode);
|
printf("Native: dwOpenMode %d\n", dwOpenMode);
|
||||||
printf("Native: dwPipeMode %d\n", dwPipeMode);
|
printf("Native: dwPipeMode %d\n", dwPipeMode);
|
||||||
printf("Native: nMaxInstances %d\n", nMaxInstances);
|
printf("Native: nMaxInstances %d\n", nMaxInstances);
|
||||||
printf("Native: nOutBufferSize %d\n", nOutBufferSize);
|
printf("Native: nOutBufferSize %d\n", nOutBufferSize);
|
||||||
printf("Native: nInBufferSize %d\n", nInBufferSize);
|
printf("Native: nInBufferSize %d\n", nInBufferSize);
|
||||||
printf("Native: nDefaultTimeOut %d\n", nDefaultTimeOut);
|
printf("Native: nDefaultTimeOut %d\n", nDefaultTimeOut);
|
||||||
}
|
}
|
||||||
|
|
||||||
pipeHandler = CreateNamedPipe((LPCSTR) pipeName, dwOpenMode, dwPipeMode,
|
pipeHandler = CreateNamedPipe((LPCSTR) pipeName, dwOpenMode, dwPipeMode,
|
||||||
nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut,
|
nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut,
|
||||||
(LPSECURITY_ATTRIBUTES) lpSecurityAttributes);
|
(LPSECURITY_ATTRIBUTES) lpSecurityAttributes);
|
||||||
|
|
||||||
(*env)->ReleaseStringUTFChars(env, sPipeName, pipeName);
|
(*env)->ReleaseStringUTFChars(env, sPipeName, pipeName);
|
||||||
return (jint) pipeHandler;
|
return (jint) pipeHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT jboolean JNICALL Java_pipe_Pipe_ConnectNamedPipe(JNIEnv *env,
|
JNIEXPORT jboolean JNICALL Java_pipe_Pipe_ConnectNamedPipe(JNIEnv *env,
|
||||||
jclass className, jint hNamedPipe, jint lpOverlapped) {
|
jclass className, jint hNamedPipe, jint lpOverlapped) {
|
||||||
BOOL fConnected;
|
BOOL fConnected;
|
||||||
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
||||||
fConnected = ConnectNamedPipe(pipeHandler, (LPOVERLAPPED) lpOverlapped);
|
fConnected = ConnectNamedPipe(pipeHandler, (LPOVERLAPPED) lpOverlapped);
|
||||||
return fConnected;
|
return fConnected;
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT jint JNICALL Java_pipe_Pipe_GetLastError(JNIEnv *env,
|
JNIEXPORT jint JNICALL Java_pipe_Pipe_GetLastError(JNIEnv *env,
|
||||||
jclass className) {
|
jclass className) {
|
||||||
DWORD errorNumber = GetLastError();
|
DWORD errorNumber = GetLastError();
|
||||||
return (jint) errorNumber;
|
return (jint) errorNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT jboolean JNICALL Java_pipe_Pipe_CloseHandle(JNIEnv *env,
|
JNIEXPORT jboolean JNICALL Java_pipe_Pipe_CloseHandle(JNIEnv *env,
|
||||||
jclass className, jint hNamedPipe) {
|
jclass className, jint hNamedPipe) {
|
||||||
BOOL result;
|
BOOL result;
|
||||||
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
||||||
result = CloseHandle(pipeHandler);
|
result = CloseHandle(pipeHandler);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT jbyteArray JNICALL Java_pipe_Pipe_ReadFile(JNIEnv *env,
|
JNIEXPORT jbyteArray JNICALL Java_pipe_Pipe_ReadFile(JNIEnv *env,
|
||||||
jclass className, jint hNamedPipe, jint nNumberOfBytesToRead) {
|
jclass className, jint hNamedPipe, jint nNumberOfBytesToRead) {
|
||||||
int bytesRead = 0;
|
int bytesRead = 0;
|
||||||
BOOL result;
|
BOOL result;
|
||||||
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
||||||
LPVOID buffer;
|
LPVOID buffer;
|
||||||
jbyteArray lpBuffer;
|
jbyteArray lpBuffer;
|
||||||
|
|
||||||
buffer = (LPVOID) LocalAlloc(LMEM_ZEROINIT, nNumberOfBytesToRead);
|
buffer = (LPVOID) LocalAlloc(LMEM_ZEROINIT, nNumberOfBytesToRead);
|
||||||
|
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
printf(
|
printf(
|
||||||
"Native: Before ReadFile pipeHandler %d nNumberOfBytesToRead %d\n",
|
"Native: Before ReadFile pipeHandler %d nNumberOfBytesToRead %d\n",
|
||||||
pipeHandler, nNumberOfBytesToRead);
|
pipeHandler, nNumberOfBytesToRead);
|
||||||
}
|
}
|
||||||
result = ReadFile(pipeHandler, (LPVOID) buffer,
|
result = ReadFile(pipeHandler, (LPVOID) buffer,
|
||||||
(DWORD) nNumberOfBytesToRead, &bytesRead, (LPOVERLAPPED) 0);
|
(DWORD) nNumberOfBytesToRead, &bytesRead, (LPOVERLAPPED) 0);
|
||||||
if (result) {
|
if (result) {
|
||||||
lpBuffer = (*env)->NewByteArray(env, (jsize) bytesRead);
|
lpBuffer = (*env)->NewByteArray(env, (jsize) bytesRead);
|
||||||
(*env)->SetByteArrayRegion(env, lpBuffer, 0, (jsize) bytesRead,
|
(*env)->SetByteArrayRegion(env, lpBuffer, 0, (jsize) bytesRead,
|
||||||
(jbyte *) buffer);
|
(jbyte *) buffer);
|
||||||
} else
|
} else
|
||||||
bytesRead = 0;
|
bytesRead = 0;
|
||||||
|
|
||||||
LocalFree(buffer);
|
LocalFree(buffer);
|
||||||
|
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
printf("Native: After ReadFile BytesRead %d\n", bytesRead);
|
printf("Native: After ReadFile BytesRead %d\n", bytesRead);
|
||||||
}
|
}
|
||||||
return lpBuffer;
|
return lpBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT jint JNICALL Java_pipe_Pipe_WriteFile(JNIEnv *env, jclass className,
|
JNIEXPORT jint JNICALL Java_pipe_Pipe_WriteFile(JNIEnv *env, jclass className,
|
||||||
jint hNamedPipe, jbyteArray lpBuffer, jint nNumberOfBytesToWrite) {
|
jint hNamedPipe, jbyteArray lpBuffer, jint nNumberOfBytesToWrite) {
|
||||||
int bytesWritten = 0;
|
int bytesWritten = 0;
|
||||||
BOOL result;
|
BOOL result;
|
||||||
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
||||||
LPVOID buffer;
|
LPVOID buffer;
|
||||||
|
|
||||||
buffer = (LPVOID) LocalAlloc(LMEM_ZEROINIT, nNumberOfBytesToWrite);
|
buffer = (LPVOID) LocalAlloc(LMEM_ZEROINIT, nNumberOfBytesToWrite);
|
||||||
|
|
||||||
(*env)->GetByteArrayRegion(env, lpBuffer, 0, nNumberOfBytesToWrite, buffer);
|
(*env)->GetByteArrayRegion(env, lpBuffer, 0, nNumberOfBytesToWrite, buffer);
|
||||||
result = WriteFile(pipeHandler, buffer, (DWORD) nNumberOfBytesToWrite,
|
result = WriteFile(pipeHandler, buffer, (DWORD) nNumberOfBytesToWrite,
|
||||||
(LPDWORD) &bytesWritten, (LPOVERLAPPED) 0);
|
(LPDWORD) &bytesWritten, (LPOVERLAPPED) 0);
|
||||||
LocalFree(buffer);
|
LocalFree(buffer);
|
||||||
|
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
printf("Native: After WriteFile BytesReadWritten %d\n", bytesWritten);
|
printf("Native: After WriteFile BytesReadWritten %d\n", bytesWritten);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!result) {
|
if (!result) {
|
||||||
if (GetLastError() != ERROR_IO_PENDING)
|
if (GetLastError() != ERROR_IO_PENDING)
|
||||||
result = 0;
|
result = 0;
|
||||||
else
|
else
|
||||||
result = 1;
|
result = 1;
|
||||||
}
|
}
|
||||||
if (!result) {
|
if (!result) {
|
||||||
bytesWritten = -1;
|
bytesWritten = -1;
|
||||||
}
|
}
|
||||||
return bytesWritten;
|
return bytesWritten;
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT jboolean JNICALL Java_pipe_Pipe_FlushFileBuffers(JNIEnv *env,
|
JNIEXPORT jboolean JNICALL Java_pipe_Pipe_FlushFileBuffers(JNIEnv *env,
|
||||||
jclass className, jint hNamedPipe) {
|
jclass className, jint hNamedPipe) {
|
||||||
BOOL result;
|
BOOL result;
|
||||||
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
||||||
result = FlushFileBuffers(pipeHandler);
|
result = FlushFileBuffers(pipeHandler);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT jboolean JNICALL Java_pipe_Pipe_DisconnectNamedPipe(JNIEnv *env,
|
JNIEXPORT jboolean JNICALL Java_pipe_Pipe_DisconnectNamedPipe(JNIEnv *env,
|
||||||
jclass className, jint hNamedPipe) {
|
jclass className, jint hNamedPipe) {
|
||||||
BOOL result;
|
BOOL result;
|
||||||
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
||||||
result = DisconnectNamedPipe(pipeHandler);
|
result = DisconnectNamedPipe(pipeHandler);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT jint JNICALL Java_pipe_Pipe_CreateFile(JNIEnv *env, jclass className,
|
JNIEXPORT jint JNICALL Java_pipe_Pipe_CreateFile(JNIEnv *env, jclass className,
|
||||||
jstring lpFileName, jint dwDesiredAccess, jint dwShareMode,
|
jstring lpFileName, jint dwDesiredAccess, jint dwShareMode,
|
||||||
jint lpSecurityAttributes, jint dwCreationDisposition,
|
jint lpSecurityAttributes, jint dwCreationDisposition,
|
||||||
jint dwFlagsAndAttributes, jint hTemplateFile) {
|
jint dwFlagsAndAttributes, jint hTemplateFile) {
|
||||||
HANDLE pipeHandler;
|
HANDLE pipeHandler;
|
||||||
const jbyte *fileName;
|
const jbyte *fileName;
|
||||||
fileName = (*env)->GetStringUTFChars(env, lpFileName, NULL );
|
fileName = (*env)->GetStringUTFChars(env, lpFileName, NULL );
|
||||||
if (fileName == NULL )
|
if (fileName == NULL )
|
||||||
return -1;
|
return -1;
|
||||||
pipeHandler = CreateFile((LPCSTR) fileName, (DWORD) dwDesiredAccess,
|
pipeHandler = CreateFile((LPCSTR) fileName, (DWORD) dwDesiredAccess,
|
||||||
(DWORD) dwShareMode, (LPSECURITY_ATTRIBUTES) lpSecurityAttributes,
|
(DWORD) dwShareMode, (LPSECURITY_ATTRIBUTES) lpSecurityAttributes,
|
||||||
(DWORD) dwCreationDisposition, (DWORD) dwFlagsAndAttributes,
|
(DWORD) dwCreationDisposition, (DWORD) dwFlagsAndAttributes,
|
||||||
(HANDLE) hTemplateFile);
|
(HANDLE) hTemplateFile);
|
||||||
return (jint) pipeHandler;
|
return (jint) pipeHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT jboolean JNICALL Java_pipe_Pipe_WaitNamedPipe(JNIEnv *env,
|
JNIEXPORT jboolean JNICALL Java_pipe_Pipe_WaitNamedPipe(JNIEnv *env,
|
||||||
jclass className, jstring lpNamedPipeName, jint nTimeOut) {
|
jclass className, jstring lpNamedPipeName, jint nTimeOut) {
|
||||||
BOOL result;
|
BOOL result;
|
||||||
const jbyte *pipeName;
|
const jbyte *pipeName;
|
||||||
pipeName = (*env)->GetStringUTFChars(env, lpNamedPipeName, NULL );
|
pipeName = (*env)->GetStringUTFChars(env, lpNamedPipeName, NULL );
|
||||||
if (pipeName == NULL )
|
if (pipeName == NULL )
|
||||||
return 0;
|
return 0;
|
||||||
result = WaitNamedPipe((LPCSTR) pipeName, (DWORD) nTimeOut);
|
result = WaitNamedPipe((LPCSTR) pipeName, (DWORD) nTimeOut);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT jstring JNICALL Java_pipe_Pipe_FormatMessage(JNIEnv *env,
|
JNIEXPORT jstring JNICALL Java_pipe_Pipe_FormatMessage(JNIEnv *env,
|
||||||
jclass className, jint errorCode) {
|
jclass className, jint errorCode) {
|
||||||
LPVOID lpMsgBuf;
|
LPVOID lpMsgBuf;
|
||||||
LPVOID lpDisplayBuf;
|
LPVOID lpDisplayBuf;
|
||||||
DWORD dw = (DWORD) errorCode;
|
DWORD dw = (DWORD) errorCode;
|
||||||
|
|
||||||
FormatMessage(
|
FormatMessage(
|
||||||
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
|
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
|
||||||
| FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw,
|
| FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw,
|
||||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0,
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0,
|
||||||
NULL );
|
NULL );
|
||||||
|
|
||||||
lpDisplayBuf = (LPVOID) LocalAlloc(LMEM_ZEROINIT,
|
lpDisplayBuf = (LPVOID) LocalAlloc(LMEM_ZEROINIT,
|
||||||
(lstrlen((LPCTSTR) lpMsgBuf) + 40) * sizeof(TCHAR));
|
(lstrlen((LPCTSTR) lpMsgBuf) + 40) * sizeof(TCHAR));
|
||||||
StringCchPrintf((LPTSTR) lpDisplayBuf,
|
StringCchPrintf((LPTSTR) lpDisplayBuf,
|
||||||
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
|
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
|
||||||
TEXT("Failed with error %d: %s"), dw, lpMsgBuf);
|
TEXT("Failed with error %d: %s"), dw, lpMsgBuf);
|
||||||
return (jstring) (*env)->NewStringUTF(env, lpDisplayBuf);
|
return (jstring) (*env)->NewStringUTF(env, lpDisplayBuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT void JNICALL Java_pipe_Pipe_Print(JNIEnv *env, jclass className,
|
JNIEXPORT void JNICALL Java_pipe_Pipe_Print(JNIEnv *env, jclass className,
|
||||||
jstring lpMsgBuf) {
|
jstring lpMsgBuf) {
|
||||||
const jbyte *str;
|
const jbyte *str;
|
||||||
str = (*env)->GetStringUTFChars(env, lpMsgBuf, NULL );
|
str = (*env)->GetStringUTFChars(env, lpMsgBuf, NULL );
|
||||||
if (str == NULL )
|
if (str == NULL )
|
||||||
return;
|
return;
|
||||||
printf("Native: %s\n", str);
|
printf("Native: %s\n", str);
|
||||||
(*env)->ReleaseStringUTFChars(env, lpMsgBuf, str);
|
(*env)->ReleaseStringUTFChars(env, lpMsgBuf, str);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,51 +6,51 @@ import com.github.boukefalos.jlibloader.Native;
|
|||||||
* @author Vikram S Khatri vikram.khatri@us.ibm.com
|
* @author Vikram S Khatri vikram.khatri@us.ibm.com
|
||||||
*/
|
*/
|
||||||
public class Pipe {
|
public class Pipe {
|
||||||
static final int ERROR_PIPE_CONNECTED = 535;
|
static final int ERROR_PIPE_CONNECTED = 535;
|
||||||
static final int ERROR_BROKEN_PIPE = 109;
|
static final int ERROR_BROKEN_PIPE = 109;
|
||||||
static final int PIPE_ACCESS_DUPLEX = 0x00000003;
|
static final int PIPE_ACCESS_DUPLEX = 0x00000003;
|
||||||
static final int PIPE_WAIT = 0x00000000;
|
static final int PIPE_WAIT = 0x00000000;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
Native.load("com.github.boukefalos", "jlibpipe");
|
Native.load("com.github.boukefalos", "jlibpipe");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final native int CreateNamedPipe(
|
public static final native int CreateNamedPipe(
|
||||||
String pipeName,
|
String pipeName,
|
||||||
int ppenMode,
|
int ppenMode,
|
||||||
int pipeMode,
|
int pipeMode,
|
||||||
int maxInstances,
|
int maxInstances,
|
||||||
int outBufferSize,
|
int outBufferSize,
|
||||||
int inBufferSize,
|
int inBufferSize,
|
||||||
int defaultTimeOut,
|
int defaultTimeOut,
|
||||||
int securityAttributes);
|
int securityAttributes);
|
||||||
|
|
||||||
public static final native boolean ConnectNamedPipe(int namedPipeHandle, int overlapped);
|
public static final native boolean ConnectNamedPipe(int namedPipeHandle, int overlapped);
|
||||||
|
|
||||||
public static final native int GetLastError();
|
public static final native int GetLastError();
|
||||||
|
|
||||||
public static final native boolean CloseHandle(int bbject);
|
public static final native boolean CloseHandle(int bbject);
|
||||||
|
|
||||||
public static final native byte[] ReadFile(int file, int numberOfBytesToRead);
|
public static final native byte[] ReadFile(int file, int numberOfBytesToRead);
|
||||||
|
|
||||||
public static final native int WriteFile(int file, byte[] buffer, int numberOfBytesToWrite);
|
public static final native int WriteFile(int file, byte[] buffer, int numberOfBytesToWrite);
|
||||||
|
|
||||||
public static final native boolean FlushFileBuffers(int file);
|
public static final native boolean FlushFileBuffers(int file);
|
||||||
|
|
||||||
public static final native boolean DisconnectNamedPipe(int namedPipeHandle);
|
public static final native boolean DisconnectNamedPipe(int namedPipeHandle);
|
||||||
|
|
||||||
public static final native int CreateFile(
|
public static final native int CreateFile(
|
||||||
String fileName,
|
String fileName,
|
||||||
int desiredAccess,
|
int desiredAccess,
|
||||||
int shareMode,
|
int shareMode,
|
||||||
int securityAttributes,
|
int securityAttributes,
|
||||||
int creationDisposition,
|
int creationDisposition,
|
||||||
int flagsAndAttributes,
|
int flagsAndAttributes,
|
||||||
int templateFile);
|
int templateFile);
|
||||||
|
|
||||||
public static final native boolean WaitNamedPipe(String namedPipeName, int timeOut);
|
public static final native boolean WaitNamedPipe(String namedPipeName, int timeOut);
|
||||||
|
|
||||||
public static final native String FormatMessage(int errorCode);
|
public static final native String FormatMessage(int errorCode);
|
||||||
|
|
||||||
public static final native void Print(String message);
|
public static final native void Print(String message);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,22 +3,22 @@ package pipe;
|
|||||||
import java.io.RandomAccessFile;
|
import java.io.RandomAccessFile;
|
||||||
|
|
||||||
public class Client {
|
public class Client {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try {
|
try {
|
||||||
// Connect to the pipe
|
// Connect to the pipe
|
||||||
RandomAccessFile pipe = new RandomAccessFile("\\\\.\\pipe\\detest", "rw");
|
RandomAccessFile pipe = new RandomAccessFile("\\\\.\\pipe\\detest", "rw");
|
||||||
String echoText = "Hello word\n";
|
String echoText = "Hello word\n";
|
||||||
|
|
||||||
// write to pipe
|
// write to pipe
|
||||||
pipe.write(echoText.getBytes());
|
pipe.write(echoText.getBytes());
|
||||||
|
|
||||||
// read response
|
// read response
|
||||||
String echoResponse = pipe.readLine();
|
String echoResponse = pipe.readLine();
|
||||||
System.out.println(echoResponse);
|
System.out.println(echoResponse);
|
||||||
pipe.close();
|
pipe.close();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,81 +6,81 @@ import java.io.InputStream;
|
|||||||
|
|
||||||
public class TestPipe {
|
public class TestPipe {
|
||||||
|
|
||||||
private int namedPipeHandle;
|
private int namedPipeHandle;
|
||||||
private String pipeName, srcFile;
|
private String pipeName, srcFile;
|
||||||
private int pipeBuffer = 131072, fileBuffer = 8192;
|
private int pipeBuffer = 131072, fileBuffer = 8192;
|
||||||
|
|
||||||
public TestPipe(String pipeName, String srcFile) {
|
public TestPipe(String pipeName, String srcFile) {
|
||||||
this.pipeName = pipeName;
|
this.pipeName = pipeName;
|
||||||
this.srcFile = srcFile;
|
this.srcFile = srcFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void log(String message) {
|
private void log(String message) {
|
||||||
System.out.println(message);
|
System.out.println(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean createPipe() {
|
private boolean createPipe() {
|
||||||
namedPipeHandle = Pipe.CreateNamedPipe(
|
namedPipeHandle = Pipe.CreateNamedPipe(
|
||||||
pipeName,
|
pipeName,
|
||||||
Pipe.PIPE_ACCESS_DUPLEX,
|
Pipe.PIPE_ACCESS_DUPLEX,
|
||||||
Pipe.PIPE_WAIT,
|
Pipe.PIPE_WAIT,
|
||||||
5,
|
5,
|
||||||
pipeBuffer,
|
pipeBuffer,
|
||||||
pipeBuffer,
|
pipeBuffer,
|
||||||
0xffffffff,
|
0xffffffff,
|
||||||
0);
|
0);
|
||||||
if (namedPipeHandle == -1) {
|
if (namedPipeHandle == -1) {
|
||||||
log("CreateNamedPipe failed for " + pipeName + " for error Message " + Pipe.FormatMessage(Pipe.GetLastError()));
|
log("CreateNamedPipe failed for " + pipeName + " for error Message " + Pipe.FormatMessage(Pipe.GetLastError()));
|
||||||
} else {
|
} else {
|
||||||
log("Named Pipe " + pipeName + " created successfully Handle=" + namedPipeHandle);
|
log("Named Pipe " + pipeName + " created successfully Handle=" + namedPipeHandle);
|
||||||
}
|
}
|
||||||
return namedPipeHandle != -1;
|
return namedPipeHandle != -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean connectToPipe() {
|
private boolean connectToPipe() {
|
||||||
log("Waiting for a client to connect to pipe " + pipeName);
|
log("Waiting for a client to connect to pipe " + pipeName);
|
||||||
boolean connected = Pipe.ConnectNamedPipe(namedPipeHandle, 0);
|
boolean connected = Pipe.ConnectNamedPipe(namedPipeHandle, 0);
|
||||||
if (!connected) {
|
if (!connected) {
|
||||||
int lastError = Pipe.GetLastError();
|
int lastError = Pipe.GetLastError();
|
||||||
if (lastError == Pipe.ERROR_PIPE_CONNECTED)
|
if (lastError == Pipe.ERROR_PIPE_CONNECTED)
|
||||||
connected = true;
|
connected = true;
|
||||||
}
|
}
|
||||||
log((connected ? "Connected to the pipe " : "Falied to connect to the pipe ") + pipeName);
|
log((connected ? "Connected to the pipe " : "Falied to connect to the pipe ") + pipeName);
|
||||||
return connected;
|
return connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void runPipe() {
|
public void runPipe() {
|
||||||
if (createPipe() && connectToPipe()) {
|
if (createPipe() && connectToPipe()) {
|
||||||
log("Client connected.");
|
log("Client connected.");
|
||||||
try {
|
try {
|
||||||
File f1 = new File(this.srcFile);
|
File f1 = new File(this.srcFile);
|
||||||
InputStream in = new FileInputStream(f1);
|
InputStream in = new FileInputStream(f1);
|
||||||
log("Sending data to the pipe");
|
log("Sending data to the pipe");
|
||||||
byte[] buf = new byte[fileBuffer];
|
byte[] buf = new byte[fileBuffer];
|
||||||
int len, bytesWritten;
|
int len, bytesWritten;
|
||||||
while ((len = in.read(buf)) > 0) {
|
while ((len = in.read(buf)) > 0) {
|
||||||
bytesWritten = Pipe.WriteFile(namedPipeHandle, buf, len);
|
bytesWritten = Pipe.WriteFile(namedPipeHandle, buf, len);
|
||||||
log("Sent " + len + "/" + bytesWritten + " bytes to the pipe");
|
log("Sent " + len + "/" + bytesWritten + " bytes to the pipe");
|
||||||
if (bytesWritten == -1) {
|
if (bytesWritten == -1) {
|
||||||
int errorNumber = Pipe.GetLastError();
|
int errorNumber = Pipe.GetLastError();
|
||||||
log("Error Writing to pipe " + Pipe.FormatMessage(errorNumber));
|
log("Error Writing to pipe " + Pipe.FormatMessage(errorNumber));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
in.close();
|
in.close();
|
||||||
Pipe.FlushFileBuffers(namedPipeHandle);
|
Pipe.FlushFileBuffers(namedPipeHandle);
|
||||||
Pipe.CloseHandle(namedPipeHandle);
|
Pipe.CloseHandle(namedPipeHandle);
|
||||||
Pipe.DisconnectNamedPipe(namedPipeHandle);
|
Pipe.DisconnectNamedPipe(namedPipeHandle);
|
||||||
log("Writing to the pipe completed.");
|
log("Writing to the pipe completed.");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String pipeName = "\\\\.\\pipe\\detest";
|
String pipeName = "\\\\.\\pipe\\detest";
|
||||||
String fileName = "C:\\Users\\Rik\\Music\\Artists\\+44\\When Your Heart Stops Beating\\+44 - 155.mp3";
|
String fileName = "C:\\Users\\Rik\\Music\\Artists\\+44\\When Your Heart Stops Beating\\+44 - 155.mp3";
|
||||||
TestPipe testPipe = new TestPipe(pipeName, fileName);
|
TestPipe testPipe = new TestPipe(pipeName, fileName);
|
||||||
testPipe.runPipe();
|
testPipe.runPipe();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user