mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-05-13 21:21:03 +00:00
shared memory
This commit is contained in:
parent
1bd57f6fef
commit
fcd53969f1
Binary file not shown.
Binary file not shown.
|
@ -29,9 +29,8 @@ namespace WelsonJS
|
||||||
{
|
{
|
||||||
private IntPtr hFile;
|
private IntPtr hFile;
|
||||||
private IntPtr hFileMappingObject;
|
private IntPtr hFileMappingObject;
|
||||||
private string _lpName;
|
private string lpName;
|
||||||
|
private static Dictionary<string, NamedSharedMemory> memDict;
|
||||||
public static Dictionary<string, NamedSharedMemory> Cached;
|
|
||||||
|
|
||||||
[Flags]
|
[Flags]
|
||||||
public enum FileProtection : uint
|
public enum FileProtection : uint
|
||||||
|
@ -63,7 +62,7 @@ namespace WelsonJS
|
||||||
FILE_MAP_ALL_ACCESS = 0xF001F
|
FILE_MAP_ALL_ACCESS = 0xF001F
|
||||||
}
|
}
|
||||||
|
|
||||||
public class FileMappingNative
|
public static class FileMappingNative
|
||||||
{
|
{
|
||||||
public const int INVALID_HANDLE_VALUE = -1;
|
public const int INVALID_HANDLE_VALUE = -1;
|
||||||
|
|
||||||
|
@ -90,24 +89,31 @@ namespace WelsonJS
|
||||||
|
|
||||||
public NamedSharedMemory(string lpName)
|
public NamedSharedMemory(string lpName)
|
||||||
{
|
{
|
||||||
Open(lpName);
|
this.lpName = lpName;
|
||||||
|
Open();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Open(string lpName)
|
public bool Open()
|
||||||
{
|
{
|
||||||
_lpName = lpName;
|
if (memDict.ContainsKey(lpName))
|
||||||
|
{
|
||||||
|
hFile = memDict[lpName].hFile;
|
||||||
|
hFileMappingObject = memDict[lpName].hFileMappingObject;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if (!Cached.ContainsKey(_lpName))
|
try
|
||||||
{
|
{
|
||||||
hFile = FileMappingNative.CreateFileMapping((IntPtr)(-1), IntPtr.Zero, FileProtection.PAGE_READWRITE, 0u, 1024u, _lpName);
|
hFile = FileMappingNative.CreateFileMapping((IntPtr)(-1), IntPtr.Zero, FileProtection.PAGE_READWRITE, 0u, 1024u, lpName);
|
||||||
hFileMappingObject = FileMappingNative.MapViewOfFile(hFile, FileMapAccess.FILE_MAP_ALL_ACCESS, 0u, 0u, 1024u);
|
hFileMappingObject = FileMappingNative.MapViewOfFile(hFile, FileMapAccess.FILE_MAP_ALL_ACCESS, 0u, 0u, 1024u);
|
||||||
Cached.Add(_lpName, this);
|
memDict.Add(lpName, this);
|
||||||
}
|
}
|
||||||
else
|
catch
|
||||||
{
|
{
|
||||||
hFile = IntPtr.Zero;
|
return false;
|
||||||
hFileMappingObject = IntPtr.Zero;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return IsInitialized();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsInitialized()
|
public bool IsInitialized()
|
||||||
|
@ -120,37 +126,59 @@ namespace WelsonJS
|
||||||
return Marshal.PtrToStringAnsi(hFileMappingObject);
|
return Marshal.PtrToStringAnsi(hFileMappingObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void WriteText(string text, int size = 1024)
|
public bool WriteText(string text, int size = 1024)
|
||||||
{
|
{
|
||||||
byte[] bytes = Encoding.ASCII.GetBytes(text);
|
try
|
||||||
byte[] array = new byte[size + 1];
|
{
|
||||||
Array.Copy(bytes, array, bytes.Length);
|
byte[] bytes = Encoding.ASCII.GetBytes(text);
|
||||||
Marshal.Copy(array, 0, hFileMappingObject, size);
|
byte[] array = new byte[size + 1];
|
||||||
|
Array.Copy(bytes, array, bytes.Length);
|
||||||
|
Marshal.Copy(array, 0, hFileMappingObject, size);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Clear(int size = 1024)
|
public bool Clear(int size = 1024)
|
||||||
{
|
{
|
||||||
Marshal.Copy(new byte[size + 1], 0, hFileMappingObject, size);
|
try
|
||||||
|
{
|
||||||
|
Marshal.Copy(new byte[size + 1], 0, hFileMappingObject, size);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Close()
|
public bool Close()
|
||||||
{
|
{
|
||||||
if (hFileMappingObject != IntPtr.Zero)
|
try
|
||||||
{
|
{
|
||||||
FileMappingNative.UnmapViewOfFile(hFileMappingObject);
|
if (hFileMappingObject != IntPtr.Zero)
|
||||||
hFileMappingObject = IntPtr.Zero;
|
{
|
||||||
|
FileMappingNative.UnmapViewOfFile(hFileMappingObject);
|
||||||
|
hFileMappingObject = IntPtr.Zero;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hFile != IntPtr.Zero)
|
||||||
|
{
|
||||||
|
FileMappingNative.CloseHandle(hFile);
|
||||||
|
hFile = IntPtr.Zero;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hFile != IntPtr.Zero)
|
return true;
|
||||||
{
|
|
||||||
FileMappingNative.CloseHandle(hFile);
|
|
||||||
hFile = IntPtr.Zero;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Cached.ContainsKey(_lpName))
|
|
||||||
{
|
|
||||||
Cached.Remove(_lpName);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -144,73 +144,53 @@ namespace WelsonJS
|
||||||
}
|
}
|
||||||
|
|
||||||
// [Toolkit] Access to a shared memory #96
|
// [Toolkit] Access to a shared memory #96
|
||||||
|
[ComVisible(true)]
|
||||||
// [ComVisible(false)]
|
public bool WriteTextToSharedMemory(string lpName, string text)
|
||||||
public NamedSharedMemory GetSharedMemory(string lpName)
|
|
||||||
{
|
{
|
||||||
if (NamedSharedMemory.Cached.ContainsKey(lpName))
|
NamedSharedMemory mem = new NamedSharedMemory(lpName);
|
||||||
|
if (mem.IsInitialized())
|
||||||
{
|
{
|
||||||
NamedSharedMemory sharedMemory = NamedSharedMemory.Cached[lpName];
|
return mem.WriteText(text);
|
||||||
if (sharedMemory.IsInitialized())
|
|
||||||
{
|
|
||||||
return sharedMemory;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
[ComVisible(true)]
|
|
||||||
public bool OpenNamedSharedMemory(string lpName)
|
|
||||||
{
|
|
||||||
NamedSharedMemory sharedMemory = new NamedSharedMemory(lpName);
|
|
||||||
return sharedMemory.IsInitialized();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
[ComVisible(true)]
|
|
||||||
public void CloseNamedSharedMemory(string lpName)
|
|
||||||
{
|
|
||||||
NamedSharedMemory sharedMemory = GetSharedMemory(lpName);
|
|
||||||
if (sharedMemory != null)
|
|
||||||
{
|
|
||||||
sharedMemory.Close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[ComVisible(true)]
|
[ComVisible(true)]
|
||||||
public string ReadTextFromSharedMemory(string lpName)
|
public string ReadTextFromSharedMemory(string lpName)
|
||||||
{
|
{
|
||||||
NamedSharedMemory sharedMemory = GetSharedMemory(lpName);
|
NamedSharedMemory mem = new NamedSharedMemory(lpName);
|
||||||
if (sharedMemory != null)
|
if (mem.IsInitialized()) {
|
||||||
{
|
return mem.ReadText();
|
||||||
return sharedMemory.ReadText();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
[ComVisible(true)]
|
[ComVisible(true)]
|
||||||
public void WriteTextToSharedMemory(string lpName, string text)
|
public bool ClearSharedMemory(string lpName)
|
||||||
{
|
{
|
||||||
NamedSharedMemory sharedMemory = GetSharedMemory(lpName);
|
NamedSharedMemory mem = new NamedSharedMemory(lpName);
|
||||||
if (sharedMemory != null)
|
if (mem.IsInitialized())
|
||||||
{
|
{
|
||||||
sharedMemory.WriteText(text);
|
return mem.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
[ComVisible(true)]
|
[ComVisible(true)]
|
||||||
public void ClearSharedMemory(string lpName)
|
public bool CloseSharedMemory(string lpName)
|
||||||
{
|
{
|
||||||
NamedSharedMemory sharedMemory = GetSharedMemory(lpName);
|
NamedSharedMemory mem = new NamedSharedMemory(lpName);
|
||||||
if (sharedMemory != null)
|
if (mem.IsInitialized())
|
||||||
{
|
{
|
||||||
sharedMemory.Clear();
|
return mem.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
[ComVisible(true)]
|
|
||||||
public void CompressLZ77(string input)
|
public void CompressLZ77(string input)
|
||||||
{
|
{
|
||||||
Compression.LZ77.Compress(input);
|
Compression.LZ77.Compress(input);
|
||||||
|
|
|
@ -63,26 +63,25 @@ function prompt(message, _default) {
|
||||||
|
|
||||||
// [Toolkit] Access to a shared memory #96
|
// [Toolkit] Access to a shared memory #96
|
||||||
function NamedSharedMemory(name) {
|
function NamedSharedMemory(name) {
|
||||||
var Toolkit = create().getInterface();
|
var _interface = create().getInterface();
|
||||||
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.isInitialized = false;
|
|
||||||
|
|
||||||
this.open = function() {
|
|
||||||
this.isInitialized = Toolkit.OpenNamedSharedMemory(this.name);
|
|
||||||
};
|
|
||||||
|
|
||||||
this.readText = function() {
|
|
||||||
return Toolkit.ReadTextFromSharedMemory(this.name);
|
|
||||||
};
|
|
||||||
|
|
||||||
this.writeText = function(text) {
|
this.writeText = function(text) {
|
||||||
Toolkit.WriteTextToSharedMemory(this.name, text);
|
return _interface.WriteTextToSharedMemory(this.name, text);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.clear = function() {
|
this.readText = function() {
|
||||||
Toolkit.ClearSharedMemory(this.name);
|
return _interface.ReadTextFromSharedMemory(this.name);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.clear = function() {
|
||||||
|
return _interface.ClearSharedMemory(this.name);
|
||||||
|
};
|
||||||
|
|
||||||
|
this.close = function() {
|
||||||
|
return _interface.CloseSharedMemory(this.name);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.create = create;
|
exports.create = create;
|
||||||
|
@ -95,7 +94,7 @@ exports.confirm = confirm;
|
||||||
exports.prompt = prompt;
|
exports.prompt = prompt;
|
||||||
exports.NamedSharedMemory = NamedSharedMemory;
|
exports.NamedSharedMemory = NamedSharedMemory;
|
||||||
|
|
||||||
exports.VERSIONINFO = "WelsonJS.Toolkit Native API version 0.3.2";
|
exports.VERSIONINFO = "WelsonJS.Toolkit Native API version 0.3.3";
|
||||||
exports.AUTHOR = "abuse@catswords.net";
|
exports.AUTHOR = "abuse@catswords.net";
|
||||||
exports.global = global;
|
exports.global = global;
|
||||||
exports.require = global.require;
|
exports.require = global.require;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user