public class TCPServer
{
// Fields
private static List<WeakReference> __ENCList = new List<WeakReference>();
private TcpClient IncomingClient;
private Thread ListenThread;
private List<Module1.Client> OnlineList;
private TcpListener ServerListener;
// Events
public event LogEventHandler Log;
public event LoginEventHandler Login;
public event LogoffEventHandler Logoff;
// Methods
public TCPServer([Optional, DefaultParameterValue(0x17de)] int port)
{
List<WeakReference> VB$t_ref$L0 = __ENCList;
lock (VB$t_ref$L0)
{
__ENCList.Add(new WeakReference(this));
}
this.OnlineList = new List<Module1.Client>();
this.ServerListener = new TcpListener(IPAddress.Any, port);
}
public void Dispose()
{
List<Module1.Client>.Enumerator VB$t_struct$L0;
IEnumerator VB$t_ref$L1;
try
{
VB$t_struct$L0 = this.OnlineList.GetEnumerator();
while (VB$t_struct$L0.MoveNext())
{
Module1.Client cli = VB$t_struct$L0.Current;
Message msg = new Message();
msg.Sender = "SERVER";
msg.Receiver = cli.Name;
msg.Message = "LOGOFF";
cli.SendMessage(msg);
object[] VB$t_array$S0 = new object[1];
Module1.Client VB$t_ref$S0 = cli;
VB$t_array$S0[0] = VB$t_ref$S0.Name;
object[] VB$t_array$S1 = VB$t_array$S0;
bool[] VB$t_array$S2 = new bool[] { true };
NewLateBinding.LateCall(this.UserInteraction(), null, "ChangeUserToOffline", VB$t_array$S1, null, null, VB$t_array$S2, true);
if (VB$t_array$S2[0])
{
VB$t_ref$S0.Name = (string) Conversions.ChangeType(RuntimeHelpers.GetObjectValue(VB$t_array$S1[0]), typeof(string));
}
}
}
finally
{
VB$t_struct$L0.Dispose();
}
try
{
VB$t_ref$L1 = ((IEnumerable) this.GroupList()).GetEnumerator();
while (VB$t_ref$L1.MoveNext())
{
((Groups) VB$t_ref$L1.Current).Dispose();
}
}
finally
{
if (VB$t_ref$L1 is IDisposable)
{
(VB$t_ref$L1 as IDisposable).Dispose();
}
}
NewLateBinding.LateCall(this.GroupList(), null, "Clear", new object[0], null, null, null, true);
this.ServerListener.Stop();
this.IncomingClient.Close();
this.ListenThread.Abort();
NewLateBinding.LateCall(this.UserInteraction(), null, "Dispose", new object[0], null, null, null, true);
this.OnlineList.Clear();
}
private void ErrorOut(Module1.Client cli, Exception err)
{
LogEventHandler VB$t_ref$S0 = this.LogEvent;
if (VB$t_ref$S0 != null)
{
VB$t_ref$S0(string.Format("Exception: {0}{1}Stack Trace: {2}", err.Message, Environment.NewLine, err.StackTrace));
}
this.OnlineList.Remove(cli);
object[] VB$t_array$S0 = new object[1];
Module1.Client VB$t_ref$S1 = cli;
VB$t_array$S0[0] = VB$t_ref$S1.Name;
object[] VB$t_array$S1 = VB$t_array$S0;
bool[] VB$t_array$S2 = new bool[] { true };
NewLateBinding.LateCall(this.UserInteraction(), null, "ChangeUserToOffline", VB$t_array$S1, null, null, VB$t_array$S2, true);
if (VB$t_array$S2[0])
{
VB$t_ref$S1.Name = (string) Conversions.ChangeType(RuntimeHelpers.GetObjectValue(VB$t_array$S1[0]), typeof(string));
}
LogoffEventHandler VB$t_ref$S2 = this.LogoffEvent;
if (VB$t_ref$S2 != null)
{
VB$t_ref$S2(cli);
}
cli.Dispose();
}
private object GetFriendList(Module1.Client cli)
{
throw new NotImplementedException();
}
private object GroupList()
{
throw new NotImplementedException();
}
private void IncomingMessage(Module1.Client cli, object obj, Type obj_type)
{
}
public void Initialize()
{
LogEventHandler VB$t_ref$S0 = this.LogEvent;
if (VB$t_ref$S0 != null)
{
VB$t_ref$S0("Starting Server....");
}
this.ServerListener.Start();
VB$t_ref$S0 = this.LogEvent;
if (VB$t_ref$S0 != null)
{
VB$t_ref$S0("Server initialized, creating Listener Thread.");
}
this.ListenThread = new Thread(new ThreadStart(this.Listener));
VB$t_ref$S0 = this.LogEvent;
if (VB$t_ref$S0 != null)
{
VB$t_ref$S0("Listener Thread Created, setting to Background.");
}
this.ListenThread.IsBackground = true;
VB$t_ref$S0 = this.LogEvent;
if (VB$t_ref$S0 != null)
{
VB$t_ref$S0("Starting listener...");
}
this.ListenThread.Start();
VB$t_ref$S0 = this.LogEvent;
if (VB$t_ref$S0 != null)
{
VB$t_ref$S0("Server Startup Complete.");
}
}
private void Listener()
{
while (true)
{
this.IncomingClient = this.ServerListener.AcceptTcpClient();
LingerOption LingerOption = new LingerOption(false, 2);
this.IncomingClient.LingerState = LingerOption;
Module1.Client Client = new Module1.Client(this.IncomingClient);
LogEventHandler VB$t_ref$S0 = this.LogEvent;
if (VB$t_ref$S0 != null)
{
VB$t_ref$S0("Client Accepted, registering handlers.");
}
Client.Message += new Module1.Client.MessageEventHandler(this.IncomingMessage);
Client.ErrorOut += new Module1.Client.ErrorOutEventHandler(this.ErrorOut);
VB$t_ref$S0 = this.LogEvent;
if (VB$t_ref$S0 != null)
{
VB$t_ref$S0("Adding to Online List.");
}
this.OnlineList.Add(Client);
VB$t_ref$S0 = this.LogEvent;
if (VB$t_ref$S0 != null)
{
VB$t_ref$S0("Waiting for Login Response.");
}
}
}
private object UserInteraction()
{
throw new NotImplementedException();
}
// Nested Types
public delegate void LogEventHandler(string message);
public delegate void LoginEventHandler(Module1.Client client);
public delegate void LogoffEventHandler(Module1.Client client);
}
public class Client
{
// Fields
private static List<WeakReference> __ENCList = new List<WeakReference>();
[AccessedThroughProperty("TimeoutTimer")]
private Timer _TimeoutTimer;
private BinaryFormatter BinFormatter;
private TcpClient m_Client;
private string m_Name;
private Thread readThread;
// Events
public event ErrorOutEventHandler ErrorOut;
public event MessageEventHandler Message;
// Methods
public Client(TcpClient client)
{
List<WeakReference> VB$t_ref$L0 = __ENCList;
lock (VB$t_ref$L0)
{
__ENCList.Add(new WeakReference(this));
}
this.m_Name = string.Empty;
Timer VB$t_ref$S1 = new Timer();
VB$t_ref$S1.Interval = 5000.0;
VB$t_ref$S1.AutoReset = true;
VB$t_ref$S1.Enabled = false;
this.TimeoutTimer = VB$t_ref$S1;
this.m_Client = client;
this.readThread = new Thread(new ThreadStart(this.client_doRead));
this.readThread.IsBackground = true;
this.readThread.Start();
this.TimeoutTimer.Enabled = true;
}
private void client_doRead()
{
while (true)
{
try
{
this.BinFormatter = new BinaryFormatter();
object obj = RuntimeHelpers.GetObjectValue(this.BinFormatter.Deserialize(this.m_Client.GetStream()));
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
Exception ex = exception1;
ErrorOutEventHandler VB$t_ref$S0 = this.ErrorOutEvent;
if (VB$t_ref$S0 != null)
{
VB$t_ref$S0(this, ex);
}
this.Dispose();
ProjectData.ClearProjectError();
}
}
}
public void Dispose()
{
this.m_Name = string.Empty;
this.BinFormatter = null;
this.m_Client.Client.Close();
try
{
this.readThread.Abort();
this.readThread = null;
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
ProjectData.ClearProjectError();
}
GC.Collect();
}
public void SendMessage(object message)
{
try
{
this.BinFormatter = new BinaryFormatter();
this.BinFormatter.Serialize(this.m_Client.GetStream(), RuntimeHelpers.GetObjectValue(message));
this.BinFormatter = null;
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
Exception ex = exception1;
ErrorOutEventHandler VB$t_ref$S0 = this.ErrorOutEvent;
if (VB$t_ref$S0 != null)
{
VB$t_ref$S0(this, ex);
}
ProjectData.ClearProjectError();
}
}
private void TimeoutTimer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
if (((!this.m_Client.GetStream().CanRead && !this.m_Client.GetStream().CanWrite) ? 1 : 0) != 0)
{
ErrorOutEventHandler VB$t_ref$S0 = this.ErrorOutEvent;
if (VB$t_ref$S0 != null)
{
VB$t_ref$S0(this, new Exception("The Client has timed out"));
}
this.Dispose();
}
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
Exception ex = exception1;
ProjectData.ClearProjectError();
}
}
// Properties
public string Name
{
get
{
return this.m_Name;
}
set
{
this.m_Name = value;
}
}
private Timer TimeoutTimer
{
[DebuggerNonUserCode]
get
{
return this._TimeoutTimer;
}
[MethodImpl(MethodImplOptions.Synchronized), DebuggerNonUserCode]
set
{
ElapsedEventHandler handler = new ElapsedEventHandler(this.TimeoutTimer_Elapsed);
if (this._TimeoutTimer != null)
{
this._TimeoutTimer.Elapsed -= handler;
}
this._TimeoutTimer = value;
if (this._TimeoutTimer != null)
{
this._TimeoutTimer.Elapsed += handler;
}
}
}
// Nested Types
public delegate void ErrorOutEventHandler(Module1.Client cli, Exception err);
public delegate void MessageEventHandler(Module1.Client cli, object obj, Type obj_Type);
}
public struct Message
{
private string _receiver;
private string _sender;
private string _message;
public string Sender { get; set; }
public string Receiver { get; set; }
public string Message { get; set; }
}