Example #1
0
        public void StartSendToClient(string id, byte[] data)
        {
            while (!Generate.isO)
            {
                Thread.Sleep(50);
            }

            if (Generate.AgentList.ContainsKey(id))
            {
                KeyValuePair <string, Agents> a = Generate.AgentList.Single(t => t.Value.id == id);
                DataHandle Handle = new DataHandle();
                byte[]     _data  = Handle.HaSe(3001, Generate._AppID, data);
                a.Value._sock.BeginSendTo(_data, 0, _data.Length, SocketFlags.None, a.Value.cep, new AsyncCallback((async) => { }), a.Value._sock);
                return;
            }
            else
            {
                connection.Send(_udpb, _sepb, Generate._AppID + "," + id, 2001);

                Thread.Sleep(WaitingTimeForClientRequest);
                WaitingTimeForClientRequest += 500;
                if (WaitingTimeForClientRequest > 8000)
                {
                    WaitingTimeForClientRequest = 4000;
                }

                StartSendToClient(id, data);
            }
        }
Example #2
0
        private void p2000(Coming comi)
        {
            foreach (KeyValuePair <string, Coming> clie in Agent.clients)
            {
                ("L1006").p2pDEBUG(); //TODO : L1006
                if (clie.Key == comi.id)
                {
                    if (comi.port == clie.Value.port)
                    {
                        //NaT CONIC

                        ("L1007").p2pDEBUG(); //TODO : L1006

                        var c = from n in Agent.clients where n.Value.id == comi.id select n;
                        foreach (KeyValuePair <string, Coming> a in c)
                        {
                            a.Value.NaTType = 1;
                        }

                        DataHandle Handle = new DataHandle();
                        byte[]     data   = Handle.HaSe(1004, "NaT CONIC");
                        comi._sock.BeginSendTo(data, 0, data.Length, SocketFlags.None, comi.cep, new AsyncCallback((async) => { }), comi._sock);
                    }
                    else
                    {
                        //NaT SYMETRIC
                        ("L1008").p2pDEBUG(); //TODO : L1007
                        DataHandle Handle = new DataHandle();
                        byte[]     data   = Handle.HaSe(1005, "NaT SYMETRIC");
                        comi._sock.BeginSendTo(data, 0, data.Length, SocketFlags.None, comi.cep, new AsyncCallback((async) => { }), comi._sock);
                    }
                }
            }
        }
Example #3
0
        private void p2001(Coming comi)
        {
            string message = comi.message;

            string[] messages = message.Split(',');

            if (messages[1].Length > 10)
            {
                if (Agent.clients.ContainsKey(messages[1]))
                {
                    foreach (KeyValuePair <string, Coming> client in Agent.clients)
                    {
                        if (client.Value.id.Equals(messages[1], StringComparison.CurrentCultureIgnoreCase))
                        {
                            DataHandle Handle = new DataHandle();
                            byte[]     data   = Handle.HaSe(1006, client.Value.id + "," + client.Value.NaTType + "," + client.Value.cep + "," + client.Value.Address + "," + client.Value.port);
                            comi._sock.BeginSendTo(data, 0, data.Length, SocketFlags.None, comi.cep, new AsyncCallback((async) => { }), comi._sock);

                            byte[] data2 = Handle.HaSe(1008, comi.id + "," + comi.NaTType + "," + comi.cep + "," + comi.Address + "," + comi.port);
                            client.Value._sock.BeginSendTo(data2, 0, data2.Length, SocketFlags.None, client.Value.cep, new AsyncCallback((async) => { }), client.Value._sock);
                        }
                    }
                }
                else
                {
                    DataHandle Handle = new DataHandle();
                    byte[]     data   = Handle.HaSe(1007, "");
                    comi._sock.BeginSendTo(data, 0, data.Length, SocketFlags.None, comi.cep, new AsyncCallback((async) => { }), comi._sock);
                }
            }
            else
            {
                //WAITING FOR REQ.
            }
        }
 internal void FirstMessage(Socket udp,EndPoint sep)
 {
     DataHandle Handle = new DataHandle();
     string _mess = Generate._AppID + "," + Generate._licenceKey + ","  + Generate.GetMacAddress() + "," + udp.LocalEndPoint.ToString();
     byte[] data = Handle.HaSe(1000,_mess);
     udp.BeginSendTo(data, 0, data.Length, SocketFlags.None, sep, new AsyncCallback((async) => { }), udp);
 }
