namespace Holo.Source.GameConnectionSystem { using Holo; using Holo.GameConnectionSystem; using Holo.Source.Socket_servers.Game_Socketsystem; using Holo.Virtual.Users; using System; using System.Net.Sockets; using System.Text; public class gameConnection { private int _ConnectionID; private AsyncCallback Callback; private Socket ClientSocket; private virtualUser ConnectedUser; private byte[] dataBuffer = new byte[0x400]; private messageHandler Handler; private bool SocketClosed = false; private bool SocketReceivedPing = true; public gameConnection(Socket _ClientSocket, int _ConnectionID) { this.Handler = new messageHandler(this); this.ConnectedUser = new virtualUser(this); this.Handler.SetUser(this.ConnectedUser); this.ConnectedUser.SetHandler(this.Handler); this.ClientSocket = _ClientSocket; this._ConnectionID = _ConnectionID; this.ConnectedUser.pingOK = true; this.Callback = new AsyncCallback(this.ReceivedData); this.WaitForData(); this.Ping(); connectionHelper.AddConnection(this, _ConnectionID); } internal void Close() { if (!this.SocketClosed) { this.SocketClosed = true; try { this.ClientSocket.Shutdown(SocketShutdown.Both); this.ClientSocket.Close(); gameSocketServer.freeConnection(this._ConnectionID); } catch { } this.ConnectedUser.Reset(); connectionHelper.RemoveConnection(this._ConnectionID); this.Crash(); } } private void Crash() { this.ClientSocket = null; this.dataBuffer = null; this._ConnectionID = 0; this.ConnectedUser = null; this.Handler = null; this.SocketClosed = true; this.SocketReceivedPing = false; this.Callback = null; } internal void Ping() { if (!this.ClientSocket.Connected) { this.Close(); } else { this.sendData("@@"); } } internal void Pinged() { this.SocketReceivedPing = true; } private void ReceivedData(IAsyncResult iAr) { try { int bytesReceived = 0; try { bytesReceived = this.ClientSocket.EndReceive(iAr); } catch { this.Close(); return; } StringBuilder DataBuffer = new StringBuilder(); DataBuffer.Append(Encoding.Default.GetString(this.dataBuffer, 0, bytesReceived)); if ((((DataBuffer.ToString() == "") || DataBuffer.ToString().Contains("\x0001")) || (DataBuffer.ToString().Contains("\x0002") || DataBuffer.ToString().Contains("\x0005"))) || DataBuffer.ToString().Contains("\t")) { this.Close(); } else { while (DataBuffer.Length > 0) { int v = Encoding.decodeB64(DataBuffer.ToString().Substring(1, 2)); this.Handler.ProcessPacket(DataBuffer.ToString().Substring(3, v)); DataBuffer.Remove(0, 3 + v); } } } catch (Exception) { this.Close(); } finally { this.WaitForData(); } } internal void sendData(string Data) { try { byte[] dataBytes = Encoding.Default.GetBytes(Data + Convert.ToChar(1)); this.ClientSocket.BeginSend(dataBytes, 0, dataBytes.Length, SocketFlags.None, new AsyncCallback(this.sentData), null); } catch { this.Close(); } } private void sentData(IAsyncResult iAr) { try { this.ClientSocket.EndSend(iAr); } catch { this.Close(); } } private void WaitForData() { if (!this.SocketClosed) { try { this.ClientSocket.BeginReceive(this.dataBuffer, 0, this.dataBuffer.Length, SocketFlags.None, this.Callback, null); } catch { this.Close(); } } else { this.Close(); } } internal int ConnectionID { get { return this._ConnectionID; } } internal string connectionRemoteIP { get { return this.ClientSocket.RemoteEndPoint.ToString().Split(new char[] { ':' })[0]; } } internal bool IsActive { get { try { return this.ClientSocket.Connected; } catch { return false; } } } internal messageHandler RemoteHandler { get { return this.Handler; } } } }