我编写了一个本质上是心跳的类,客户端每隔x秒就向服务器发送一条消息。
无耻地被盗发送代码
private void SendUdpPacket() {
byte[] data = new byte[1024];
Socket udpClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
data = Encoding.ASCII.GetBytes("lubdub");
udpClientSocket.SendTo(data, 0, data.Length, SocketFlags.None, ipep);
}
无耻地被盗接收代码
void ReceiveData(IAsyncResult iar) {
byte[] buffer = new byte[1024];
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint tempRemoteEP = (EndPoint)sender;
Socket remote = (Socket)iar.AsyncState;
int recv = remote.EndReceiveFrom(iar, ref tempRemoteEP);
string stringData = Encoding.ASCII.GetString(buffer, 0, recv);
Console.WriteLine(stringData);
lastUpdate = DateTime.Now.ToUniversalTime();
if (!this.IsDisposed) {
udpServerSocket.BeginReceiveFrom(buffer, 0, 1024, SocketFlags.None, ref ep, new AsyncCallback(ReceiveData), udpServerSocket);
}
然后由内部计时器
监控private void clTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
try {
SendUdpPacket();
connected = true;
} catch {
connected = false;
}
}
private void srTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
// Calculate the Timespan since the Last Update from the Client.
timeSinceLastHeartbeat = DateTime.Now.ToUniversalTime() - lastUpdate;
if (timeSinceLastHeartbeat > TimeSpan.FromMilliseconds(Timer.Interval))
connected = false;
else
connected = true;
}
如果消息成功则连接套接字,这将通过公共布尔值connected
显示
因此,在我的应用程序中,我将有一个运行以下内容的计时器:
private void ServerCheck() {
if (heartin.connected) {
GKstat = true;
GKStatus.Text = "GateKeeper Status: Connected";
} else {
GKstat = false;
GKStatus.Text = "GateKeeper Status: Disconnected";
}
}
然而,这并不理想,因为它需要客户端或服务器上的计时器不断检查Heart
是否已连接。
我想知道将此转换为一组事件是否有益,
说OnConnectionLost
和OnConnected
我一直在四处寻找并阅读各种页面,他们只是让我更加困惑。
这是我到目前为止所拥有的
public delegate void OnConnectionLost(Heart sender, EventArgs e);
public delegate void OnConnected(object sender, EventArgs e);
public event OnConnectionLost ConnectionLost;
我的问题是,这是否有益,如果是这样,我将如何创建事件,使其仅在连接状态发生变化时触发?
答案 0 :(得分:1)
这是布尔是邪恶的场景之一:我强烈建议你制作一个enum
,例如:
public enum ConnectionState
{
Disconnected = 0,
Disconnecting = 1,
Connecting = 2,
Connected = 3,
// etc.
}
然后,您可以将此项用于ConnectionState
等属性。此外,通常的做法是使用标准EventHandler<T>
委托而不是您自己的自定义委托类型。例如:
public sealed class ConnectionStateEventArgs : EventArgs
{
public ConnectionState ConnectionState { get; private set; }
public ConnectionStateEventArgs(ConnectionState connectionState)
{
ConnectionState = connectionState;
}
}
public event EventHandler<ConnectionStateEventArgs> ConnectionStateChanged;
这样您就可以将连接状态扩展到您现在所拥有的简单“开 - 关”之外。 ConnectionState
属性与ConnectionStateChanged
事件相结合是我在各种API中注意到的常见做法。
要在连接状态确实更改时才启动它,只需在ConnectionState
属性中添加一个正文:
private ConnectionState _connectionState;
public ConnectionState ConnectionState
{
get { return _connectionState; }
set
{
if (value != _connectionState)
{
_connectionState = value;
var tmp = ConnectionStateChanged;
if (tmp != null)
tmp (this, new ConnectionStateEventArgs(value));
}
}
}
您需要做的只是为属性分配一个不同的值,事件将触发。