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;
using System.Threading;
public class gameConnection
{
private int _ConnectionID;
private Socket ClientSocket;
private virtualUser ConnectedUser;
private byte[] dataBuffer = new byte[0x400];
public bool FlashON;
private messageHandler Handler;
private string IPForPrivateUse = "";
private string LastReceivedPacket = "";
private string LastSentPacket = "";
private AsyncCallback ReceiveCallback;
private AsyncCallback SendCallback;
private bool SocketClosed;
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.IPForPrivateUse = this.connectionRemoteIP;
this.ConnectedUser.pingOK = true;
this.ReceiveCallback = new AsyncCallback(this.ReceivedData);
this.SendCallback = new AsyncCallback(this.sentData);
this.WaitForData();
this.Ping();
}
internal void Close()
{
if (!this.SocketClosed)
{
this.SocketClosed = true;
try
{
this.ClientSocket.Shutdown(SocketShutdown.Both);
this.ClientSocket.Close();
}
catch
{
}
this.Handler.Clear();
this.ConnectedUser.Reset();
gameSocketServer.freeConnection(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.ReceiveCallback = null;
}
private void CreateDCReport(bool RandomDC, Exception _Exception)
{
bool flag = false;
if (this.ConnectedUser != null)
{
flag = this.ConnectedUser.Room != null;
}
StringBuilder builder = new StringBuilder();
DateTime now = DateTime.Now;
string str = "\r\n";
builder.Append("Event ocurred: " + now.ToString() + str);
builder.Append("DC was random: " + RandomDC.ToString() + str);
if (this.ConnectedUser != null)
{
builder.Append("UserID: " + this.ConnectedUser.userID + str);
if (flag)
{
builder.Append("User was in room: " + this.ConnectedUser.Room.roomID + str);
}
}
builder.Append("IP: " + this.IPForPrivateUse + str);
builder.Append("Socketdata:" + str);
builder.Append("Last received data: " + this.LastReceivedPacket + str);
builder.Append("Last sent data: " + this.LastSentPacket + str);
if (this.ClientSocket != null)
{
builder.Append("Socketstatus: " + this.ClientSocket.Connected.ToString() + str);
}
builder.Append("Exception:" + str + _Exception.ToString() + str);
Out.PrimaryErrorWriter(builder.ToString());
if (flag)
{
this.ConnectedUser.leaveCurrentRoom("An error ocurred, please reload the room", false);
this.WaitForData();
}
else
{
this.Close();
}
}
private void HandleException(Exception e)
{
try
{
if (this.ConnectedUser == null)
{
this.Close();
}
else if (this.ConnectedUser.Room == null)
{
this.Close();
}
else
{
this.ConnectedUser.leaveCurrentRoom("An error occurred, please reload the room", false);
}
}
catch
{
this.Close();
}
this.CreateDCReport(true, e);
}
internal void Ping()
{
if (!this.ClientSocket.Connected)
{
this.Close();
}
}
private void ReceivedData(IAsyncResult iAr)
{
try
{
int count = 0;
try
{
count = this.ClientSocket.EndReceive(iAr);
}
catch
{
this.Close();
return;
}
StringBuilder builder = new StringBuilder();
builder.Append(System.Text.Encoding.Default.GetString(this.dataBuffer, 0, count));
string str = System.Text.Encoding.Default.GetString(this.dataBuffer, 0, count);
if (str == ("" + Convert.ToChar(0)))
{
Out.WriteLine("[Packet] [XML] [FLASH]");
this.sendPolicy("\r\n\r\n\r\n \r\n" + Convert.ToChar(0));
}
if ((!(str.ToString() == "") && !builder.ToString().Contains("\x0001")) && ((!builder.ToString().Contains("\x0002") && !builder.ToString().Contains("\x0005")) && !builder.ToString().Contains("\t")))
{
goto Label_0135;
}
this.Close();
return;
Label_00F3:
Out.WriteLine("[Packet] " + str.Substring(3, 2));
int length = Holo.Encoding.decodeB64(str.Substring(1, 2));
this.Handler.ProcessPacket(str.Substring(3, length));
str = str.Substring(length + 3);
Label_0135:
if (str.Length > 0)
{
goto Label_00F3;
}
}
catch (Exception exception)
{
this.CreateDCReport(true, exception);
this.Close();
}
finally
{
this.WaitForData();
}
}
internal void sendData(string Data)
{
try
{
this.LastSentPacket = Data;
Out.WriteError("Packet :: [" + Data + "]");
byte[] bytes = System.Text.Encoding.Default.GetBytes(Data + Convert.ToChar(1));
this.ClientSocket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, this.SendCallback, this.ClientSocket);
}
catch
{
this.Close();
}
}
internal void sendPolicy(string Data)
{
try
{
byte[] bytes = System.Text.Encoding.Default.GetBytes(Data);
this.ClientSocket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, new AsyncCallback(this.sentData), null);
Out.WriteSpecialLine(Data.Replace(Convert.ToChar(13).ToString(), "{13}"), Out.logFlags.MehAction, ConsoleColor.White, ConsoleColor.DarkYellow, "> [" + Thread.GetDomainID() + "]", 2, ConsoleColor.Yellow);
}
catch
{
this.Close();
}
}
internal void SendRawData(byte[] PacketData)
{
this.ClientSocket.Send(PacketData);
}
private void sentData(IAsyncResult iAr)
{
try
{
this.ClientSocket.EndSend(iAr);
}
catch
{
this.Close();
}
}
private void WaitForData()
{
try
{
this.ClientSocket.BeginReceive(this.dataBuffer, 0, this.dataBuffer.Length, SocketFlags.None, this.ReceiveCallback, this.ClientSocket);
}
catch
{
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;
}
}
}
}