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; namespace Holo.Source.GameConnectionSystem { public class gameConnection { #region Declares private Socket ClientSocket; private byte[] dataBuffer; private int _ConnectionID; private virtualUser ConnectedUser; private messageHandler Handler; private bool SocketClosed = false; #endregion #region Constructor public gameConnection(Socket _ClientSocket, int _ConnectionID) { this.ConnectedUser = new virtualUser(this); this.ClientSocket = _ClientSocket; this._ConnectionID = _ConnectionID; this.dataBuffer = new byte[1024]; this.ClientSocket.BeginReceive(this.dataBuffer, 0, this.dataBuffer.Length, SocketFlags.None, new AsyncCallback(this.dataArrival), null); ConnectedUser.pingOK = true; sendData("@@"); } #endregion #region Properities internal string connectionRemoteIP { get { return ClientSocket.RemoteEndPoint.ToString().Split(':')[0]; } } #endregion #region Methods //internal void PreformStatusCheck() //{ // if (this.ClientSocket.Connected == false) // { // Close(); // Out.WriteLine("Connection" + _ConnectionID + " is dead"); // } // else // { // ConnectedUser.pingOK = true; // sendData("@@"); // Out.WriteLine("Connection" + _ConnectionID + " is ready for connections"); // if (ConnectedUser.Room != null) // ConnectedUser.Room.removeUser(ConnectedUser.roomUser.roomUID, true, "An error has occured, please reload the room."); // } // } internal void dataArrival(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 = new messageHandler(DataBuffer.ToString().Substring(3, v)); ConnectedUser.processPacket(Handler); //string NewData = DataBuffer.ToString().Substring(v + 3); DataBuffer.Remove(0, 3 + v); //DataBuffer.Append(NewData); } } catch (Exception e) { Out.WriteDCError(e.ToString()); Close();/*PreformStatusCheck();*/ } finally { try { ClientSocket.BeginReceive(dataBuffer, 0, dataBuffer.Length, SocketFlags.None, new AsyncCallback(dataArrival), null); } catch { Close(); } } } internal void Close() { if (!SocketClosed) { SocketClosed = true; try { ClientSocket.Shutdown(SocketShutdown.Both); ClientSocket.Close(); } catch { } //Handler.Clear(); ConnectedUser.Reset(); //connectionHelper.RemoveConnection(_ConnectionID); gameSocketServer.freeConnection(_ConnectionID); //Crash(); } } internal void sendData(string Data) { try { //Out.WriteSpecialLine(Data.Replace("\r", "{13}"), Out.logFlags.MehAction, ConsoleColor.White, ConsoleColor.DarkCyan, "> []", 2, ConsoleColor.Cyan); byte[] dataBytes = System.Text.Encoding.Default.GetBytes(Data + Convert.ToChar(1)); ClientSocket.BeginSend(dataBytes, 0, dataBytes.Length, 0, new AsyncCallback(sentData), null); } catch { Close(); } } internal void sentData(IAsyncResult iAr) { try { ClientSocket.EndSend(iAr); } catch { Close(); } } internal int ConnectionID { get { return this._ConnectionID; } } #endregion } }