Example #5
0
        internal void Send(Socket udp, EndPoint sep, string message, int EventType)
        {
            DataHandle Handle = new DataHandle();

            byte[] data = Handle.HaSe(EventType, message);
            udp.BeginSendTo(data, 0, data.Length, SocketFlags.None, sep, new AsyncCallback((async) => { }), udp);
        }
Example #6
0
        void StartConnectionWithClient(Coming c)
        {
            Socket _udpc = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            _udpc.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            _udpc.Bind(_udpb.LocalEndPoint);

            EndPoint cep = ((EndPoint) new IPEndPoint(IPAddress.Parse(c.messages[3]), Convert.ToInt32(c.messages[4])));

            connection.Rect(_udpc);

            DataHandle Handle = new DataHandle();

            byte[] data = Handle.HaSe(3000, Generate._AppID);

            _udpc.BeginSendTo(data, 0, data.Length, SocketFlags.None, cep, new AsyncCallback((async) => { }), _udpc);
            Thread.Sleep(new Random().Next(100, 300));
            _udpc.BeginSendTo(data, 0, data.Length, SocketFlags.None, cep, new AsyncCallback((async) => { }), _udpc);

            if (!Generate.AgentList.ContainsKey(c.messages[0]))
            {
                try
                {
                    Generate.AgentList.Add(c.messages[0], new Agents
                    {
                        id    = c.messages[0],
                        cep   = cep,
                        _sock = c._sock
                    });
                }
                catch
                {
                }
            }
        }
Example #7
0
        internal void SecondMessage(Socket udp, EndPoint sep)
        {
            DataHandle Handle = new DataHandle();
            string     _mess  = Generate._AppID + "," + Generate._licenceKey + "," + Generate.GetMacAddress() + "," + udp.LocalEndPoint.ToString();

            byte[] data = Handle.HaSe(2000, _mess);
            udp.BeginSendTo(data, 0, data.Length, SocketFlags.None, sep, new AsyncCallback((async) => { }), udp);
        }
        internal void KeepAgentsAlive()
        {
            DataHandle Handle = new DataHandle();
            byte[] _data = Handle.HaSe(3002, Generate._AppID, Encoding.UTF8.GetBytes(""));

            timerc.Elapsed += (Sender, e) =>
            {
                foreach (KeyValuePair<string, Agents> agent in Generate.AgentList)
                {
                    agent.Value._sock.BeginSendTo(_data, 0, _data.Length, SocketFlags.None, agent.Value.cep, new AsyncCallback((async) => { }), agent.Value._sock);
                    Thread.Sleep(10);
                }
            };
        }
Example #9
0
        internal void KeepAgentsAlive()
        {
            DataHandle Handle = new DataHandle();

            byte[] _data = Handle.HaSe(3002, Generate._AppID, Encoding.UTF8.GetBytes(""));

            timerc.Elapsed += (Sender, e) =>
            {
                foreach (KeyValuePair <string, Agents> agent in Generate.AgentList)
                {
                    agent.Value._sock.BeginSendTo(_data, 0, _data.Length, SocketFlags.None, agent.Value.cep, new AsyncCallback((async) => { }), agent.Value._sock);
                    Thread.Sleep(10);
                }
            };
        }
 internal void Rect(Socket udp)
 {
     DataHandle Hand = new DataHandle();
     Thread thread = new Thread(new ThreadStart(() =>
     {
         byte[] data = new byte[20480];
         cepb = new IPEndPoint(IPAddress.Any, 0);
         int size = udp.ReceiveFrom(data, ref cepb);
         Thread threada = new Thread(new ThreadStart(() =>
         {
             Coming c = Hand.HaCo(data, size, udp, cepb);
             Protocol protocol = new Protocol(c);
         }));
         threada.Start();
         Rect(udp);
     }));
     thread.Start();
 }
