You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
250 lines
9.3 KiB
250 lines
9.3 KiB
using connor_zwcadm.dialog;
|
|
using connor_zwcadm.model;
|
|
using connor_zwcadm.util;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
using ZwSoft.ZwCAD.ApplicationServices;
|
|
using ZwSoft.ZwCAD.DatabaseServices;
|
|
|
|
namespace connor_zwcadm.commands {
|
|
public class SocketServerCommand {
|
|
|
|
public const int PORT = 11415;
|
|
// Incoming data from the client.
|
|
public static string data = null;
|
|
public const string CLOSE_SOCKET = "CLose<EOF>";
|
|
public const string SUCCESS_MESSAGE = "OK";
|
|
public const char REQUEST_SPLITTER = '|';
|
|
public const string CMD_SYNC_PROPERTIES = "SYNC";
|
|
public const string CMD_CONVERT_PDF = "PDF";
|
|
public Dictionary<string, PlotData> plotInfos = new Dictionary<string, PlotData>();
|
|
private KBackgroundWorker bgWorker { get; }
|
|
public SocketServerCommand() {
|
|
bgWorker = new KBackgroundWorker();
|
|
bgWorker.backgroundWorker.DoWork += StartServer;
|
|
}
|
|
|
|
public static void StopServer() {
|
|
byte[] bytes = new byte[1024];
|
|
KUtil.Log("开始关闭Socket服务");
|
|
IPAddress ipAddress = IPAddress.Loopback;
|
|
IPEndPoint remoteEP = new IPEndPoint(ipAddress, PORT);
|
|
Socket sender = new Socket(ipAddress.AddressFamily,
|
|
SocketType.Stream, ProtocolType.Tcp);
|
|
sender.Connect(remoteEP);
|
|
try {
|
|
byte[] msg = Encoding.UTF8.GetBytes(CLOSE_SOCKET);
|
|
sender.Send(msg);
|
|
int bytesRec = sender.Receive(bytes);
|
|
KUtil.Log("收到返沪数据:" + Encoding.UTF8.GetString(bytes, 0, bytesRec));
|
|
}
|
|
finally {
|
|
sender.Shutdown(SocketShutdown.Both);
|
|
sender.Close();
|
|
}
|
|
}
|
|
|
|
public void StartServer(object sender, DoWorkEventArgs e) {
|
|
KUtil.Log("开始启动Socket服务");
|
|
bgWorker.Progress("服务运行中", "", 0);
|
|
// Data buffer for incoming data.
|
|
byte[] bytes = new byte[1024];
|
|
|
|
// Establish the local endpoint for the socket.
|
|
// Dns.GetHostName returns the name of the
|
|
// host running the application.
|
|
//IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
|
|
IPAddress ipAddress = IPAddress.Any; // ipHostInfo.AddressList[0];
|
|
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, PORT);
|
|
// Create a TCP/IP socket.
|
|
Socket listener = new Socket(ipAddress.AddressFamily,
|
|
SocketType.Stream, ProtocolType.Tcp);
|
|
|
|
// Bind the socket to the local endpoint and
|
|
// listen for incoming connections.
|
|
|
|
listener.Bind(localEndPoint);
|
|
listener.Listen(20);
|
|
// Start listening for connections.
|
|
while (true) {
|
|
bgWorker.Progress("服务运行中", "", 0);
|
|
KUtil.Log("--------------------------------------------------------------------------");
|
|
KUtil.Log("等待连接");
|
|
// Program is suspended while waiting for an incoming connection.
|
|
Socket handler = listener.Accept();
|
|
try {
|
|
data = null;
|
|
// An incoming connection needs to be processed.
|
|
while (true) {
|
|
int bytesRec = handler.Receive(bytes);
|
|
data += Encoding.UTF8.GetString(bytes, 0, bytesRec);
|
|
if (data.IndexOf("<EOF>") > -1) {
|
|
break;
|
|
}
|
|
}
|
|
if (CLOSE_SOCKET.Equals(data)) {
|
|
handler.Send(Encoding.UTF8.GetBytes("Closed"));
|
|
handler.Shutdown(SocketShutdown.Both);
|
|
handler.Close();
|
|
break;
|
|
}
|
|
bgWorker.Progress("处理请求", "", 30);
|
|
string resp;
|
|
KUtil.Log("收到请求:" + data);
|
|
try {
|
|
resp = ProcessRequest(data);
|
|
}
|
|
catch (Exception ex1) {
|
|
KUtil.LogErr(ex1);
|
|
resp = ex1.Message;
|
|
}
|
|
KUtil.Log("返回:" + resp);
|
|
bgWorker.Progress("返回数据", "", 100);
|
|
// Echo the data back to the client.
|
|
byte[] msg = Encoding.UTF8.GetBytes(resp);
|
|
handler.Send(msg);
|
|
}
|
|
catch (Exception ex) {
|
|
KUtil.LogErr(ex);
|
|
}
|
|
{
|
|
try {
|
|
handler.Shutdown(SocketShutdown.Both);
|
|
handler.Close();
|
|
}
|
|
catch (Exception ex) {
|
|
KUtil.LogErr(ex);
|
|
}
|
|
}
|
|
}
|
|
KUtil.Log("Socket服务已关闭");
|
|
}
|
|
|
|
public static Document OpenDoc(string path, bool readOnly) {
|
|
KUtil.Log("打开:"+path);
|
|
Document doc = Application.DocumentManager.Open(path, readOnly);
|
|
Application.DocumentManager.MdiActiveDocument = doc;
|
|
return doc;
|
|
}
|
|
|
|
public static void CloseAllDoc() {
|
|
foreach (Document doc in Application.DocumentManager) {
|
|
doc.CloseAndDiscard();
|
|
}
|
|
}
|
|
|
|
public static void CloseDoc(Document doc, bool save) {
|
|
if (doc == null) {
|
|
return;
|
|
}
|
|
KUtil.Log("关闭:"+doc.Name+",保存:"+save);
|
|
if (save) {
|
|
doc.CloseAndSave(doc.Name);
|
|
}
|
|
else {
|
|
doc.CloseAndDiscard();
|
|
}
|
|
KUtil.Log("关闭完成");
|
|
}
|
|
|
|
public string ProcessRequest(string request) {
|
|
string[] split = request.Split(REQUEST_SPLITTER);
|
|
int len = KUtil.GetLen(split);
|
|
if (len == 0) {
|
|
return "无请求信息";
|
|
}
|
|
// CloseAllDoc();
|
|
string command = split[0].Trim().ToUpper();
|
|
string tukuai = null;
|
|
switch (command) {
|
|
case CMD_CONVERT_PDF:
|
|
bgWorker.ProgressDetail("转换PDF"); // 文件路径 标题栏名称 MediaName
|
|
if (len < 3) {
|
|
throw new Exception("参数错误");
|
|
}
|
|
bgWorker.pbDialog.Dispatcher.Invoke(new Action(delegate {
|
|
Document doc = OpenDoc(split[1].Trim(), true);
|
|
try {
|
|
using (DocumentLock docLock = doc.LockDocument()) {
|
|
tukuai = ConvertPDFCommand.ConvertPDF(doc, plotInfos);
|
|
}
|
|
}
|
|
finally {
|
|
CloseDoc(doc, false);
|
|
}
|
|
// 后台打开图纸
|
|
//using (Database db = new Database(false, true)) {
|
|
// db.ReadDwgFile(split[1].Trim(), FileOpenMode.OpenForReadAndAllShare, false, null);
|
|
// db.CloseInput(true);
|
|
// ConvertPDFCommand.ConvertPDF(db, split[1].Trim(), plotInfos);
|
|
// db.Dispose();
|
|
//}
|
|
}));
|
|
break;
|
|
case CMD_SYNC_PROPERTIES:
|
|
bgWorker.ProgressDetail("同步属性");
|
|
if (len < 3) {
|
|
throw new Exception("参数错误");
|
|
}
|
|
bgWorker.pbDialog.Dispatcher.Invoke(new Action(delegate {
|
|
// 前台打开图纸
|
|
Document doc = OpenDoc(split[1].Trim(), false);
|
|
try {
|
|
//new ReadInfoCommand().ExecuteCommand(doc);
|
|
new ReadInfoCommand(split[1].Trim()).Execute();
|
|
}
|
|
finally {
|
|
CloseDoc(doc, true);
|
|
}
|
|
// new ReadInfoCommand(split[1].Trim()).Execute();
|
|
|
|
// 后台打开图纸
|
|
//using (Database db = new Database(false, true)) {
|
|
// db.ReadDwgFile(split[1].Trim(), FileOpenMode.OpenForReadAndAllShare, false, null);
|
|
// db.CloseInput(true);
|
|
// new ReadInfoCommand(split[1].Trim()).Execute();
|
|
// // new ReadInfoCommand().ExecuteCommand(db, split[1].Trim());
|
|
// //db.SaveAs(split[1].Trim(), DwgVersion.Current); // 不能用这里的保存
|
|
// db.Dispose();
|
|
//}
|
|
}));
|
|
break;
|
|
default:
|
|
return "未知命令\""+command+"\"";
|
|
}
|
|
if ("OFF".Equals(tukuai)) {
|
|
return tukuai;
|
|
}
|
|
return SUCCESS_MESSAGE;
|
|
}
|
|
|
|
private void InitCommand() {
|
|
string[] titleBlockNames = ConfigUtil.GetValue("titles").Split(';');
|
|
string[] mediaNames = ConfigUtil.GetValue("media_names").Split(';');
|
|
string[] rotates = ConfigUtil.GetValue("rotates").Split(';');
|
|
int len = KUtil.GetLen(titleBlockNames);
|
|
for (int i = 0; i < len; i++) {
|
|
PlotData plot = PlotData.Parse(i, titleBlockNames, mediaNames, rotates);
|
|
if (plot == null) {
|
|
break;
|
|
}
|
|
KUtil.Log("添加打印信息: " + plot.TitleBlockName + "|" + plot.MediaName + "|" + plot.Rotate);
|
|
KUtil.Put(plotInfos, plot.TitleBlockName, plot);
|
|
}
|
|
if (plotInfos.Count == 0) {
|
|
throw new Exception("未配置PDF转换数据");
|
|
}
|
|
}
|
|
|
|
public void ExecuteCommand() {
|
|
InitCommand();
|
|
bgWorker.Start(null);
|
|
}
|
|
|
|
}
|
|
}
|