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.
106 lines
3.1 KiB
106 lines
3.1 KiB
4 years ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Text;
|
||
|
using System.ServiceModel;
|
||
|
|
||
|
namespace OpcServerTestWinService
|
||
|
{
|
||
|
public class PLCServiceTest : IPLCServiceTest
|
||
|
{
|
||
|
#region IPLCServiceTest 成员
|
||
|
|
||
|
private static Object syncObjTest = new Object();////定义一个静态对象用于线程部份代码块的锁定,用于lock操作
|
||
|
IChatCallbackTest callback = null;
|
||
|
|
||
|
public int Register(string machineNo)
|
||
|
{
|
||
|
//建立双通道通信
|
||
|
lock (syncObjTest)//线程的同步性,同步访问多个线程的任何变量,利用lock(独占锁),确保数据访问的唯一性。
|
||
|
{
|
||
|
if (string.IsNullOrEmpty(machineNo) == false && !ChatEventHelperTest.chatterstest.ContainsKey(machineNo))
|
||
|
{
|
||
|
ChatEventHelperTest.chatterstest.Add(machineNo, ReturnProductCode);
|
||
|
Console.WriteLine(machineNo + "注册双工通信");
|
||
|
|
||
|
callback = OperationContext.Current.GetCallbackChannel<IChatCallbackTest>();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
public int Logout(string machineNo)
|
||
|
{
|
||
|
//关闭双通道通信
|
||
|
|
||
|
//关闭对应的线程。
|
||
|
|
||
|
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
public void SendOperateOrder(string config)
|
||
|
{
|
||
|
Console.WriteLine("客户端调用");
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
/// <summary>
|
||
|
/// 对工控机端执行的接口进行封装,
|
||
|
/// </summary>
|
||
|
/// <param name="sender"></param>
|
||
|
/// <param name="e"></param>
|
||
|
public void ReturnProductCode(object sender, ChatEventArgsTest e)
|
||
|
{
|
||
|
callback.ReturnProductCode(e.message);
|
||
|
}
|
||
|
|
||
|
public void ReturnProductCodeToMachine(string productCode, string machineNo)
|
||
|
{
|
||
|
|
||
|
|
||
|
ChatEventArgsTest e = new ChatEventArgsTest();
|
||
|
e.message = productCode;
|
||
|
|
||
|
try
|
||
|
{
|
||
|
|
||
|
ChatEventHelperTest.ChatEventHandlerTest chatEvent;
|
||
|
lock (syncObjTest)
|
||
|
{
|
||
|
//通过工控机名称获取事件信息
|
||
|
chatEvent = ChatEventHelperTest.chatterstest[machineNo]; ; //查找成员字典中,找到要接收者的委托调用
|
||
|
}
|
||
|
|
||
|
//向工控机发送条码信息
|
||
|
chatEvent.BeginInvoke(this, e, new AsyncCallback(EndAsync), null);//异步方式调用接收者的委托调用
|
||
|
}
|
||
|
catch (KeyNotFoundException)
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
private void EndAsync(IAsyncResult ar)
|
||
|
{
|
||
|
ChatEventHelperTest.ChatEventHandlerTest d = null;
|
||
|
|
||
|
try
|
||
|
{
|
||
|
//封装异步委托上的异步操作结果
|
||
|
System.Runtime.Remoting.Messaging.AsyncResult asres = (System.Runtime.Remoting.Messaging.AsyncResult)ar;
|
||
|
d = ((ChatEventHelperTest.ChatEventHandlerTest)asres.AsyncDelegate);
|
||
|
d.EndInvoke(ar);
|
||
|
}
|
||
|
catch
|
||
|
{
|
||
|
//ChatEvent -= d;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|