private void ReadServerData() { if (IsConnected) { ServerS.BeginReceive(ServerB, 0, ServerB.Length, SocketFlags.None, DataFromServer, null); } }
private void Connected(IAsyncResult iAr) { ServerS.EndConnect(iAr); ServerB = new byte[1024]; ClientB = new byte[512]; ReadClientData(); ReadServerData(); }
public override int SendToServer(byte[] Data) { if (IsConnected) { lock (SendToServerLock) { if (ClientEncrypt != null) { Data = ClientEncrypt.SafeParse(Data); } return(ServerS.Send(Data)); } } else { return(0); } }
public void Disconnect() { lock (DisconnectLock) { if (!DisconnectAllowed) { return; } DisconnectAllowed = false; if (ClientS != null) { ClientS.Shutdown(SocketShutdown.Both); ClientS.Close(); ClientS = null; } if (ServerS != null) { ServerS.Shutdown(SocketShutdown.Both); ServerS.Close(); ServerS = null; } ResetHost(); if (TCP != null) { TCP.Stop(); TCP = null; } Protocol = HProtocols.Unknown; ToClientS = ToServerS = SocketCount = 0; ClientB = ServerB = SCache = CCache = null; HasOfficialSocket = RequestEncrypted = ResponseEncrypted = false; ClientEncrypt = ClientDecrypt = ServerEncrypt = ServerDecrypt = null; if (OnDisconnected != null) { OnDisconnected(this, EventArgs.Empty); } } }
private void DataFromServer(IAsyncResult iAr) { try { if (ServerS == null) { return; } int Length = ServerS.EndReceive(iAr); if (Length > 0) { byte[] Data = ByteUtils.CopyBlock(ServerB, 0, Length); #region Official Socket Check if (!HasOfficialSocket) { SendToClient(Data); TCP.BeginAcceptSocket(SocketAccepted, null); return; } #endregion try { #region Decrypt/Split if (ServerDecrypt != null) { ServerDecrypt.Parse(Data); } if ((ToClientS + 1) == 3 && Protocol == HProtocols.Modern) { int DLength = BigEndian.DecypherInt(Data); ResponseEncrypted = (DLength > Data.Length - 4 || DLength < 6); } byte[][] Chunks = ResponseEncrypted ? new byte[1][] { Data } : ByteUtils.Split(ref CCache, Data, HDestinations.Client, Protocol); #endregion foreach (byte[] Chunk in Chunks) { ++ToClientS; if (DataToClient == null) { SendToClient(Chunk); } else { DataToEventArgs Args = new DataToEventArgs(Chunk, HDestinations.Client, ToClientS); try { DataToClient(this, Args); } catch { SendToClient(Chunk); continue; } if (!Args.Skip) { SendToClient(Args.Packet.ToBytes()); } } if (CaptureEvents && !ResponseEncrypted) { DataFromServer(Chunk); } } } catch { SendToClient(Data); } ReadServerData(); } else { Disconnect(); } } catch { Disconnect(); } }