Java のソケットから Google Protocol Buffer メッセージを読み取ろうとするコードがいくつかあります。ただし、mergeDelimitedFrom() メソッドは、無効なデータを読み取った場合、またはソケット接続がリセットされた場合 (およびおそらくその他の理由)、IOException をスローする可能性があります。接続がリセットされた場合はループを終了したいのですが、それが無効なメッセージである場合は実行を続けたいと思います。1 つの考えとしては、ある種の例外カウンターを用意し、X 回連続して失敗した後に終了することですが、暗闇の中にいるのではなく、どのような種類のエラーが発生したかを把握できるようにしたいと考えていました。
これは基本的に私が持っているコードです:
while (m_Running)
{
SomeMessage message = null;
try
{
final Builder builder = SomeMessage.newBuilder();
if (builder.mergeDelimitedFrom(m_InputStream))
{
message = builder.build();
}
else
{
// Google protocol buffers doesn't document it very well
// but if mergeDelimietedFrom returns false then it has
// reached the end of the input stream. For a socket, no
// more data will be coming so exit from the thread
m_Running = false;
}
}
catch (final IOException e)
{
// what should really be done here ???
}
}