using ConsoleApp3.Contracts; using OpenQA.Selenium.DevTools; using OpenQA.Selenium.Support.UI; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; namespace CardMarketBot { internal class CheckSevDeskPublicList : ICheckSevDesk { private static readonly object _lock = new object(); private static bool _closed; enum EResult { FAILED, AVAIBLE, OK, WRITTEN } private static void Write(TcpClient client, string str) { try { //SocketShutdown reason = SocketShutdown.Send; lock (_lock) { BinaryWriter writer = new BinaryWriter(client.GetStream()); writer.Write(str); if (_closed) { //reason = SocketShutdown.Both; } } //client.Client.Shutdown(reason); } catch (IOException e) { Console.WriteLine($"IOException writing to socket: {e.Message}"); } } private static EResult Read(TcpClient client) { try { //while(true) //{ try { if (!client.Connected) { lock (_lock) { _closed = true; //break; } } BinaryReader reader = new BinaryReader(client.GetStream()); string res = reader.ReadString(); if (res.Contains("OK")) return EResult.OK; return EResult.FAILED; } catch (EndOfStreamException) { lock (_lock) { _closed = true; } return EResult.FAILED; } //client.Client.Shutdown(SocketShutdown.Receive); //} } catch (IOException e) { Console.WriteLine(e.Message); return EResult.FAILED; } } public bool AlreadyKnown(string verkaufsnummer) { try { TcpClient client = new TcpClient("nas.cosysda.de", 4000); Write(client, string.Format("#TEST#{0}", verkaufsnummer)); EResult result = Read(client); client.Client.Shutdown(SocketShutdown.Both); client.Close(); return result == EResult.OK; } catch(SocketException) { throw new SocketException(); } } public bool Write(string verkaufsnummer) { TcpClient client = new TcpClient("nas.cosysda.de", 4000); Write(client, string.Format("#WRITE#{0}", verkaufsnummer)); EResult result = Read(client); client.Client.Shutdown(SocketShutdown.Both); client.Close(); return result == EResult.OK; } } }