1. 增加UDP消息通道

2. 增加主程序配置项
3. 格式化代码
This commit is contained in:
hzhuangxin01 2019-02-19 12:02:14 +08:00
parent e34633edea
commit 3aace68789
12 changed files with 2970 additions and 2846 deletions

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
</configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>

View File

@ -11,12 +11,12 @@ namespace GeneratorCode.Configure
{
public static class NConfig
{
private static string _cfgFileName = @".\config.ini";
private static IniData _cfgData = null;
public delegate void ConfigChangedHandle();
private static readonly string _cfgFileName = @".\config.ini";
private static IniData _cfgData;
public static event ConfigChangedHandle OnConfigChanged;
private static void ConfigChanged()
@ -35,7 +35,7 @@ namespace GeneratorCode.Configure
ws.WriteLine("; 应用程序配置文件\n");
}
FileIniDataParser iniPraser = new FileIniDataParser();
var iniPraser = new FileIniDataParser();
iniPraser.Parser.Configuration.CommentString = ";";
_cfgData = iniPraser.ReadFile(_cfgFileName);
//Trace.WriteLine(_cfgData);
@ -50,17 +50,17 @@ namespace GeneratorCode.Configure
{
var cfgTask = new List<string>();
var appPath = Process.GetCurrentProcess().MainModule.FileName;
//Trace.WriteLine("Application: " + appPath);
//Trace.WriteLine("Directory: " + Path.GetDirectoryName(appPath));
//Trace.WriteLine("File: " + Path.GetFileName(appPath));
var fw = new FileSystemWatcher()
var fw = new FileSystemWatcher
{
NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite,
Filter = _cfgFileName,
Path = Path.GetDirectoryName(appPath),
IncludeSubdirectories = false,
IncludeSubdirectories = false
};
fw.Changed += (sender, e) =>
@ -99,20 +99,17 @@ namespace GeneratorCode.Configure
{
var ret = defValue;
if (_cfgData.Equals(null))
{
return ret;
}
if (_cfgData.Equals(null)) return ret;
try
{
ret = (T) Convert.ChangeType(_cfgData[secName][keyName], typeof(T));
}
catch(Exception e)
catch (Exception e)
{
Trace.WriteLine("[" + _cfgData[secName][keyName] + "] :" + e.Message);
ret = defValue;
throw(e);
throw e;
}
return ret;

View File

@ -58,7 +58,7 @@
<ItemGroup>
<Compile Include="Configure\NConfig.cs" />
<Compile Include="Logs\NLog.cs" />
<Compile Include="Program.cs" />
<Compile Include="MainCode.cs" />
<Compile Include="Properties\Annotations.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TmatrixSDK\OIDPublishImageGenerator.cs" />

View File

@ -29,10 +29,9 @@ namespace GeneratorCode.Logs
public class NLogConfig
{
private object _cfgLock = new object();
public delegate void LogCfgChangedHandle();
public static event LogCfgChangedHandle OnLogCfgChanged;
private readonly object _cfgLock = new object();
public NLogConfig()
{
@ -40,11 +39,6 @@ namespace GeneratorCode.Logs
NConfig.OnConfigChanged += () => { LogCfgChanged(); };
}
protected static void LogCfgChanged()
{
OnLogCfgChanged?.Invoke();
}
public bool LogEnable { get; set; }
public NLogLevel LogLevel { get; set; }
public NLogLevel DefaultLevel { get; set; }
@ -78,13 +72,20 @@ namespace GeneratorCode.Logs
public bool RoolbackFile { get; set; }
public int MaxFileNum { get; set; }
public static event LogCfgChangedHandle OnLogCfgChanged;
protected static void LogCfgChanged()
{
OnLogCfgChanged?.Invoke();
}
public void LogConfig()
{
lock (_cfgLock)
{
LogEnable = NConfig.GetCfgValue("LogGlobal", "LogEnable", false);
LogLevel = (NLogLevel)NConfig.GetCfgValue("LogGlobal", "LogLevel", (int)NLogLevel.MaxLevel);
DefaultLevel = (NLogLevel)NConfig.GetCfgValue("LogGlobal", "DefaultLogLevel", (int)NLogLevel.Info);
LogLevel = (NLogLevel) NConfig.GetCfgValue("LogGlobal", "LogLevel", (int) NLogLevel.MaxLevel);
DefaultLevel = (NLogLevel) NConfig.GetCfgValue("LogGlobal", "DefaultLogLevel", (int) NLogLevel.Info);
AsyncMode = NConfig.GetCfgValue("LogGlobal", "AsyncMode", false);
ForceNewLine = NConfig.GetCfgValue("LogGlobal", "AutoForceNewLine", false);
@ -107,7 +108,8 @@ namespace GeneratorCode.Logs
if (MaxItemsCache == 0) MaxItemsCache = int.MaxValue;
CacheMode = (CacheOptMode)NConfig.GetCfgValue("AsyncLogSetting", "CacheFullOpts", (int)CacheOptMode.Drop);
CacheMode = (CacheOptMode) NConfig.GetCfgValue("AsyncLogSetting", "CacheFullOpts",
(int) CacheOptMode.Drop);
NumOutItems = NConfig.GetCfgValue("AsyncLogSetting", "NumItemsOutEachTime", 10);
}
@ -118,14 +120,9 @@ namespace GeneratorCode.Logs
DirPath = NConfig.GetCfgValue("FileLogSetting", "Path", @"");
if (DirPath == "" || !Directory.Exists(DirPath) || DirPath == @".\")
{
DirPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\";
}
if (!Directory.Exists(DirPath))
{
Directory.CreateDirectory(DirPath);
}
if (!Directory.Exists(DirPath)) Directory.CreateDirectory(DirPath);
FileNamePre = NConfig.GetCfgValue("FileLogSetting", "FileNamePrefix", "");
EnSplitLog = NConfig.GetCfgValue("FileLogSetting", "AutoSplitFile", true);
@ -152,13 +149,9 @@ namespace GeneratorCode.Logs
DateTime? dt = null)
{
if (dt == null)
{
LogStamp = DateTime.Now;
}
else
{
LogStamp = (DateTime)dt;
}
LogStamp = (DateTime) dt;
LogLevel = level;
LogContent = logContent;
@ -181,8 +174,8 @@ namespace GeneratorCode.Logs
private static readonly ConcurrentQueue<NLogItem> _logItemCollection = new ConcurrentQueue<NLogItem>();
private static readonly object _logOutputLock = new object();
private static string _logFileName = "";
private static uint _logFileNum = 0;
private static StreamWriter _logSw = null;
private static uint _logFileNum;
private static StreamWriter _logSw;
private static void CreateLogFileHead()
{
@ -190,7 +183,7 @@ namespace GeneratorCode.Logs
_logSw?.WriteLine("CreateTime: " + string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now));
_logSw?.WriteLine("Program: " + Process.GetCurrentProcess().MainModule.FileName);
_logSw?.WriteLine("PID: " + Process.GetCurrentProcess().Id);
_logSw?.WriteLine("Split Count: " + (_logFileNum - 1).ToString());
_logSw?.WriteLine("Split Count: " + (_logFileNum - 1));
_logSw?.WriteLine("--------------------------------------------------");
_logSw?.Flush();
}
@ -215,12 +208,8 @@ namespace GeneratorCode.Logs
_logFileName += ".log";
if (File.Exists(_logFileName))
{
if (_logCfg.EnSplitLog)
{
File.Delete(_logFileName);
}
}
_logSw = new StreamWriter(_logFileName, true);
CreateLogFileHead();
@ -261,43 +250,33 @@ namespace GeneratorCode.Logs
var tolOut = Math.Min(_logCfg.NumOutItems, _logItemCollection.Count);
foreach (var val in Enumerable.Range(1, tolOut))
{
if (_logItemCollection.TryDequeue(out var logItem))
{
LogOutput(logItem);
}
}
Thread.Sleep(_logCfg.SleepTime);
// 每秒执行一次维护工作
if ((++cnt % (1000 / _logCfg.SleepTime)) != 0)
{
continue;
}
if (++cnt % (1000 / _logCfg.SleepTime) != 0) continue;
_logSw?.Flush();
if (_logCfg.EnSplitLog)
{
bool isNeedSplit = false;
var isNeedSplit = false;
if (_logCfg.SplitByData && lastDt.Day != DateTime.Now.Day)
{
isNeedSplit = true;
}
else if (_logCfg.SplitBySize > 0 && (new FileInfo(_logFileName)).Length > _logCfg.SplitBySize * (1024 * 1024))
{
else if (_logCfg.SplitBySize > 0 &&
new FileInfo(_logFileName).Length > _logCfg.SplitBySize * 1024 * 1024)
isNeedSplit = true;
}
if (isNeedSplit)
{
_logSw?.Close();
_logSw?.Dispose();
_logSw = null;
string parttn = string.Format("*[{0:d3}]_{1}",
var parttn = string.Format("*[{0:d3}]_{1}",
Process.GetCurrentProcess().Id,
Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule.FileName));
@ -314,16 +293,17 @@ namespace GeneratorCode.Logs
parttn += $"_{_logFileNum:d3}";
_logFileNum = Convert.ToUInt32((_logFileNum + 1) % _logCfg.MaxFileNum);
}
_logFileName += ".log";
parttn += ".log";
foreach (var f in Directory.GetFiles(_logCfg.DirPath, parttn, SearchOption.TopDirectoryOnly))
foreach (var f in Directory.GetFiles(_logCfg.DirPath, parttn,
SearchOption.TopDirectoryOnly))
{
File.Delete(f);
Trace.WriteLine("Delect Rollback log: " + f);
}
_logSw = new StreamWriter(_logFileName, true);
CreateLogFileHead();
}
@ -352,58 +332,33 @@ namespace GeneratorCode.Logs
{
var msg = "";
if (_logCfg.ShowDate || _logCfg.ShowTime)
{
msg += "[";
}
if (_logCfg.ShowDate || _logCfg.ShowTime) msg += "[";
if (_logCfg.ShowDate)
{
msg += dt.ToString("yyyy-MM-dd");
if (_logCfg.ShowTime)
{
msg += " ";
}
if (_logCfg.ShowTime) msg += " ";
}
if (_logCfg.ShowTime)
{
msg += dt.ToString("HH:mm:ss");
if (_logCfg.ShowMSec)
{
msg += "." + dt.ToString("fff");
}
if (_logCfg.ShowMSec) msg += "." + dt.ToString("fff");
}
if (_logCfg.ShowDate || _logCfg.ShowTime)
{
msg += "]";
}
if (_logCfg.ShowDate || _logCfg.ShowTime) msg += "]";
if (_logCfg.ShowLevel)
{
msg += " [" + LogLevelToString(logLevel) + "] ";
}
if (_logCfg.ShowLevel) msg += " [" + LogLevelToString(logLevel) + "] ";
if (_logCfg.ShowCodeFile)
{
msg += "[" + Path.GetFileName(fileName) + "] ";
}
if (_logCfg.ShowCodeFile) msg += "[" + Path.GetFileName(fileName) + "] ";
if (_logCfg.ShowFunction)
{
msg += "- " + funName;
}
if (_logCfg.ShowFunction) msg += "- " + funName;
if (_logCfg.ShowCodeFile || _logCfg.ShowFunction)
{
if (_logCfg.ShowCodeLine)
{
msg += "(" + lineNo.ToString() + ")";
}
}
msg += "(" + lineNo + ")";
msg += ": " + logMsg;
@ -419,11 +374,8 @@ namespace GeneratorCode.Logs
logItem.CodeLine,
logItem.LogStamp);
if (_logCfg.ForceNewLine)
{
msg += Environment.NewLine;
}
if (_logCfg.ForceNewLine) msg += Environment.NewLine;
lock (_logOutputLock)
{
if (_logCfg.EnConsole) Console.Write(msg);
@ -436,10 +388,7 @@ namespace GeneratorCode.Logs
System.Diagnostics.Debug.WriteLine(msg);
}
if (_logCfg.EnFile)
{
_logSw?.Write(msg);
}
if (_logCfg.EnFile) _logSw?.Write(msg);
}
}
@ -457,10 +406,7 @@ namespace GeneratorCode.Logs
System.Diagnostics.Debug.WriteLine(logMsg);
}
if (_logCfg.EnFile)
{
_logSw?.Write(logMsg);
}
if (_logCfg.EnFile) _logSw?.Write(logMsg);
}
}
@ -475,15 +421,10 @@ namespace GeneratorCode.Logs
{
if (_logItemCollection.Count >= _logCfg.MaxItemsCache)
{
if (_logCfg.CacheMode == CacheOptMode.Drop)
{
return;
}
else
{
NLogItem val;
_logItemCollection.TryDequeue(out val);
}
if (_logCfg.CacheMode == CacheOptMode.Drop) return;
NLogItem val;
_logItemCollection.TryDequeue(out val);
}
var logItem = new NLogItem(_logCfg.DefaultLevel, logMsg, fileName, funName, lineNo, DateTime.Now);
@ -508,17 +449,12 @@ namespace GeneratorCode.Logs
{
if (_logItemCollection.Count >= _logCfg.MaxItemsCache)
{
if (_logCfg.CacheMode == CacheOptMode.Drop)
{
return;
}
else
{
NLogItem val;
_logItemCollection.TryDequeue(out val);
}
if (_logCfg.CacheMode == CacheOptMode.Drop) return;
NLogItem val;
_logItemCollection.TryDequeue(out val);
}
var logItem = new NLogItem(NLogLevel.Debug, logMsg, fileName, funName, lineNo, DateTime.Now);
_logItemCollection.Enqueue(logItem);
}

View File

@ -1,250 +1,303 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using GeneratorCode.Configure;
using GeneratorCode.Logs;
using Newtonsoft.Json;
using TmatrixLibrary;
namespace GeneratorCode
{
public class GeneratorParams
{
public GeneratorParams()
{
dpi = new[] { 0, 0, 0, 0 };
point_type = new[] { 0, 0, 0, 0 };
image_type = new[] { false, false, false, true };
StartPageID = 0;
key = "S0,O000,B0000,P000-255,D2018/12/31;CCAFBQMXYPOAOCIRK52S8QC8SO4A0AGA8Y";
//filePath = "E:\\NetEase\\轨迹笔\\Sample\\123.pdf"; //"C:\\Works\\pdf\\123.pdf";
filePath = "C:\\Works\\pdf\\123.pdf";
sessionId = "4BD5D923-47EA-4DEF-A1CD-9B85B515B191";
}
public int[] dpi { get; set; }
public int[] point_type { get; set; }
public bool[] image_type { get; set; }
public string key { get; set; }
public string filePath { get; set; }
public int StartPageID { get; set; }
public string sessionId { get; set; }
}
public class GenerCodeRet
{
public GenerCodeRet(string sId, int prg)
{
sessionId = sId;
progress = prg;
}
public string sessionId { get; set; }
public int progress { get; set; }
}
public class RspMessage
{
public RspMessage(string sId)
{
err = 0;
msg = "";
data = new GenerCodeRet(sId, 0);
}
public RspMessage(string sId, int prg)
{
err = 0;
msg = "";
data = new GenerCodeRet(sId, prg);
}
public int err { get; set; }
public string msg { get; set; }
public GenerCodeRet data { get; set; }
public string FormatRspMessage(int errCode, string readme, int prg)
{
var rsp = new RspMessage(data.sessionId, prg);
rsp.err = errCode;
rsp.msg = (Convert.ToBase64String(Encoding.Default.GetBytes(readme)));
return JsonConvert.SerializeObject(rsp);
}
public static int SendRspMessage(string msg)
{
NLog.Debug("Send: " + msg + Environment.NewLine);
Console.WriteLine(msg);
return 0;
}
public void ProgressMessage(int step)
{
var msgContent = FormatRspMessage(0, "Progress", step);
msg = JsonConvert.SerializeObject(msgContent);
SendRspMessage(msg);
}
}
internal class Program
{
private static int Main(string[] args)
{
NConfig.InitConfigure();
NLog.NLog_Init();
GeneratorParams inParams = null;
RspMessage rspMsg;
var tmObj = new TmatrixClass();
AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
{
//Trace.WriteLine("+++++++++++++++++++++++++++++++++++++++++++xajhuang1\n");
};
AppDomain.CurrentDomain.DomainUnload += (sender, e) =>
{
//Trace.WriteLine("+++++++++++++++++++++++++++++++++++++++++++xajhuang2\n");
};
AppDomain.CurrentDomain.ProcessExit += (sender, ev) =>
{
try
{
tmObj.TmatrixUninitialize();
}
catch (Exception e)
{
//var msg = rspMsg.FormatRspMessage(9, e.Message, 0);
//RspMessage.SendRspMessage(msg);
NLog.Crash(e.ToString());
}
NLog.NLog_Finish();
};
//string inputArg =
// "eyJkcGkiOlswLDAsMCwwXSwicG9pbnRfdHlwZSI6WzAsMCwwLDBdLCJpbWFnZV90eXBlI" +
// "jpbZmFsc2UsZmFsc2UsZmFsc2UsdHJ1ZV0sImtleSI6IlMwLE8wMDAsQjAwMDAsUDAwMC0" +
// "yNTUsRDIwMTgvMTIvMzE7Q0NBRkJRTVhZUE9BT0NJUks1MlM4UUM4U080QTBBR0E4WSIsIm" +
// "ZpbGVQYXRoIjoiRTpcXE5ldEVhc2VcXLnsvKOxylxcU2FtcGxlXFwxMjMucGRmIiwiU3Rhcn" +
// "RQYWdlSUQiOjAsInNlc3Npb25JZCI6IjRCRDVEOTIzLTQ3RUEtNERFRi1BMUNELTlCODVCNT" +
// "E1QjE5MSJ9";
//string inputArg2 = "eyJkcGkiOlswLDAsMCwwXSwicG9pbnRfdHlwZSI6WzAsMCwwLDBdLCJpb" +
// "WFnZV90eXBlIjpbZmFsc2UsZmFsc2UsZmFsc2UsdHJ1ZV0sImtleSI6Il" +
// "MwLE8wMDAsQjAwMDAsUDAwMC0yNTUsRDIwMTgvMTIvMzE7Q0NBRkJRTVh" +
// "ZUE9BT0NJUks1MlM4UUM4U080QTBBR0E4WSIsImZpbGVQYXRoIjoiQzpc" +
// "XFdvcmtzXFxwZGZcXDEyMy5wZGYiLCJTdGFydFBhZ2VJRCI6MCwic2Vzc" +
// "2lvbklkIjoiNEJENUQ5MjMtNDdFQS00REVGLUExQ0QtOUI4NUI1MTVCMTkxIn0=";
try
{
if (args.Length == 1)
{
var deCode = Convert.FromBase64String(args[0]);
var strParams = Encoding.Default.GetString(deCode);
inParams = JsonConvert.DeserializeObject<GeneratorParams>(strParams);
}
else if (args.Length == 2)
{
var deCode = Convert.FromBase64String(args[1]);
var strParams = Encoding.Default.GetString(deCode);
inParams = JsonConvert.DeserializeObject<GeneratorParams>(strParams);
inParams.sessionId = args[0];
}
else
{
inParams = new GeneratorParams();
}
}
catch (Exception e)
{
rspMsg = new RspMessage("");
var msg = rspMsg.FormatRspMessage(10, e.Message, 0);
RspMessage.SendRspMessage(msg);
NLog.Crash(string.Format("[{0}]: ", (inParams == null) ? inParams.sessionId : "UnInit") + e.Message);
return -(int) ErrCode.ERR_JSON_DECODE;
}
rspMsg = new RspMessage(inParams.sessionId);
tmObj.ProgressChange += rspMsg.ProgressMessage;
var jsInput = JsonConvert.SerializeObject(inParams);
NLog.Debug("Input:" + jsInput + Environment.NewLine);
if (!File.Exists(inParams.filePath))
{
NLog.Error("File Not Exists: " + inParams.filePath + Environment.NewLine);
return -(int) ErrCode.ERR_FILE_NOTEXISTS;
}
try
{
tmObj.TmatrixInitialize();
}
catch (Exception e)
{
var msg = rspMsg.FormatRspMessage(7, e.Message, 0);
RspMessage.SendRspMessage(msg);
NLog.Crash(e.ToString());
return -(int) ErrCode.ERR_EXCEPT_THROW;
}
try
{
var ret = tmObj.GenerateTmatrixCode_OID4(inParams.key,
inParams.filePath, inParams.StartPageID, inParams.point_type,
inParams.image_type, inParams.dpi);
if (ret.Substring(0, 1) != "0")
{
var msg = rspMsg.FormatRspMessage(int.Parse(ret.Substring(0, 1)),
ret.Substring(1, ret.Length - 1), tmObj.GetProgerss());
NLog.Error(msg + Environment.NewLine);
RspMessage.SendRspMessage(msg);
}
else
{
var msg = rspMsg.FormatRspMessage(int.Parse(ret.Substring(0, 1)),
ret.Substring(1, ret.Length - 1), 100);
NLog.Debug(msg + Environment.NewLine);
RspMessage.SendRspMessage(msg);
}
}
catch (Exception e)
{
var msg = rspMsg.FormatRspMessage(8, e.Message, tmObj.GetProgerss());
RspMessage.SendRspMessage(msg);
NLog.Crash(e.ToString());
return -(int) ErrCode.ERR_EXCEPT_THROW;
}
return 0;
}
private enum ErrCode
{
ERR_INPUT_PARAMS = 1,
ERR_FILE_NOTEXISTS,
ERR_EXCEPT_THROW,
ERR_JSON_DECODE
}
}
}
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using GeneratorCode.Configure;
using GeneratorCode.Logs;
using JetBrains.Annotations;
using Newtonsoft.Json;
using TmatrixLibrary;
namespace GeneratorCode
{
public class GeneratorParams
{
public GeneratorParams()
{
dpi = new[] { 0, 0, 0, 0 };
point_type = new[] { 0, 0, 0, 0 };
image_type = new[] { false, false, false, true };
StartPageID = 0;
key = "S0,O000,B0000,P000-255,D2018/12/31;CCAFBQMXYPOAOCIRK52S8QC8SO4A0AGA8Y";
filePath = "E:\\NetEase\\轨迹笔\\Sample\\123.pdf"; //"C:\\Works\\pdf\\123.pdf";
//filePath = "C:\\Works\\pdf\\123.pdf";
sessionId = "4BD5D923-47EA-4DEF-A1CD-9B85B515B191";
}
public int[] dpi { get; set; }
public int[] point_type { get; set; }
public bool[] image_type { get; set; }
public string key { get; set; }
public string filePath { get; set; }
public int StartPageID { get; set; }
public string sessionId { get; set; }
}
public class GenerCodeRet
{
public GenerCodeRet(string sId, int prg)
{
sessionId = sId;
progress = prg;
}
public string sessionId { get; set; }
public int progress { get; set; }
}
public class MainConfig
{
public delegate void MainCfgChangedHandle();
private readonly object _cfgLock = new object();
public MainConfig()
{
LoadConfig();
OnMainCfgChanged += MainCfgChanged;
}
public bool Base64MsgContent { get; set; }
public int ServerPort { get; set; }
public string ServerAddr { get; set; }
public static event MainCfgChangedHandle OnMainCfgChanged;
protected static void MainCfgChanged()
{
OnMainCfgChanged?.Invoke();
}
public void LoadConfig()
{
lock (_cfgLock)
{
Base64MsgContent = NConfig.GetCfgValue("Main", "ProBase64Msg", true);
ServerPort = NConfig.GetCfgValue("Main", "ProServerPort", 10088);
ServerAddr = NConfig.GetCfgValue("Main", "ProServerAddr", "127.0.0.1");
}
}
}
public class MessageProcess
{
private readonly UdpClient _udp = new UdpClient();
public MessageProcess(MainConfig cfg)
{
var svrAddr = IPAddress.Parse(cfg.ServerAddr);
_server = new IPEndPoint(svrAddr, cfg.ServerPort);
}
private IPEndPoint _server { get; }
public void SendMessage([NotNull] string msg)
{
if (msg.Length > 0)
{
var sendData = Encoding.Default.GetBytes(msg);
_udp.Send(sendData, sendData.Length, _server);
NLog.Debug("Send: " + msg + Environment.NewLine);
}
}
}
public class RspMessage
{
public RspMessage(string sId)
{
err = 0;
msg = "";
data = new GenerCodeRet(sId, 0);
}
public RspMessage(string sId, int prg)
{
err = 0;
msg = "";
data = new GenerCodeRet(sId, prg);
}
public int err { get; set; }
public string msg { get; set; }
public GenerCodeRet data { get; set; }
public string FormatRspMessage(int errCode, string readme, int prg, bool enBase64 = true)
{
var rsp = new RspMessage(data.sessionId, prg);
rsp.err = errCode;
if (enBase64)
rsp.msg = Convert.ToBase64String(Encoding.Default.GetBytes(readme));
else
rsp.msg = readme;
return JsonConvert.SerializeObject(rsp);
}
}
internal class MainCode
{
private static int Main(string[] args)
{
NConfig.InitConfigure();
NLog.NLog_Init();
GeneratorParams inParams = null;
RspMessage rspMsg;
var mainCfg = new MainConfig();
var msgProcess = new MessageProcess(mainCfg);
var tmObj = new TmatrixClass();
AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
{
NLog.Crash("UnHandled Exception: " + e.ToString());
};
AppDomain.CurrentDomain.ProcessExit += (sender, ev) =>
{
try
{
tmObj.TmatrixUninitialize();
}
catch (Exception e)
{
//var msg = rspMsg.FormatRspMessage(9, e.Message, 0);
//RspMessage.SendRspMessage(msg);
NLog.Crash(e.ToString());
}
NLog.NLog_Finish();
};
//string inputArg =
// "eyJkcGkiOlswLDAsMCwwXSwicG9pbnRfdHlwZSI6WzAsMCwwLDBdLCJpbWFnZV90eXBlI" +
// "jpbZmFsc2UsZmFsc2UsZmFsc2UsdHJ1ZV0sImtleSI6IlMwLE8wMDAsQjAwMDAsUDAwMC0" +
// "yNTUsRDIwMTgvMTIvMzE7Q0NBRkJRTVhZUE9BT0NJUks1MlM4UUM4U080QTBBR0E4WSIsIm" +
// "ZpbGVQYXRoIjoiRTpcXE5ldEVhc2VcXLnsvKOxylxcU2FtcGxlXFwxMjMucGRmIiwiU3Rhcn" +
// "RQYWdlSUQiOjAsInNlc3Npb25JZCI6IjRCRDVEOTIzLTQ3RUEtNERFRi1BMUNELTlCODVCNT" +
// "E1QjE5MSJ9";
//string inputArg2 = "eyJkcGkiOlswLDAsMCwwXSwicG9pbnRfdHlwZSI6WzAsMCwwLDBdLCJpb" +
// "WFnZV90eXBlIjpbZmFsc2UsZmFsc2UsZmFsc2UsdHJ1ZV0sImtleSI6Il" +
// "MwLE8wMDAsQjAwMDAsUDAwMC0yNTUsRDIwMTgvMTIvMzE7Q0NBRkJRTVh" +
// "ZUE9BT0NJUks1MlM4UUM4U080QTBBR0E4WSIsImZpbGVQYXRoIjoiQzpc" +
// "XFdvcmtzXFxwZGZcXDEyMy5wZGYiLCJTdGFydFBhZ2VJRCI6MCwic2Vzc" +
// "2lvbklkIjoiNEJENUQ5MjMtNDdFQS00REVGLUExQ0QtOUI4NUI1MTVCMTkxIn0=";
try
{
if (args.Length == 1)
{
var deCode = Convert.FromBase64String(args[0]);
var strParams = Encoding.Default.GetString(deCode);
inParams = JsonConvert.DeserializeObject<GeneratorParams>(strParams);
}
else if (args.Length == 2)
{
var deCode = Convert.FromBase64String(args[1]);
var strParams = Encoding.Default.GetString(deCode);
inParams = JsonConvert.DeserializeObject<GeneratorParams>(strParams);
inParams.sessionId = args[0];
}
else
{
inParams = new GeneratorParams();
}
}
catch (Exception e)
{
rspMsg = new RspMessage("");
var msg = rspMsg.FormatRspMessage(10, e.Message, 0, mainCfg.Base64MsgContent);
msgProcess.SendMessage(msg);
NLog.Crash(string.Format("[{0}]: ", inParams == null ? inParams.sessionId : "UnInit") + e.Message);
return -(int) ErrCode.ERR_JSON_DECODE;
}
rspMsg = new RspMessage(inParams.sessionId);
tmObj.ProgressChange += step =>
{
var msgContent = rspMsg.FormatRspMessage(0, "Progress", step, mainCfg.Base64MsgContent);
var msg = JsonConvert.SerializeObject(msgContent);
msgProcess.SendMessage(msg);
};
var jsInput = JsonConvert.SerializeObject(inParams);
NLog.Debug("Input:" + jsInput + Environment.NewLine);
if (!File.Exists(inParams.filePath))
{
NLog.Error("File Not Exists: " + inParams.filePath + Environment.NewLine);
return -(int) ErrCode.ERR_FILE_NOTEXISTS;
}
try
{
tmObj.TmatrixInitialize();
}
catch (Exception e)
{
var msg = rspMsg.FormatRspMessage(7, e.Message, 0, mainCfg.Base64MsgContent);
msgProcess.SendMessage(msg);
NLog.Crash(e.ToString());
return -(int) ErrCode.ERR_EXCEPT_THROW;
}
try
{
var ret = tmObj.GenerateTmatrixCode_OID4(inParams.key,
inParams.filePath, inParams.StartPageID, inParams.point_type,
inParams.image_type, inParams.dpi);
if (ret.Substring(0, 1) != "0")
{
var msg = rspMsg.FormatRspMessage(int.Parse(ret.Substring(0, 1)),
ret.Substring(1, ret.Length - 1),
tmObj.GetProgerss(),
mainCfg.Base64MsgContent);
NLog.Error(msg + Environment.NewLine);
msgProcess.SendMessage(msg);
}
else
{
var msg = rspMsg.FormatRspMessage(int.Parse(ret.Substring(0, 1)),
ret.Substring(1, ret.Length - 1), 100,
mainCfg.Base64MsgContent);
NLog.Debug(msg + Environment.NewLine);
msgProcess.SendMessage(msg);
}
}
catch (Exception e)
{
var msg = rspMsg.FormatRspMessage(8, e.Message, tmObj.GetProgerss(), mainCfg.Base64MsgContent);
msgProcess.SendMessage(msg);
NLog.Crash(e.ToString());
return -(int) ErrCode.ERR_EXCEPT_THROW;
}
return 0;
}
private enum ErrCode
{
ERR_INPUT_PARAMS = 1,
ERR_FILE_NOTEXISTS,
ERR_EXCEPT_THROW,
ERR_JSON_DECODE
}
}
}

View File

@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<File Version="1.0">
<ExtensionPoint ID="I_OIDPGHelper" Enable="1">
<Plugin ID="OIDPGHelper" Bundle="OIDPGHelper.dll" Enable="1"/>
</ExtensionPoint>
<ExtensionPoint ID="I_OIDTiffConvertor" Enable="1">
<Plugin ID="OIDTiffConvertor" Bundle="OIDTiffConvertor.dll" Enable="1"/>
</ExtensionPoint>
</File>
<ExtensionPoint ID="I_OIDPGHelper" Enable="1">
<Plugin ID="OIDPGHelper" Bundle="OIDPGHelper.dll" Enable="1" />
</ExtensionPoint>
<ExtensionPoint ID="I_OIDTiffConvertor" Enable="1">
<Plugin ID="OIDTiffConvertor" Bundle="OIDTiffConvertor.dll" Enable="1" />
</ExtensionPoint>
</File>

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 有关程序集的一般信息由以下

View File

@ -1,7 +1,7 @@
using System.Runtime.InteropServices;
namespace OIDModule.Generator
{
using System;
using System.Runtime.InteropServices;
internal enum OIDBeginBuildState
{
eBBState_OK,
@ -9,17 +9,20 @@ namespace OIDModule.Generator
eBBState_FailToOpenImageFile,
eBBState_Unknown
}
internal enum OIDPrintPointType
{
eOID_PrintPointType_2x2,
eOID_PrintPointType_3x3,
eOID_PrintPointType_4x4
}
internal enum OIDPublishImageDPIType
{
eOID_PublishImageDPI_600,
eOID_PublishImageDPI_1200
}
internal enum OIDPublishImageType
{
eOID_PIT_Publish_Image,
@ -27,6 +30,7 @@ namespace OIDModule.Generator
eOID_PIT_BG_Vertor_Image,
eOID_PIT_Publish_BG_Image
}
internal enum OIDPublishObjectType
{
eOID_OT_ElementCode,
@ -35,64 +39,90 @@ namespace OIDModule.Generator
internal class OIDPublishImageGenerator
{
public bool AddObjectInfo(int nPageIndex, ulong uiObjectIndex, uint[] arPointX, uint[] arPointY, int nPointsCount, int nZOrder, int nObjectType)
public bool AddObjectInfo(int nPageIndex, ulong uiObjectIndex, uint[] arPointX, uint[] arPointY,
int nPointsCount, int nZOrder, int nObjectType)
{
return OID_PIG_AddObjectInfo(nPageIndex, uiObjectIndex, arPointX, arPointY, nPointsCount, nZOrder, nObjectType);
return OID_PIG_AddObjectInfo(nPageIndex, uiObjectIndex, arPointX, arPointY, nPointsCount, nZOrder,
nObjectType);
}
public int BeginBuildPublishImage(char[] szBGImage, bool bExportPDFImage, int nExportPDFImageDPI)
{
return OID_PIG_BeginBuildPublishImage(szBGImage, bExportPDFImage, nExportPDFImageDPI);
}
public int BeginBuildPublishImageByInfo(double dbCMWidth, double dbCMHeight)
{
return OID_PIG_BeginBuildPublishImageByInfo(dbCMWidth, dbCMHeight);
}
public bool BuildPublishImage(char[] szOutputFolderPath, bool bPrintIdleCode, bool bSplitBigImage, bool bMergeSplittedImages, int nPublishImageDPIType, int nPrintPointType, int nPublishImageType)
public bool BuildPublishImage(char[] szOutputFolderPath, bool bPrintIdleCode, bool bSplitBigImage,
bool bMergeSplittedImages, int nPublishImageDPIType, int nPrintPointType, int nPublishImageType)
{
return OID_PIG_BuildPublishImage(szOutputFolderPath, bPrintIdleCode, bSplitBigImage, bMergeSplittedImages, nPublishImageDPIType, nPrintPointType, nPublishImageType);
return OID_PIG_BuildPublishImage(szOutputFolderPath, bPrintIdleCode, bSplitBigImage, bMergeSplittedImages,
nPublishImageDPIType, nPrintPointType, nPublishImageType);
}
public void EndBuildPublishImage()
{
OID_PIG_EndBuildPublishImage();
}
public bool Initialize()
{
return OID_PIG_Initialize();
}
[DllImport(@".\OIDPublishImageGenerator\OIDPublishImageGenerator.dll", CallingConvention=CallingConvention.StdCall)]
private static extern bool OID_PIG_AddObjectInfo(int nPageIndex, ulong uiObjectIndex, uint[] arPointX, uint[] arPointY, int nPointsCount, int nZOrder, int nObjectType);
[DllImport(@".\OIDPublishImageGenerator\OIDPublishImageGenerator.dll", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
private static extern int OID_PIG_BeginBuildPublishImage(char[] szBGImage, bool bExportPDFImage, int nExportPDFImageDPI);
[DllImport(@".\OIDPublishImageGenerator\OIDPublishImageGenerator.dll", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
[DllImport(@".\OIDPublishImageGenerator\OIDPublishImageGenerator.dll", CallingConvention =
CallingConvention.StdCall)]
private static extern bool OID_PIG_AddObjectInfo(int nPageIndex, ulong uiObjectIndex, uint[] arPointX,
uint[] arPointY, int nPointsCount, int nZOrder, int nObjectType);
[DllImport(@".\OIDPublishImageGenerator\OIDPublishImageGenerator.dll",
CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
private static extern int OID_PIG_BeginBuildPublishImage(char[] szBGImage, bool bExportPDFImage,
int nExportPDFImageDPI);
[DllImport(@".\OIDPublishImageGenerator\OIDPublishImageGenerator.dll",
CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
private static extern int OID_PIG_BeginBuildPublishImageByInfo(double dbCMWidth, double dbCMHeight);
[DllImport(@".\OIDPublishImageGenerator\OIDPublishImageGenerator.dll", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
private static extern bool OID_PIG_BuildPublishImage(char[] szOutputFolderPath, bool bPrintIdleCode, bool bSplitBigImage, bool bMergeSplittedImages, int nPublishImageDPIType, int nPrintPointType, int nPublishImageType);
[DllImport(@".\OIDPublishImageGenerator\OIDPublishImageGenerator.dll", CallingConvention=CallingConvention.StdCall)]
[DllImport(@".\OIDPublishImageGenerator\OIDPublishImageGenerator.dll",
CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
private static extern bool OID_PIG_BuildPublishImage(char[] szOutputFolderPath, bool bPrintIdleCode,
bool bSplitBigImage, bool bMergeSplittedImages, int nPublishImageDPIType, int nPrintPointType,
int nPublishImageType);
[DllImport(@".\OIDPublishImageGenerator\OIDPublishImageGenerator.dll", CallingConvention =
CallingConvention.StdCall)]
private static extern void OID_PIG_EndBuildPublishImage();
[DllImport(@".\OIDPublishImageGenerator\OIDPublishImageGenerator.dll", CallingConvention=CallingConvention.StdCall)]
[DllImport(@".\OIDPublishImageGenerator\OIDPublishImageGenerator.dll", CallingConvention =
CallingConvention.StdCall)]
private static extern bool OID_PIG_Initialize();
[DllImport(@".\OIDPublishImageGenerator\OIDPublishImageGenerator.dll", CallingConvention=CallingConvention.StdCall)]
[DllImport(@".\OIDPublishImageGenerator\OIDPublishImageGenerator.dll", CallingConvention =
CallingConvention.StdCall)]
private static extern bool OID_PIG_SetPublishPages(int[] arPageNumbers, int nPageCount);
[DllImport(@".\OIDPublishImageGenerator\OIDPublishImageGenerator.dll", CallingConvention=CallingConvention.StdCall)]
[DllImport(@".\OIDPublishImageGenerator\OIDPublishImageGenerator.dll", CallingConvention =
CallingConvention.StdCall)]
private static extern bool OID_PIG_SetStartPosition(int nPageIndex, int nXStart, int nYStart);
[DllImport(@".\OIDPublishImageGenerator\OIDPublishImageGenerator.dll", CallingConvention=CallingConvention.StdCall)]
[DllImport(@".\OIDPublishImageGenerator\OIDPublishImageGenerator.dll", CallingConvention =
CallingConvention.StdCall)]
private static extern void OID_PIG_Uninitialize();
public bool SetPublishPages(int[] arPageNumbers, int nPageCount)
{
return OID_PIG_SetPublishPages(arPageNumbers, nPageCount);
}
public bool SetStartPosition(int nPageIndex, int nXStart, int nYStart)
{
return OID_PIG_SetStartPosition(nPageIndex, nXStart, nYStart);
}
public void Uninitialize()
{
OID_PIG_Uninitialize();

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,16 @@
; 应用程序配置文件
;---------------------------------------
; 主要配置项
;
[Main]
; 是否对协议中的 msg 字段进行 Base64 编码
ProBase64Msg = false
; 服务器端口号
ProServerPort = 10088
; 服务器 IP 地址
ProServerAddr = 127.0.0.1
;---------------------------------------
; Log 相关配置
;

View File

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ini-parser" version="2.5.2" targetFramework="net45" />
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net45" />