我正在C#中創建一個服務器,我有一個單例處理數組中的所有登錄用戶。該數組是一個名爲UserSession的類的數組。在類內處理幾個方法的最佳方法?
此UserSession類具有一個方法,該方法在處理該用戶的傳入數據包的單獨線程上運行。
好,考慮下面的代碼:
class UserSession
{
public UserSession(TcpClient client)
{
var thread = new Thread(new ParameterizedThreadStart(HandleComm);
thread.Start((object)client);
}
public void HandleComm(object tcpClient)
{
TcpClient client = (TcpClient)tcpClient;
NetworkStream stream = client.GetStream();
while(1 == 1)
{
byte[] buffer = new byte[4096];
stream.Read(buffer, 0, buffer.Length);
int testShort = Convert.ToInt32(buffer);
switch((ActionEnum)testShort)
{
case ActionEnum.Something:
// here is my problem!
// do some more parsing of the message
this.DoSomething(SomethingStruct argument); // argument taken from the byte array
break;
case ActionEnum.AnotherSomething:
// the same as before
break;
// and this goes on and on
}
}
}
}
什麼是處理所有這些不同的枚舉而無需重複的超過80種方法對班上最好的方法是什麼? (ActionEnum是具有當前用戶特定操作的枚舉)。
我序列化該緩衝區,我只是快速讓你有一個想法的代碼。
鏈如何出現在你的類'UserSession'是單身?它有一個公共構造函數 – Habib 2012-07-10 04:24:36
我希望你處理任何stream.Read()返回你的生產代碼。至於這個問題,有很多方法有什麼問題?如果你只做了幾行代碼,處理可能會停留在案例本身;否則它可能值得一種方法。 upd:如果你的對象是一致的序列化的,它實際上可能是一個方法,其中一些描述傳入消息的格式作爲參數。 – 2012-07-10 04:28:38
@ Habib.OSU我在一個名爲LoggedUsers的類裏面有一個UserSession數組,這是一個單身人士 – Pacha 2012-07-10 04:31:23