what about this back ground worker ?Quote:
and these ??
You know, sometimes you need to step into, not step over. Like when you see a shit lying down, in order to check it out, step into it, don't step over it, or you might miss to see that that's a real shit.Quote:
what about this back ground worker ?
the issue is when i send a packet to the client when the session is started the client dont receive it till the session is end and when i break pointed the coming packet in the client i kept press F10 till the last line and its give me this screen ...
HandForm = new HandForm(this, client);
HandForm.UserForm.AdminControlIsVisible = false;
HandForm.UserForm.UpdateStart();
HandForm.ShowDialog(this);
case Data.Enums.ClientUser.Guest:
{
switch (client.Session.Limited)
{
case true:
{
this.Hide();
HandForm = new HandForm(this, client);
HandForm.UserForm.AdminControlIsVisible = false;
HandForm.UserForm.UpdateStart();
HandForm.ShowDialog(this);
break;
}
case false:
{
this.Hide();
HandForm = new HandForm(this, client);
HandForm.UserForm.AdminControlIsVisible = false;
HandForm.UserForm.UpdateStart();
HandForm.ShowDialog(this);
break;
}
}
break;
}
HandForm.ShowDialog(this);
public class AsyncSocket
{
public Socket Connection, HashSocket;
public event Action<AsyncSocketWrapper> OnClientConnect;
public event Action<AsyncSocketWrapper> OnClientDisconnect;
public event Action<byte[], AsyncSocketWrapper> OnClientReceive;
public AsyncSocket()
{
Connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
HashSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
public bool Connect(string IP, int Port)
{
IPAddress ipAddress = IPAddress.Parse(IP);
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, Port);
while (!Connection.Connected)
{
try
{
HashSocket.Connect(ipEndPoint);
if (HashSocket.Connected)
{
Connection.BeginConnect(ipEndPoint, new AsyncCallback(AsyncConnect), null);
System.Threading.Thread.Sleep(2000);
if (Connection.Connected)
{
return true;
}
}
}
catch
{
System.Threading.Thread.Sleep(2000);
}
}
return false;
}
private void AsyncConnect(IAsyncResult res)
{
try
{
AsyncSocketWrapper sender = null;
sender = new AsyncSocketWrapper();
sender.BufferSize = 8000;
sender.Socket = this.Connection;
sender.Buffer = new byte[sender.BufferSize];
this.Connection.EndConnect(res);
if (this.OnClientConnect != null)
{
this.OnClientConnect(sender);
}
sender.Socket.BeginReceive(sender.Buffer, 0, sender.Buffer.Length, SocketFlags.None, new AsyncCallback(this.AsyncReceive), sender);
}
catch (Exception ex)
{
Program.WriteLine(ex);
}
}
private void OnSend(IAsyncResult res)
{
try
{
Connection.EndSend(res);
byte[] byteData = new byte[1024];
Connection.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(AsyncReceive), null);
}
catch (Exception ex)
{
Program.WriteLine(ex);
}
}
private void AsyncReceive(IAsyncResult res)
{
bool was = false;
try
{
SocketError error;
AsyncSocketWrapper asyncState = (AsyncSocketWrapper)res.AsyncState;
int RecvSize = asyncState.Socket.EndReceive(res, out error);
if ((error == SocketError.Success) && (RecvSize > 0))
{
was = true;
byte[] buffer = new byte[RecvSize];
for (int i = 0; i < RecvSize; i++)
{
buffer[i] = asyncState.Buffer[i];
}
if (this.OnClientReceive != null)
{
this.OnClientReceive(buffer, asyncState);
}
asyncState.Socket.BeginReceive(asyncState.Buffer, 0, asyncState.Buffer.Length, SocketFlags.None, new AsyncCallback(this.AsyncReceive), asyncState);
}
else
{
this.InvokeDisconnect(asyncState);
}
}
catch (SocketException e)
{
Program.WriteLine(e);
if (was)
{
AsyncSocketWrapper asyncState = (AsyncSocketWrapper)res.AsyncState;
asyncState.Socket.BeginReceive(asyncState.Buffer, 0, asyncState.Buffer.Length, SocketFlags.None, new AsyncCallback(this.AsyncReceive), asyncState);
}
}
catch (ObjectDisposedException e)
{
Program.WriteLine(e);
}
catch (Exception ex)
{
Program.WriteLine(ex);
}
}
public void InvokeDisconnect(AsyncSocketWrapper Client)
{
if (Client != null)
{
try
{
if (Client.Socket.Connected)
{
Client.Socket.Shutdown(SocketShutdown.Both);
Client.Socket.Close();
if (this.OnClientDisconnect != null)
{
this.OnClientDisconnect(Client);
}
Client.Connector = null;
Client = null;
}
else
{
if (this.OnClientDisconnect != null)
{
this.OnClientDisconnect(Client);
}
Client.Connector = null;
Client = null;
}
}
catch (ObjectDisposedException e)
{
Program.WriteLine(e);
}
}
}
}
class Program
{
static MainForm MF;
public static Network.Sockets.AsyncSocket AuthServer;
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(MF = new MainForm());
}
public static void StartEngine()
{
AuthServer = new AsyncSocket();
AuthServer.OnClientConnect += new Action<AsyncSocketWrapper>(AuthServer_AnnounceNewConnection);
AuthServer.OnClientReceive += new Action<byte[], AsyncSocketWrapper>(AuthServer_AnnounceReceive);
AuthServer.OnClientDisconnect += new Action<AsyncSocketWrapper>(AuthServer_AnnounceDisconnection);
if (AuthServer.Connect("192.168.1.100", 9958))
{
GC.Collect();
}
}
public static void WriteLine(string Text)
{
//MF.WriteLine(Text);
}
public static void WriteLine(Exception Text)
{
//MF.WriteLine(Text.Message);
}
public static void WriteLine(uint Text)
{
//MF.WriteLine(Text.ToString());
}
static void AuthServer_AnnounceNewConnection(AsyncSocketWrapper obj)
{
Client.State client = new Client.State(obj.Socket, MF);
obj.Connector = client;
if (MF.client == null)
{
MF.client = client;
}
Network.Packets.ComputerInformation ComputerInformation = new Network.Packets.ComputerInformation(true, client);
ComputerInformation.Send(client);
}
static void AuthServer_AnnounceDisconnection(AsyncSocketWrapper obj)
{
if (obj != null)
{
Client.State client = (Client.State)obj.Connector;
if (client != null)
{
client.Reconnect();
}
}
}
static void AuthServer_AnnounceReceive(byte[] packet, AsyncSocketWrapper obj)
{
try
{
Client.State client = (Client.State)obj.Connector;
Network.PacketHandler.Handle(packet, client);
}
catch (Exception e)
{
Program.WriteLine(e.ToString());
}
}
}
public static void Handle(byte[] packet, Client.State client)
{
if (packet == null)
return;
if (client == null)
return;
ushort ID = BitConverter.ToUInt16(packet, 0);
ushort Length = BitConverter.ToUInt16(packet, 2);
switch (ID)
{
case 1003:
{
Message message = new Message();
message.Deserialize(packet);
if (message.MESSAGE_CONTENT.Split(new string[] { "\\n" }, StringSplitOptions.RemoveEmptyEntries).Length > 0)
message.MESSAGE_CONTENT = message.MESSAGE_CONTENT.Split(new string[] { "\\n" }, StringSplitOptions.RemoveEmptyEntries)[0];
Chat(message, client);
break;
}
case 1005:
{
Network.Packets.StartSession StartSession = new Network.Packets.StartSession();
StartSession.Deserialize(packet);
client.Session = new Data.Session(client, StartSession);
client.Session.Start();
break;
}
case 1007:
{
Network.Packets.EditSession EditSession = new Network.Packets.EditSession();
EditSession.Deserialize(packet);
client.Session.Edit(EditSession.Add, EditSession.Time);
break;
}
default:
{
client.Send(packet);
break;
}
}
}
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
public Client.State client;
public HandForm HandForm;
private void BGW_DoWork(object sender, DoWorkEventArgs e)
{
Program.StartEngine();
}
#region Form Load / Close
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
}
private void MainForm_Load(object sender, EventArgs e)
{
FlashWallpaper.Play();
BGW.RunWorkerAsync();
}
#endregion
#region Session Handle
public void LogIn(Client.State _client)
{
switch (client.PC_USER)
{
case Data.Enums.ClientUser.Admin:
{
this.Hide();
HandForm = new HandForm(this, client);
HandForm.UserForm.AdminControlIsVisible = true;
HandForm.UserForm.UpdateStart();
HandForm.ShowDialog(this);
}
break;
case Data.Enums.ClientUser.Guest:
{
switch (client.Session.Limited)
{
case true:
{
this.Hide();
HandForm = new HandForm(this, client);
HandForm.UserForm.AdminControlIsVisible = false;
HandForm.UserForm.UpdateStart();
HandForm.ShowDialog(this);
break;
}
case false:
{
this.Hide();
HandForm = new HandForm(this, client);
HandForm.UserForm.AdminControlIsVisible = false;
HandForm.UserForm.UpdateStart();
HandForm.ShowDialog(this);
break;
}
}
break;
}
}
}
public void Pause(Client.State _client)
{
this.LoginTimer.Stop();
this.Show();
this.HandForm.Close();
}
public void Resume(Client.State _client)
{
}
public void LogOut(Client.State _client)
{
this.LoginTimer.Stop();
this.Show();
this.HandForm.Close();
}
#endregion
}
public partial class HandForm : Form
{
public UserForm UserForm;
public MainForm MainForm;
public Client.State client;
bool Opened = false;
SlidingController slider = null;
public HandForm(MainForm mainForm, Client.State client)
{
InitializeComponent();
this.MainForm = mainForm;
this.client = client;
UserForm = new UserForm(mainForm, client);
slider = new SlidingController(UserForm, this);
}
private void HandForm_Load(object sender, EventArgs e)
{
UserForm.Show();
slider.Initialize();
}
private void HandForm_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (Opened)
{
slider.DrawUndraw();
this.BackgroundImage = HotCafe_Client.Properties.Resources.Show;
Opened = false;
}
else
{
slider.DrawUndraw();
this.BackgroundImage = HotCafe_Client.Properties.Resources.Hide;
Opened = true;
}
}
}
private void HandForm_FormClosing(object sender, FormClosingEventArgs e)
{
UserForm.Close();
}
}
public partial class UserForm : Form
{
private Client.State client;
private MainForm MF;
public UserForm(MainForm mainForm, Client.State client)
{
this.MF = mainForm;
this.client = client;
InitializeComponent();
}
public bool AdminControlIsVisible
{
set { this.Exit_Key.Enabled = value; this.Options_Key.Enabled = value; }
}
private void Options_Key_Click(object sender, EventArgs e)
{
client.Session.Pause();
}
private void Exit_Key_Click(object sender, EventArgs e)
{
}
private void Logout_Key_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure that you want to end the session ?", "HotCafe", MessageBoxButtons.YesNo, MessageBoxIcon.Question ) == System.Windows.Forms.DialogResult.Yes)
{
client.Session.End();
}
}
}
}
[Only registered and activated users can see links. Click Here To Register...]Quote:
i got it i can receive the packets even if i hided the main form but it's not receiving packet when i show the hand form ...
so how to fix it keeping it receive packet if i am showing multiple winforms ?Quote:
No, ShowDialog indeed blocks the thread from which it is called.
class Program
{
static MainForm MF;
public static Network.Sockets.AsyncSocket AuthServer;
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(MF = new MainForm());
}
public static void StartEngine()
{
AuthServer = new AsyncSocket();
AuthServer.OnClientConnect += new Action<AsyncSocketWrapper>(AuthServer_AnnounceNewConnection);
AuthServer.OnClientReceive += new Action<byte[], AsyncSocketWrapper>(AuthServer_AnnounceReceive);
AuthServer.OnClientDisconnect += new Action<AsyncSocketWrapper>(AuthServer_AnnounceDisconnection);
if (AuthServer.Connect("192.168.1.100", 9958))
{
GC.Collect();
}
}
public static void WriteLine(string Text)
{
//MF.WriteLine(Text);
}
public static void WriteLine(Exception Text)
{
//MF.WriteLine(Text.Message);
}
public static void WriteLine(uint Text)
{
//MF.WriteLine(Text.ToString());
}
static void AuthServer_AnnounceNewConnection(AsyncSocketWrapper obj)
{
Client.State client = new Client.State(obj.Socket, MF);
obj.Connector = client;
if (MF.client == null)
{
MF.client = client;
}
Network.Packets.ComputerInformation ComputerInformation = new Network.Packets.ComputerInformation(true, client);
ComputerInformation.Send(client);
}
static void AuthServer_AnnounceDisconnection(AsyncSocketWrapper obj)
{
if (obj != null)
{
Client.State client = (Client.State)obj.Connector;
if (client != null)
{
client.Reconnect();
}
}
}
static void AuthServer_AnnounceReceive(byte[] packet, AsyncSocketWrapper obj)
{
try
{
Client.State client = (Client.State)obj.Connector;
Network.PacketHandler.Handle(packet, client);
}
catch (Exception e)
{
Program.WriteLine(e.ToString());
}
}
}
public static class PacketHandler
{
public static void Handle(byte[] packet, Client.State client)
{
if (packet == null)
return;
if (client == null)
return;
ushort ID = BitConverter.ToUInt16(packet, 0);
ushort Length = BitConverter.ToUInt16(packet, 2);
switch (ID)
{
case 1003:
{
Message message = new Message();
message.Deserialize(packet);
if (message.MESSAGE_CONTENT.Split(new string[] { "\\n" }, StringSplitOptions.RemoveEmptyEntries).Length > 0)
message.MESSAGE_CONTENT = message.MESSAGE_CONTENT.Split(new string[] { "\\n" }, StringSplitOptions.RemoveEmptyEntries)[0];
Chat(message, client);
break;
}
case 1005:
{
Network.Packets.StartSession StartSession = new Network.Packets.StartSession();
StartSession.Deserialize(packet);
client.Session = new Data.Session(client, StartSession);
client.Session.Start();
break;
}
case 1007:
{
Network.Packets.EditSession EditSession = new Network.Packets.EditSession();
EditSession.Deserialize(packet);
client.Session.Edit(EditSession.Add, EditSession.Time);
break;
}
default:
{
client.Send(packet);
break;
}
}
}
private static void Chat(Message message, Client.State client)
{
switch (message.MESSAGE_TYPE)
{
case Data.Enums.ChatType.System:
{
string M = message.MESSAGE_CONTENT.Replace("\0", "").Split('\0')[0];
switch (M)
{
case "ADMIN_LOGIN_NO":
{
client.MF.UpdateSystemMessage("Invalid Username or Password!!");
break;
}
case "LOGIN_REQUEST_REJECTED":
{
client.MF.UpdateSystemMessage("Login request has been rejected!!");
break;
}
}
break;
}
case Data.Enums.ChatType.Whisper:
{
break;
}
}
}
}
[Only registered and activated users can see links. Click Here To Register...]Quote:
PHP Code:if (AuthServer.Connect("192.168.1.100", 9958))
{
GC.Collect();
}