public async void ConnectReciever(BTReaderWriter btRW) { try { await btRW.ConnectBTService(); ReceiveStringLoop(btRW); } catch (NullReferenceException ex) { Debug.WriteLine(ex.Message + " : BTサービス探索をリトライします。"); ConnectReciever(btRW); } catch (InvalidOperationException ex) { Debug.WriteLine(ex.Message + ":" + ex.InnerException.Message); ConnectReciever(btRW); } catch (Exception ex) { Debug.WriteLine("不明なエラー: " + ex.Message + ":" + ex.InnerException?.Message); ConnectReciever(btRW); } }
private async void ReceiveStringLoop(BTReaderWriter btRW) { var chatReader = btRW.BTReader; Debug.WriteLine("/////entering recieve string roop/////"); try { uint size = await chatReader.LoadAsync(sizeof(uint)); Debug.WriteLine("size check in ReceiveStringLoop"); if (size < sizeof(uint)) { //Disconnect(); //return; ReceiveStringLoop(btRW); } Debug.WriteLine("ReadUInt32 in ReceiveStringLoop"); uint stringLength = chatReader.ReadUInt32(); Debug.WriteLine("LoadAsync in ReceiveStringLoop"); uint actualStringLength = await chatReader.LoadAsync(stringLength); if (actualStringLength != stringLength) { // The underlying socket was closed before we were able to read the whole data throw new InvalidOperationException("The underlying socket was closed before we were able to read the whole data"); } //ConversationList.Items.Add("Received: " + chatReader.ReadString(stringLength)); //TODO: 受信した文字列のハンドル String resultString = chatReader.ReadString(stringLength); OnMessageRecieved(new BTMessageRecievedEventArgs(resultString)); Debug.WriteLine(resultString); ReceiveStringLoop(btRW); } catch (ObjectDisposedException ex) { Debug.WriteLine("BT接続がDisposeされています。再接続を試みます。: " + ex.Message); //disposedReaderWriters.Add(btReaderWriter); await btRW.ConnectBTService(); ReceiveStringLoop(btRW); } catch (Exception ex) { lock (this) { if (btRW.BTStreamSocket == null) { // Do not print anything here - the user closed the socket. if ((uint)ex.HResult == 0x80072745) { //rootPage.NotifyUser("Disconnect triggered by remote device", NotifyType.StatusMessage); } else if ((uint)ex.HResult == 0x800703E3) { //rootPage.NotifyUser("The I/O operation has been aborted because of either a thread exit or an application request.", NotifyType.StatusMessage); } } else { btRW.Disconnect(); Debug.WriteLine("サーバーとの接続が切断されました。: " + ex.Message + ":::" + ex.HResult); //throw new Exception("サーバーとの接続が解除されました。", ex); } } } }