public void Registry(MediaConnectionInstance m) { mLocker.Synchronized(() => { if (mServers.IndexOf(m) == -1) { mServers.Add(m); } }); }
private void HandleConnection(MediaConnectionInstance sck) { bool autoClose = AutoClose; sck.Disposed += sck_Disposed; AsyncTask task = new AsyncTask(() => { if (OnHandleConnection != null) { OnHandleConnectionEventArgs args = new OnHandleConnectionEventArgs(this, sck); OnHandleConnection(this, args); } }); if (autoClose) { task.AddAfterFinishJob(() => { try { //Console.WriteLine("Disconnect {0} (PID:{1}) (Second Connection?{2})", sck.IPAddress, sck.Attributes.GetAttributeInt("PID"), sck.Attributes.GetAttributeBool("IsSecondConnection")); if (sck.RawConnection != null) { sck.RawConnection.Close(); sck.RawConnection.Dispose(); } } catch (Exception ee) { Console.WriteLine(ee.ToString()); } mLocker.Synchronized(() => { mServers.Remove(sck); }); if (OnServerRemoved != null) { OnServerRemoved(this, new OnHandleConnectionEventArgs(this, sck)); } }); } task.Start(false); }
void sck_Disposed(object sender, EventArgs e) { try { if (sender != null) { MediaConnectionInstance sck = (MediaConnectionInstance)sender; mLocker.Synchronized(() => { mServers.Remove(sck); }); if (OnServerRemoved != null) { OnServerRemoved(this, new OnHandleConnectionEventArgs(this, sck)); } } } catch (Exception ee) { Console.WriteLine(ee.ToString()); } }
public void Start(int Port) { try { ServerSck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ServerSck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); IPEndPoint ip = new IPEndPoint(IPAddress.Parse("0.0.0.0"), Port); ServerSck.Bind(ip); ServerSck.Listen(256); bServerRun = true; while (bServerRun) { Socket sck = ServerSck.Accept(); if (sck != null) { MediaConnectionInstance server = new MediaConnectionInstance(sck); HandleConnection(server); } } } catch (ThreadAbortException ee) { } catch (Exception ee) { Console.WriteLine(ee.ToString()); } finally { if (ServerSck != null) { ServerSck.Close(); ServerSck.Dispose(); ServerSck = null; } } }
public OnHandleConnectionEventArgs(ServerHolder server, MediaConnectionInstance current) { this.Server = server; this.Current = current; }