在这里请耐心等待,并尝试轻松处理不良练习:)
我开始理解接口的概念,我已经在我的程序中实现了一个。所以我会尝试解释..我正在创建一个类库 dll,它将与我的警报面板接口。报警面板可以有两种类型的连接,IP和串行。所以我为此实现了一个称为 IConnection 的接口。
并创建连接,如下所示:
//IConnection connection = new SerialConnection("com1", 9600);
IConnection conn = new TcpConnection(System.Net.IPAddress.Parse("192.168.0.14"), 1234);
AlarmPanel alarm = new AlarmPanel(conn, Pass);
alarm.SetLogger(logger);
alarm.Connect();
在具体类中(正确的术语?我实现了一个名为SendMessage的方法,我用它来与传输无关,运行良好。
但是,我现在想添加一个异步处理程序来处理发回的不是命令/响应样式消息的即席消息。
我有一个事件处理程序在我的主TCPConnection类中工作:
private static void tcpReceive(Socket client)
{
try
{
// Create the state object.
StateObject state = new StateObject {workSocket = client};
// Begin receiving the data from the remote device.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(receiveCallback), state);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void receiveCallback(IAsyncResult ar)
{
try
{
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
// Read data from the remote device.
int bytesRead = client.EndReceive(ar);
if (bytesRead <= 0) return; // No data...
// Console.WriteLine("Ascii {0}", Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
Console.WriteLine("Raw: {0}", BitConverter.ToString(state.buffer, 0, bytesRead));
processMessage(new Response {Data = state.buffer,BytesLength = bytesRead} );
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(receiveCallback), state);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void processMessage(Response resp)
{
// Do something with the message here..
}
但是,我想从处理代码中抽象出IP内容,并将processMessage移回使用该接口的类中。(我知道我没有很好地解释这一点......所以让我再试一次)
在我的"类 TcpConnection : IConnection"中设置事件处理程序
从 AlarmPanel 类打开事件处理,构造函数如下所示:
public AlarmPanel(IConnection connection, int Password)
{
_connection = connection;
_Password = Password;
}
它使用该接口(IConnection),并且能够说使用alarmPanel类中的ProcessMessage方法,以便我可以在使串行事件处理工作时调用相同的方法。
听起来您想在界面上注册一个事件 IConnection
,AlarmPanel
可以订阅该事件。这样,您可以让 IConnection
实现处理用于检索消息的逻辑,但让AlarmPanel
对收到的消息执行所需的操作。
public class AlarmPanel
{
public AlarmPanel(IConnection connection, int Password)
{
_connection = connection;
_Password = Password;
// Bind event.
_connection.MessageReceived += ProcessMessage;
}
private void ProcessMessage(object sender, MessageEventArgs e)
{
// Do your central processing here with e.Message.
}
}
public interface IConnection
{
event Action<object, MessageEventArgs> MessageRecieved;
}
public class TcpConnection : IConnection
{
// Other code.
private static void processMessage(Response resp)
{
// Do something with the message here..
var eventArgs = new MessageEventArgs
{
Message = response
};
OnMessageReceived(eventArgs);
}
protected virtual void OnMessageReceived(MessageEventArgs e)
{
// Call subscribers.
var handler = MessageRecieved;
if (handler != null) handler(this, e);
}
public event Action<object, MessageEventArgs> MessageRecieved;
}
// Class for passing Response back to AlarmPanel.
public class MessageEventArgs : System.EventArgs
{
Response Message { get; set; } // Consider using an interface for Response.
}