private void OnConnectCallback(IAsyncResult ar) { Socket sock = (Socket)ar.AsyncState; if (sock == null) return; IPEndPoint ep = null; try { sock.EndConnect(ar); ep = sock.RemoteEndPoint as IPEndPoint; _TCP = new ATCPConnector(sock, Uid64.CreateNewSync().Data, NetHelper.READ_BUFFER_BIG); _TCP.DisconnectEvent += OnDisconnectEventHandler; _TCP.ReceiveAction += OnReceveDataHandler; _TCP.ReceiveComAction += OnReceiveCommandHandler; } catch (SocketException ex){ sock.Close(); sock = null; NetLogger.Log(ex); return; } _isActive = true; if (ep != null) { NetLogger.Log("Connected to " + sock.RemoteEndPoint); } ConnectEvent?.Invoke(this, _TCP); }
private void OnClientConnectHandler(object sender, IConnector connector) { btnConnect.Invoke((MethodInvoker) delegate { btnConnect.BackgroundImage = Properties.Resources.Connect; }); btnOptions.Invoke((MethodInvoker) delegate { btnOptions.Enabled = false; }); ATCPConnector atcp = connector as ATCPConnector; if (atcp != null) { _traffic.Subscribe(atcp); atcp.SendPacketSize = _config.SendPacketSize; atcp.SendInterval = _config.SendPacketInterval; } SendComLogin(); }
private void OnDisconnectEventHandler(object sender, object disconnectStatus) { ATCPConnector connector = sender as ATCPConnector; if (connector != null) { _traffic.UnSubscribe(_connector); if (_ntf != null) { _ntf.Invoke((MethodInvoker) delegate { _ntf.Close(); }); } connector.DisconnectEvent -= OnDisconnectEventHandler; } }
private void OnDisconnectEventHandler(object sender, object disconnectState) { _TCP.DisconnectEvent -= OnDisconnectEventHandler; _TCP.ReceiveAction -= OnReceveDataHandler; _TCP.ReceiveComAction -= OnReceiveCommandHandler; _clientID = 0; _isActive = false; string s = "Disconnected "; if (disconnectState != null) { s += disconnectState.ToString(); } NetLogger.Log(s); DisconnectEvent?.Invoke(_TCP, disconnectState); _TCP = null; }
private void NCAutorizeNewClient(IConnector connector, ArgComAutorize arg) { if (arg == null || connector == null) { return; } ATCPConnector atcpcon = connector as ATCPConnector; if (atcpcon == null) { return; } if (!string.IsNullOrEmpty(arg.Login) && arg.Password == "Password") { _autorizator.RemoveConnector(connector.UniqueID); ArgUniqueID argClientID = new ArgUniqueID(NComSendClientID.uniqueID); argClientID.UniqueID = Uid64.CreateNewSync().Data; TeamPainterTCPClient client = new TeamPainterTCPClient(atcpcon, argClientID.UniqueID, arg.Login); bool added = _worker.AddClient(client); if (!added) { client.Disconnect(); return; } lvClients.Invoke((MethodInvoker) delegate { ListViewItem lvi = lvClients.Items[client.Connector.UniqueID.ToString()]; if (lvi != null) { lvi.Name = client.UniqueID.ToString(); lvi.SubItems[0].Text = client.UniqueID.ToString(); lvi.SubItems[1].Text = arg.Login; lvi.BackColor = Color.WhiteSmoke; lvi.ForeColor = Color.Black; lvClients.Invalidate(); } }); connector.SendCommand(argClientID); } }
public TeamPainterTCPClient(ATCPConnector connector, long clientID, string name) { if (connector == null) { throw new ArgumentNullException("Connector is null"); } if (clientID == 0) { throw new ArgumentException("Incorrent uniqueID [0]"); } _clientID = clientID; _name = name; _syncronized = false; _connector = connector; _connector.DisconnectEvent += OnDisconnectEventHandler; _traffic = new TrafficCounter(100, 30); _traffic.Subscribe(connector); }