public virtual void Connect() { if (ServerSide) { throw new SystemException("Serverside socket cannot connect to a remote peer!"); } NetSupport.DoStateCheck(IsAlive || (eventListener != null && eventListener.IsAlive), false); Connection = new Socket(SocketType.Stream, ProtocolType.Tcp); Connection.Connect(target, Port); Running = true; eventListener = new Thread(() => { bool cryptoEstablished = false; int mLen = 0; Queue <byte> ibuf = new Queue <byte>(); byte[] buffer = new byte[bufSize]; while (Running) { if (SyncListener(ref cryptoEstablished, ref mLen, out bool _, ibuf, buffer)) { break; } } if (ibuf.Count != 0) { Console.WriteLine("Client socket closed with unread data!"); } }) { Priority = ThreadPriority.Highest, Name = $"NetClient-${target}:${Port}" }; eventListener.Start(); }
/// <summary> /// Disconnect from server /// </summary> /// <returns></returns> public virtual async Task <object> Disconnect() { NetSupport.DoStateCheck(IsAlive, true); Running = false; return(await new TaskFactory().StartNew <object>(() => { eventListener.Join(); return null; })); }
public virtual void Send(byte[] message) { NetSupport.DoStateCheck(IsAlive, true); lock (messageBuffer) messageBuffer.Enqueue(Crypto.Encrypt(NetSupport.WithHeader(message))); }