using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using Holo.Virtual.Users; using Holo.Socketservers; using Holo.GameConnectionSystem; using Holo.Source.Socket_servers.Game_Socketsystem; using System.Threading; namespace Holo.Source.GameConnectionSystem { public class gameConnection { #region Declares private Socket ClientSocket; private byte[] dataBuffer = new byte[1024]; private int _ConnectionID; private virtualUser ConnectedUser; private messageHandler Handler; private bool SocketClosed = false; private bool SocketReceivedPing = true; private AsyncCallback Callback; #endregion #region Constructor public gameConnection(Socket _ClientSocket, int _ConnectionID) { this.Handler = new messageHandler(this); this.ConnectedUser = new virtualUser(this); Handler.SetUser(ConnectedUser); ConnectedUser.SetHandler(Handler); this.ClientSocket = _ClientSocket; this._ConnectionID = _ConnectionID; ConnectedUser.pingOK = true; Callback = new AsyncCallback(ReceivedData); WaitForData(); Ping(); connectionHelper.AddConnection(this, _ConnectionID); } #endregion #region Properities internal string connectionRemoteIP { get { return ClientSocket.RemoteEndPoint.ToString().Split(':')[0]; } } internal messageHandler RemoteHandler { get { return Handler; } } internal bool IsActive { get { try { return (bool)ClientSocket.Connected; } catch { return false; } } } internal int ConnectionID { get { return this._ConnectionID; } } #endregion #region Methods private void WaitForData() { if (!SocketClosed) { try { ClientSocket.BeginReceive(dataBuffer, 0, dataBuffer.Length, SocketFlags.None, Callback, null); } catch { Close(); } } else Close(); } internal void Ping() { if (!ClientSocket.Connected) Close(); else sendData("@@"); } private void ReceivedData(IAsyncResult iAr) { try { int bytesReceived = new int(); try { bytesReceived = ClientSocket.EndReceive(iAr); } catch { Close(); return; } StringBuilder DataBuffer = new StringBuilder(); DataBuffer.Append(System.Text.Encoding.Default.GetString(dataBuffer, 0, bytesReceived)); if (DataBuffer.ToString() == "" || DataBuffer.ToString().Contains("\x01") || DataBuffer.ToString().Contains("\x02") || DataBuffer.ToString().Contains("\x05") || DataBuffer.ToString().Contains("\x09")) { Close(); return; } while (DataBuffer.Length > 0) { int v = Encoding.decodeB64(DataBuffer.ToString().Substring(1, 2)); Handler.ProcessPacket(DataBuffer.ToString().Substring(3, v)); DataBuffer.Remove(0, 3 + v); } } catch { Close(); } finally { WaitForData(); } } internal void Close() { if (!SocketClosed) { SocketClosed = true; try { ClientSocket.Shutdown(SocketShutdown.Both); ClientSocket.Close(); gameSocketServer.freeConnection(_ConnectionID); } catch { } ConnectedUser.Reset(); connectionHelper.RemoveConnection(_ConnectionID); Crash(); } } internal void sendData(string Data) { try { if (bool.Parse(Eucalypt.showPackets) == true) Out.WriteLine(Data.Replace("\r", ""), ConsoleColor.DarkYellow); byte[] dataBytes = System.Text.Encoding.Default.GetBytes(Data + Convert.ToChar(1)); ClientSocket.BeginSend(dataBytes, 0, dataBytes.Length, 0, new AsyncCallback(sentData), null); } catch { Close(); } } private void sentData(IAsyncResult iAr) { try { ClientSocket.EndSend(iAr); } catch { Close(); } } internal void Pinged() { this.SocketReceivedPing = true; } private void Crash() { ClientSocket = null; dataBuffer = null; _ConnectionID = 0; ConnectedUser = null; Handler = null; SocketClosed = true; SocketReceivedPing = false; Callback = null; } #endregion } }