私は 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 は、現在のユーザーの特定のアクションを持つ列挙型です)。
私はそのバッファをシリアル化し、アイデアを得るためにそのコードを高速にしました。