Example #11
0
        private void Reca(Socket udpa)
        {
            DataHandle Hand   = new DataHandle();
            Thread     thread = new Thread(new ThreadStart(() =>
            {
                byte[] data = new byte[20480];
                cepa        = new IPEndPoint(IPAddress.Any, 0);
                int size    = udpa.ReceiveFrom(data, ref cepa);

                Thread threada = new Thread(new ThreadStart(() =>
                {
                    Hand.HaCo(data, size, udpa, cepa);
                }));
                threada.Start();
                Reca(udpa);
            }));

            thread.Start();
        }
Example #12
0
        internal void Rect(Socket udp)
        {
            DataHandle Hand   = new DataHandle();
            Thread     thread = new Thread(new ThreadStart(() =>
            {
                byte[] data    = new byte[20480];
                cepb           = new IPEndPoint(IPAddress.Any, 0);
                int size       = udp.ReceiveFrom(data, ref cepb);
                Thread threada = new Thread(new ThreadStart(() =>
                {
                    Coming c          = Hand.HaCo(data, size, udp, cepb);
                    Protocol protocol = new Protocol(c);
                }));
                threada.Start();
                Rect(udp);
            }));

            thread.Start();
        }
        private void Reca(Socket udpa)
        {
            DataHandle Hand = new DataHandle();
            Protocol prot = new Protocol();

            Thread thread = new Thread(new ThreadStart(() =>
            {
                byte[] data = new byte[20480];
                cepa = new IPEndPoint(IPAddress.Any, 0);
                int size = udpa.ReceiveFrom(data, ref cepa);
                ("L1002").p2pDEBUG(); //TODO : L1002
                Thread threada = new Thread(new ThreadStart(() =>
                    {
                        Coming comi = Hand.HaCo(data, size ,udpa ,cepa);
                        prot.ComingData(comi);
                    }));
                threada.Start();
                Reca(udpa);
            }));
            thread.Start();
        }
Example #14
0
        private void Recb(Socket udpb)
        {
            DataHandle Hand = new DataHandle();
            Protocol   prot = new Protocol();

            Thread thread = new Thread(new ThreadStart(() =>
            {
                byte[] data = new byte[20480];
                cepb        = new IPEndPoint(IPAddress.Any, 0);
                int size    = udpb.ReceiveFrom(data, ref cepb);
                ("L1004").p2pDEBUG(); //TODO : L1004
                Thread threada = new Thread(new ThreadStart(() =>
                {
                    Coming comi = Hand.HaCo(data, size, udpb, cepb);
                    prot.ComingData(comi);
                }));
                threada.Start();
                Recb(udpb);
            }));

            thread.Start();
        }
        private void p2001(Coming comi)
        {
            string message = comi.message;
            string[] messages = message.Split(',');

            if (messages[1].Length > 10)
            {
                if (Agent.clients.ContainsKey(messages[1]))
                {
                    foreach (KeyValuePair<string, Coming> client in Agent.clients)
                    {
                        if (client.Value.id.Equals(messages[1], StringComparison.CurrentCultureIgnoreCase))
                        {
                            DataHandle Handle = new DataHandle();
                            byte[] data = Handle.HaSe(1006, client.Value.id + "," + client.Value.NaTType + "," + client.Value.cep + "," + client.Value.Address + "," + client.Value.port);
                            comi._sock.BeginSendTo(data, 0, data.Length, SocketFlags.None, comi.cep, new AsyncCallback((async) => { }), comi._sock);

                            byte[] data2 = Handle.HaSe(1008, comi.id + "," + comi.NaTType + "," + comi.cep + "," + comi.Address + "," + comi.port);
                            client.Value._sock.BeginSendTo(data2, 0, data2.Length, SocketFlags.None, client.Value.cep, new AsyncCallback((async) => { }), client.Value._sock);
                        }
                    }
                }
                else
                {
                    DataHandle Handle = new DataHandle();
                    byte[] data = Handle.HaSe(1007, "");
                    comi._sock.BeginSendTo(data, 0, data.Length, SocketFlags.None, comi.cep, new AsyncCallback((async) => { }), comi._sock);
                }
            }
            else
            {
                //WAITING FOR REQ.
            }
        }
        private void p2000(Coming comi)
        {
            foreach (KeyValuePair<string, Coming> clie in Agent.clients)
            {
                ("L1006").p2pDEBUG(); //TODO : L1006
                if (clie.Key == comi.id)
                {
                    if (comi.port == clie.Value.port)
                    {
                        //NaT CONIC

                        ("L1007").p2pDEBUG(); //TODO : L1006

                        var c = from n in Agent.clients where n.Value.id == comi.id select n;
                        foreach (KeyValuePair<string, Coming> a in c) { a.Value.NaTType = 1; }

                        DataHandle Handle = new DataHandle();
                        byte[] data = Handle.HaSe(1004, "NaT CONIC");
                        comi._sock.BeginSendTo(data, 0, data.Length, SocketFlags.None, comi.cep, new AsyncCallback((async) => { }), comi._sock);
                    }
                    else
                    {
                        //NaT SYMETRIC
                        ("L1008").p2pDEBUG(); //TODO : L1007
                        DataHandle Handle = new DataHandle();
                        byte[] data = Handle.HaSe(1005, "NaT SYMETRIC");
                        comi._sock.BeginSendTo(data, 0, data.Length, SocketFlags.None, comi.cep, new AsyncCallback((async) => { }), comi._sock);
                    }
                }
            }
        }
