2016-06-27 21 views
3

質問は簡単です:私は文書全体を読んだことがありますが、プロトコルを実装せずに使用する方法があるかどうかは分かりません。C#SuperSocket without protocol

私は特定のコマンドを送信する必要はありませんが、多くの要因によって1つまたは数百のバイトしか送信できません。私はシンプルソケットを使用する古いTCPサーバーを更新する必要があります。これは4年以上前にSystem.Net.Socketsのlibsを使って作成されたもので、SuperSocketのようなウェルノートライブラリを使用するより強力なソリューションを実現したいと思います。

いいですか?

ありがとうございます。

+0

プロトコルを実装しないとどういう意味ですか?スーパーソケットは、設定に応じてTCPまたはUDPのいずれかを使用します –

+0

まあ、明らかに、トランスポートレベルのプロトコルとしてTCPとUDPを使用します。しかし、ドキュメントを読むことで、データをカプセル化するための通信プロトコルを開発する必要があるようです。 あなたが[ここ](http://docs.supersocket.net/v1-6/en-US/The-Built-in-Command-Line-Protocol)を見れば、私の意味を読むことができます。このページの最初の質問には、あなたの答えがあります。私はあなたのアプリケーションレベルのプロトコルを定義する必要があります... _、これは必須と思われます。 ありがとうございます。 – tedebus

+0

データをバイト単位で受信したいだけですか? –

答えて

7

プロトコルを実装する必要はありません。インターフェイスIReceiveFilterを実装してReceiveFilterを作成するだけです。

だから、最初の下のいずれかのようにカスタムRequestInfoをクラスを作成します。

public class MyRequestInfo : IRequestInfo 
{ 
    public string Key { get; set; } 
    public string Unicode { get; set; } 

    // You can add more properties here 
} 

その後ReceiveFilterを作成 - ReceiveFilterは基本的にすべての着信メッセージをフィルタリングするクラスです。これは、プロトコルを実装したくない場合に必要なものです。

public class MyReceiveFilter: IReceiveFilter<MyRequestInfo> 
{ 

// This Method (Filter) is called whenever there is a new request from a connection/session 
//- This sample method will convert the incomming Byte Array to Unicode string 

    public MyRequestInfo Filter(byte[] readBuffer, int offset, int length, bool toBeCopied, out int rest) 
    { 
     rest = 0; 

     try 
     { 
      var dataUnicode = Encoding.Unicode.GetString(readBuffer, offset, length); 
      var deviceRequest = new MyRequestInfo { Unicode = dataUnicode }; 
      return deviceRequest; 
     } 
     catch (Exception ex) 
     { 
      return null; 
     } 
    } 

    public void Reset() 
    { 
     throw new NotImplementedException(); 
    } 

    public int LeftBufferSize { get; } 
    public IReceiveFilter<MyRequestInfo> NextReceiveFilter { get; } 
    public FilterState State { get; } 
} 

次のステップは、カスタムAppSessionを作成することです。セッションとは、クライアントがサーバーに接続してセッションを作成したときのようなもので、クライアントの接続が切断されたときやサーバーが接続を終了したときに破棄されます。これは、クライアントの接続が必要な状況で、サーバーが次のメッセージを送信するための確認応答を送信する必要がある場合に適しています。

public class MyAppSession : AppSession<MyAppSession, MyRequestInfo> 
{ 
    // Properties related to your session. 

    public int ClientKey { get; set; } 

    public string SomeProperty { get; set; } 

} 

最後のステップは、あなたはまた、同様のAppServerクラスの他のメソッドをオーバーライドすることができ、カスタムAppServer

// Here you will be telling the AppServer to use MyAppSession as the default AppSession class and the MyRequestInfo as the defualt RequestInfo 

public class MyAppServer : AppServer<MyAppSession, MyRequestInfo> 
{ 
// Here in constructor telling to use MyReceiveFilter and MyRequestInfo 

    protected MyAppServer() : base(new DefaultReceiveFilterFactory<MyReceiveFilter, MyRequestInfo>()) 
    { 
     NewRequestReceived += ProcessNewMessage; 
    } 

    // This method/event will fire whenever a new message is received from the client/session 
    // After passing through the filter 
    // the requestInfo will contain the Unicode string 
    private void ProcessNewMessage(MyAppSession session, MyRequestInfo requestinfo) 
    { 
     session.ClientKey = SessionCount; 

     // Here you can access the Unicode strings that where generated in the MyReceiveFilter.Filter() Method. 

     Console.WriteLine(requestinfo.Unicode); 

     // Do whatever you want 

     session.Send("Hello World"); 


     session.Close(); 
    } 
} 

を作成することです:OnSessionClosedOnNewSessionConnected

それだこと - そして、あなただけの持っていますサーバを初期化して起動する:

  var myAppServer = new MyAppServer(); 

      if (!myAppServer.Setup(2012)) 
      { 
       _logger.LogMessage(MessageType.Error, string.Format("Failed to setup server")); 
       return; 
      } 
      if (!myAppServer.Start()) 
      { 
       _logger.LogMessage(MessageType.Error, string.Format("Failed to start server")); 
       return; 
      } 
+0

ありがとう、私は試してみます。 [編集]申し訳ありませんが、私の評判が十分に高くないので、私はあなたの答えをupvoteできません。 : 私は将来することができるときにそれをやるつもりです。私は約束します。 – tedebus

+1

私が約束したように、1年以上後に...私はあなたの答えをアップアップすることができます! – tedebus