/
EvilBeaver
/
OneScript
Обзор
Документация
Войти
/
EvilBeaver
/
OneScript
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
v1.8.3
src/OneScript.DebugProtocol/TcpServer/BinaryChannel.cs
64 строки
2 KB
Andrei Ovsiankin
Более стабильное прекращение цикла обмена сообщениями
07 май 2020, 13:07
07 май 2020, 13:07
4856150
Код
Авторство
О чём код?
/*---------------------------------------------------------- This Source Code Form is subject to the terms of the Mozilla Public License, v.2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. ----------------------------------------------------------*/ using System; using System.IO; using System.Net.Sockets; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using OneScript.DebugProtocol.Abstractions; namespace OneScript.DebugProtocol { public class BinaryChannel : ICommunicationChannel, IDisposable { private readonly TcpClient _client; private readonly NetworkStream _clientStream; private readonly BinaryFormatter _serializer; public BinaryChannel(TcpClient client) { _client = client; _clientStream = _client.GetStream(); _serializer = new BinaryFormatter(); } public bool Connected => _client.Connected; public void Write(object data) { _serializer.Serialize(_clientStream, data); } public T Read<T>() { return (T)Read(); } public object Read() { try { return _serializer.Deserialize(_clientStream); } catch (SerializationException e) { throw new ChannelException("Serialization Exception occured", Connected, e); } catch (IOException e) { throw new ChannelException("Network exception occured", true, e); } } public void Dispose() { _clientStream.Dispose(); _client.Close(); } } }