mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-02-06 15:04:58 +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 hFileMappingObject;
|
||||
private string _lpName;
|
||||
|
||||
public static Dictionary<string, NamedSharedMemory> Cached;
|
||||
private string lpName;
|
||||
private static Dictionary<string, NamedSharedMemory> memDict;
|
||||
|
||||
[Flags]
|
||||
public enum FileProtection : uint
|
||||
|
@ -63,7 +62,7 @@ namespace WelsonJS
|
|||
FILE_MAP_ALL_ACCESS = 0xF001F
|
||||
}
|
||||
|
||||
public class FileMappingNative
|
||||
public static class FileMappingNative
|
||||
{
|
||||
public const int INVALID_HANDLE_VALUE = -1;
|
||||
|
||||
|
@ -90,24 +89,31 @@ namespace WelsonJS
|
|||
|
||||
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);
|
||||
Cached.Add(_lpName, this);
|
||||
memDict.Add(lpName, this);
|
||||
}
|
||||
else
|
||||
catch
|
||||
{
|
||||
hFile = IntPtr.Zero;
|
||||
hFileMappingObject = IntPtr.Zero;
|
||||
return false;
|
||||
}
|
||||
|
||||
return IsInitialized();
|
||||
}
|
||||
|
||||
public bool IsInitialized()
|
||||
|
@ -120,37 +126,59 @@ namespace WelsonJS
|
|||
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);
|
||||
byte[] array = new byte[size + 1];
|
||||
Array.Copy(bytes, array, bytes.Length);
|
||||
Marshal.Copy(array, 0, hFileMappingObject, size);
|
||||
try
|
||||
{
|
||||
byte[] bytes = Encoding.ASCII.GetBytes(text);
|
||||
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);
|
||||
hFileMappingObject = IntPtr.Zero;
|
||||
if (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)
|
||||
{
|
||||
FileMappingNative.CloseHandle(hFile);
|
||||
hFile = IntPtr.Zero;
|
||||
}
|
||||
|
||||
if (Cached.ContainsKey(_lpName))
|
||||
{
|
||||
Cached.Remove(_lpName);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -144,73 +144,53 @@ namespace WelsonJS
|
|||
}
|
||||
|
||||
// [Toolkit] Access to a shared memory #96
|
||||
|
||||
// [ComVisible(false)]
|
||||
public NamedSharedMemory GetSharedMemory(string lpName)
|
||||
[ComVisible(true)]
|
||||
public bool WriteTextToSharedMemory(string lpName, string text)
|
||||
{
|
||||
if (NamedSharedMemory.Cached.ContainsKey(lpName))
|
||||
NamedSharedMemory mem = new NamedSharedMemory(lpName);
|
||||
if (mem.IsInitialized())
|
||||
{
|
||||
NamedSharedMemory sharedMemory = NamedSharedMemory.Cached[lpName];
|
||||
if (sharedMemory.IsInitialized())
|
||||
{
|
||||
return sharedMemory;
|
||||
}
|
||||
return mem.WriteText(text);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
[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();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
[ComVisible(true)]
|
||||
public string ReadTextFromSharedMemory(string lpName)
|
||||
{
|
||||
NamedSharedMemory sharedMemory = GetSharedMemory(lpName);
|
||||
if (sharedMemory != null)
|
||||
{
|
||||
return sharedMemory.ReadText();
|
||||
NamedSharedMemory mem = new NamedSharedMemory(lpName);
|
||||
if (mem.IsInitialized()) {
|
||||
return mem.ReadText();
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
[ComVisible(true)]
|
||||
public void WriteTextToSharedMemory(string lpName, string text)
|
||||
public bool ClearSharedMemory(string lpName)
|
||||
{
|
||||
NamedSharedMemory sharedMemory = GetSharedMemory(lpName);
|
||||
if (sharedMemory != null)
|
||||
NamedSharedMemory mem = new NamedSharedMemory(lpName);
|
||||
if (mem.IsInitialized())
|
||||
{
|
||||
sharedMemory.WriteText(text);
|
||||
return mem.Clear();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
[ComVisible(true)]
|
||||
public void ClearSharedMemory(string lpName)
|
||||
public bool CloseSharedMemory(string lpName)
|
||||
{
|
||||
NamedSharedMemory sharedMemory = GetSharedMemory(lpName);
|
||||
if (sharedMemory != null)
|
||||
NamedSharedMemory mem = new NamedSharedMemory(lpName);
|
||||
if (mem.IsInitialized())
|
||||
{
|
||||
sharedMemory.Clear();
|
||||
return mem.Close();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
[ComVisible(true)]
|
||||
public void CompressLZ77(string input)
|
||||
{
|
||||
Compression.LZ77.Compress(input);
|
||||
|
|
|
@ -63,26 +63,25 @@ function prompt(message, _default) {
|
|||
|
||||
// [Toolkit] Access to a shared memory #96
|
||||
function NamedSharedMemory(name) {
|
||||
var Toolkit = create().getInterface();
|
||||
var _interface = create().getInterface();
|
||||
|
||||
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) {
|
||||
Toolkit.WriteTextToSharedMemory(this.name, text);
|
||||
return _interface.WriteTextToSharedMemory(this.name, text);
|
||||
};
|
||||
|
||||
this.clear = function() {
|
||||
Toolkit.ClearSharedMemory(this.name);
|
||||
this.readText = function() {
|
||||
return _interface.ReadTextFromSharedMemory(this.name);
|
||||
};
|
||||
|
||||
this.clear = function() {
|
||||
return _interface.ClearSharedMemory(this.name);
|
||||
};
|
||||
|
||||
this.close = function() {
|
||||
return _interface.CloseSharedMemory(this.name);
|
||||
};
|
||||
}
|
||||
|
||||
exports.create = create;
|
||||
|
@ -95,7 +94,7 @@ exports.confirm = confirm;
|
|||
exports.prompt = prompt;
|
||||
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.global = global;
|
||||
exports.require = global.require;
|
||||
|
|
Loading…
Reference in New Issue
Block a user