好的,情况如下:
我有一个具有 2 个设置的客户端:ConnectionState 和 ConnectionSollState,它们都是相同的可枚举(TypeConnectionState),它们存储客户端连接的实际状态和连接应该是的状态。在 to 的每种组合上,都应该发生不同的事情,例如 ConnectionState 为“Connected”但 ConnectionSollState 为“Closed”-> 拆除客户端。所以我有 4 种可能性需要检查。现在每个客户端都可以处理无限数量的会话,并且每个会话也有一个状态(StreamState & StreamSollState),这些状态可以有 6 个可枚举的选项。
从现在开始,我做了大约 20 个开关条件,我的代码看起来非常混乱,我在编码时每 5 分钟就会做错事。有没有更简单的方法来处理这种情况?(如果/否则)会使事情变得更糟。
例子:
private void RTSPWorker() {
try {
byte[] buffer = new byte[2048];
while (!mb_RTSPWorkerAbbort) {
// Call TransportWD
Thread.Sleep(100 * mi_ConnectionTimeOut);
// Check ConnectionSollState
switch(ConnectionSollState) {
case TypeConnectionState.Connected:
// ConnectionSollState = Connected, check ConnectionState
switch(ConnectionState) {
case TypeConnectionState.Connected:
// ConnectionState is connected, keep-alive!
if(GET_PARAMETER() == null) {
DESCRIBE();
}
// Check streams too
foreach (cRTSPStream oStream in mo_StreamDict.Values) {
// Check StreamSollState
switch(oStream.RTSPStreamSollState) {
case cRTSPStream.TypeRTSPStreamState.Play:
// SollState is PLAY, check State
switch(oStream.RTSPStreamState) {
case cRTSPStream.TypeRTSPStreamState.Play:
//Stream is alive, keep-alive!
if (oStream.PLAY() == null) { oStream.DESCRIBE(); } break;
case cRTSPStream.TypeRTSPStreamState.Closed:
// Reinitialise.
if (oStream.SETUP() != null) { oStream.PLAY(); } break;
default:
// Default, send play.
oStream.PLAY(); break;
}
break;
case cRTSPStream.TypeRTSPStreamState.Pause:
// SollState is on pause, check State
switch(oStream.RTSPStreamState) {
case cRTSPStream.TypeRTSPStreamState.Closed:
// Reinitialise.
if (oStream.SETUP() != null) { oStream.PLAY(); } break;
default:
oStream.PAUSE();
break;
}
break;
case cRTSPStream.TypeRTSPStreamState.Closed:
// SollState is closed, check State
switch(oStream.RTSPStreamState) {
case cRTSPStream.TypeRTSPStreamState.Closed:
// Is closed, do nothing
break;
default:
// Default teardown, remove session
oStream.TEARDOWN();
this.RemoveRTSPSession(oStream);
break;
}
default:
// Default, what do?
break;
}
}
break;
case TypeConnectionState.Closed:
// ConnectionState should be connected, re-connect!
while(Connect() != true) {
// Sleep for 200ms, try again
Thread.Sleep(200);
}
break;
default:
// TODO anything else
break;
}
break;
case TypeConnectionState.Closed:
// Check ConnectionState
switch(ConnectionState) {
case TypeConnectionState.Connected:
// Is connected, should be closed. Close connection & clean up!
Close(null);
break;
default:
// Anything other than Connected, do nothing.
break;
}
break;
default:
break;
}
}
} catch {
}
}