Example #17
0
        private void p1000(Coming c)
        {
            Recording  recording = new Recording();
            clients    _clients  = recording.Record(ref c);
            DataHandle Handle    = new DataHandle();

            if (Agent.clients.ContainsKey(c.id))
            {
                #region XML && CLIENT UPLOADED

                XmlDocument xmld = new XmlDocument();
                try { xmld.Load(xmlPath); }
                catch { Thread.Sleep(50); xmld.Load(xmlPath); }
                foreach (XmlElement xmle in xmld.GetElementsByTagName("clients"))
                {
                    if (xmle["id"].InnerText.Equals(_clients.id, StringComparison.CurrentCultureIgnoreCase))
                    {
                        xmle["id"].InnerText         = _clients.id;
                        xmle["lep"].InnerText        = _clients.lep;
                        xmle["macAddress"].InnerText = _clients.macAddress;
                        xmle["licence"].InnerText    = _clients.licence;
                        xmle["Session"].InnerText    = (Convert.ToInt32(xmle["Session"].InnerText) + 1).ToString();
                        xmle["port"].InnerText       = _clients.port.ToString();
                        xmle["Address"].InnerText    = _clients.Address;
                        xmle["cep"].InnerText        = _clients.cep;
                    }
                }
                try { xmld.Save(xmlPath); }
                catch { Thread.Sleep(50); xmld.Save(xmlPath); }

                #endregion
                #region CLIENT CONNECTED AGAIN REPLACE PROPERTIES

                foreach (KeyValuePair <string, Coming> client in Agent.clients)
                {
                    if (client.Key.Equals(c.id, StringComparison.CurrentCultureIgnoreCase))
                    {
                        #region MAC ADDRESS DIFFERENT
                        if (!client.Value.macAddress.Equals("NOT"))
                        {
                            if (!client.Value.macAddress.Equals(c.macAddress))
                            {
                                byte[] data = Handle.HaSe(1001, "ID SAVED BEFORE FOR DIFFERENT CLIENT");
                                c._sock.BeginSendTo(data, 0, data.Length, SocketFlags.None, c.cep, new AsyncCallback((async) => { }), c._sock);
                                return;
                            }
                        }
                        #endregion
                        #region CLIENT UPDATE
                        client.Value.lep        = c.lep;
                        client.Value._sock      = c._sock;
                        client.Value.Address    = c.Address;
                        client.Value.cep        = c.cep;
                        client.Value.EventType  = c.EventType;
                        client.Value.licence    = c.licence;
                        client.Value.macAddress = c.macAddress;
                        client.Value.message    = c.message;
                        client.Value.port       = c.port;
                        #endregion
                        byte[] d = Handle.HaSe(1002, "CLIENT ALREADY IN THE LIST");
                        c._sock.BeginSendTo(d, 0, d.Length, SocketFlags.None, c.cep, new AsyncCallback((async) => { }), c._sock);
                    }
                }

                #endregion
            }
            else
            {
                Agent.clients.Add(c.id, c);
                if (!File.Exists(xmlPath))
                {
                    #region WRITE DOC
                    XmlTextWriter writer = new XmlTextWriter(xmlPath, Encoding.UTF8);
                    writer.Formatting = Formatting.Indented;
                    writer.WriteComment("Saved Clients list");
                    writer.WriteStartElement("Clients");
                    writer.WriteEndElement();
                    writer.Close();
                    #endregion
                }
                #region XML SAVE
                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.Load(xmlPath);
                }
                catch
                {
                    Thread.Sleep(50);
                    doc.Load(xmlPath);
                }

                XmlNode    client  = doc.DocumentElement;
                XmlElement element = doc.CreateElement("clients");

                XmlNode node1 = doc.CreateElement("id");
                node1.InnerText = _clients.id;
                element.AppendChild(node1);

                XmlNode node2 = doc.CreateElement("licence");
                node2.InnerText = _clients.licence;
                element.AppendChild(node2);

                XmlNode node3 = doc.CreateElement("macAddress");
                node3.InnerText = _clients.macAddress;
                element.AppendChild(node3);

                XmlNode node4 = doc.CreateElement("Address");
                node4.InnerText = _clients.Address;
                element.AppendChild(node4);

                XmlNode node5 = doc.CreateElement("port");
                node5.InnerText = _clients.port.ToString();
                element.AppendChild(node5);

                XmlNode node6 = doc.CreateElement("cep");
                node6.InnerText = _clients.cep;
                element.AppendChild(node6);

                XmlNode node7 = doc.CreateElement("lep");
                node7.InnerText = _clients.lep;
                element.AppendChild(node7);

                XmlNode node8 = doc.CreateElement("Session");
                node8.InnerText = "1";
                element.AppendChild(node8);

                XmlNode node9 = doc.CreateElement("message");
                node9.InnerText = _clients.message;
                element.AppendChild(node9);

                client.AppendChild(element);
                try
                {
                    doc.Save(xmlPath);
                }
                catch
                {
                    Thread.Sleep(50);
                    doc.Save(xmlPath);
                }
                #endregion
                byte[] data = Handle.HaSe(1003, "CLIENT RECORDED SUCCESFULY");
                c._sock.BeginSendTo(data, 0, data.Length, SocketFlags.None, c.cep, new AsyncCallback((async) => { }), c._sock);
            }
        }
        private void Reca(Socket udpa)
        {
            DataHandle Hand = new DataHandle();
            Thread thread = new Thread(new ThreadStart(() =>
            {
                byte[] data = new byte[20480];
                cepa = new IPEndPoint(IPAddress.Any, 0);
                int size = udpa.ReceiveFrom(data, ref cepa);

                Thread threada = new Thread(new ThreadStart(() =>
                {
                    Hand.HaCo(data, size, udpa,cepa);
                }));
                threada.Start();
                Reca(udpa);
            }));
            thread.Start();
        }
        private void p1000(Coming c)
        {
            Recording recording = new Recording();
            clients _clients = recording.Record(ref c);
            DataHandle Handle = new DataHandle();

            if (Agent.clients.ContainsKey(c.id))
            {
                #region XML && CLIENT UPLOADED

                XmlDocument xmld = new XmlDocument();
                try { xmld.Load(xmlPath); }
                catch { Thread.Sleep(50); xmld.Load(xmlPath); }
                foreach (XmlElement xmle in xmld.GetElementsByTagName("clients"))
                {
                    if (xmle["id"].InnerText.Equals(_clients.id, StringComparison.CurrentCultureIgnoreCase))
                    {
                        xmle["id"].InnerText = _clients.id;
                        xmle["lep"].InnerText = _clients.lep;
                        xmle["macAddress"].InnerText = _clients.macAddress;
                        xmle["licence"].InnerText = _clients.licence;
                        xmle["Session"].InnerText = (Convert.ToInt32(xmle["Session"].InnerText) + 1).ToString();
                        xmle["port"].InnerText = _clients.port.ToString();
                        xmle["Address"].InnerText = _clients.Address;
                        xmle["cep"].InnerText = _clients.cep;
                    }
                }
                try { xmld.Save(xmlPath); }
                catch { Thread.Sleep(50); xmld.Save(xmlPath); }

                #endregion
                #region CLIENT CONNECTED AGAIN REPLACE PROPERTIES

                foreach (KeyValuePair<string, Coming> client in Agent.clients)
                {
                    if (client.Key.Equals(c.id, StringComparison.CurrentCultureIgnoreCase))
                    {

                        #region MAC ADDRESS DIFFERENT
                        if (!client.Value.macAddress.Equals("NOT"))
                        {
                            if (!client.Value.macAddress.Equals(c.macAddress))
                            {
                                byte[] data = Handle.HaSe(1001, "ID SAVED BEFORE FOR DIFFERENT CLIENT");
                                c._sock.BeginSendTo(data, 0, data.Length, SocketFlags.None, c.cep, new AsyncCallback((async) => { }), c._sock);
                                return;
                            }
                        }
                        #endregion
                        #region CLIENT UPDATE
                        client.Value.lep = c.lep;
                        client.Value._sock = c._sock;
                        client.Value.Address = c.Address;
                        client.Value.cep = c.cep;
                        client.Value.EventType = c.EventType;
                        client.Value.licence = c.licence;
                        client.Value.macAddress = c.macAddress;
                        client.Value.message = c.message;
                        client.Value.port = c.port;
                        #endregion
                        byte[] d = Handle.HaSe(1002, "CLIENT ALREADY IN THE LIST");
                        c._sock.BeginSendTo(d, 0, d.Length, SocketFlags.None, c.cep, new AsyncCallback((async) => { }), c._sock);
                    }
                }

                #endregion
            }
            else
            {
                Agent.clients.Add(c.id, c);
                if (!File.Exists(xmlPath))
                {
                    #region WRITE DOC
                    XmlTextWriter writer = new XmlTextWriter(xmlPath, Encoding.UTF8);
                    writer.Formatting = Formatting.Indented;
                    writer.WriteComment("Saved Clients list");
                    writer.WriteStartElement("Clients");
                    writer.WriteEndElement();
                    writer.Close();
                    #endregion
                }
                #region XML SAVE
                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.Load(xmlPath);
                }
                catch
                {
                    Thread.Sleep(50);
                    doc.Load(xmlPath);
                }

                XmlNode client = doc.DocumentElement;
                XmlElement element = doc.CreateElement("clients");

                XmlNode node1 = doc.CreateElement("id");
                node1.InnerText = _clients.id;
                element.AppendChild(node1);

                XmlNode node2 = doc.CreateElement("licence");
                node2.InnerText = _clients.licence;
                element.AppendChild(node2);

                XmlNode node3 = doc.CreateElement("macAddress");
                node3.InnerText = _clients.macAddress;
                element.AppendChild(node3);

                XmlNode node4 = doc.CreateElement("Address");
                node4.InnerText = _clients.Address;
                element.AppendChild(node4);

                XmlNode node5 = doc.CreateElement("port");
                node5.InnerText = _clients.port.ToString();
                element.AppendChild(node5);

                XmlNode node6 = doc.CreateElement("cep");
                node6.InnerText = _clients.cep;
                element.AppendChild(node6);

                XmlNode node7 = doc.CreateElement("lep");
                node7.InnerText = _clients.lep;
                element.AppendChild(node7);

                XmlNode node8 = doc.CreateElement("Session");
                node8.InnerText = "1";
                element.AppendChild(node8);

                XmlNode node9 = doc.CreateElement("message");
                node9.InnerText = _clients.message;
                element.AppendChild(node9);

                client.AppendChild(element);
                try
                {
                    doc.Save(xmlPath);
                }
                catch
                {
                    Thread.Sleep(50);
                    doc.Save(xmlPath);
                }
                #endregion
                byte[] data = Handle.HaSe(1003, "CLIENT RECORDED SUCCESFULY");
                c._sock.BeginSendTo(data, 0, data.Length, SocketFlags.None, c.cep, new AsyncCallback((async) => { }), c._sock);
            }
        }
 internal void Send(Socket udp,EndPoint sep, string message,int EventType)
 {
     DataHandle Handle = new DataHandle();
     byte[] data = Handle.HaSe(EventType, message);
     udp.BeginSendTo(data, 0, data.Length, SocketFlags.None, sep, new AsyncCallback((async) => { }), udp);
 }