示例#1
0
        /// <summary>
        /// a bejövő adatok kezelése
        /// </summary>
        /// <param name="nm">ezt jött hálózaton</param>
        private void erkezettadatfeldolgozas(CNetworkMessage nm)
        {
            MemoryStream    ms = new MemoryStream(nm.objData);
            BinaryFormatter bf = new BinaryFormatter();
            object          o  = (object)bf.Deserialize(ms);

            ms.Close();

            Trace.WriteLine("incoming object: " + nm.objTipus);

            if (nm.objTipus == typeof(String))
            {
                Trace.WriteLine((string)o);

                //if ((string)o == "auth_failed")
                //{
                //    disconnect();
                //    return;
                //}
            }
            if (nm.objTipus == typeof(Ping))
            {
                sendObject(new Ping());
            }
            if (nm.objTipus == typeof(CHalozatiKliensID))
            {
                _kliensID.id = ((CHalozatiKliensID)o).id;
                //Log.logToFile("ID a szerveren: " + _kliensID.id, true);
                Trace.WriteLine(string.Format("ID a szerveren: {0}", _kliensID.id.wsname));
                //return;
            }

            //HalozatiEsemeny(this, "process");
        }
示例#2
0
        /// <summary>
        /// Hálózaton adatot küldünk
        /// </summary>
        /// <param name="o">ezt az adatot küldjük</param>
        public bool sendObject(object o)
        {
            //Trace.WriteLine("outging object: " + o.GetType().ToString());
            bool          ret     = false;
            NetworkStream nstream = null;

            try
            {
                nstream = _client.GetStream();
            }
            catch
            {
                //Trace.WriteLine("nincs network stream...");
                return(ret);
            }

            MemoryStream    ms = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();

            //MemoryStream előállítás
            try
            {
                bf.Serialize(ms, o);
            }
            catch (Exception e)
            {
                Trace.WriteLine("MemoryStream error");
                Debug.Assert(false, e.Message);
            }
            byte[] data = ms.GetBuffer();
            ms.Close();

            CNetworkMessage üzenet = new CNetworkMessage(o.GetType(), data);

            IFormatter formatter = new BinaryFormatter();

            try
            {
                formatter.Serialize(nstream, üzenet);
                ret = true;
            }
            catch (Exception ex)
            {
                Trace.WriteLine("NetworkMessage send failed");
            }
            return(ret);
        }
示例#3
0
        /// <summary>
        /// a hálózatról jövő adatok figyelése, szálban (végtelen ciklus)
        /// </summary>
        /// <param name="client">ezt a TcpClient-t figyeljük</param>
        private void handleComm(object client)
        {
            TcpClient tcpClient = (TcpClient)client;

            Thread.Sleep(200);
            NetworkStream clientStream;

            try
            {
                clientStream = tcpClient.GetStream();
            }
            catch (Exception e)
            {
                Trace.WriteLine(e);

                return;
            }

            while (true)
            {
                try
                {
                    IFormatter formatter = new BinaryFormatter();

                    _networkMessage = (CNetworkMessage)formatter.Deserialize(clientStream);

                    erkezettadatfeldolgozas(_networkMessage);
                }
                catch
                {
                    //Trace.WriteLine(e);
                    disconnect();
                    //Trace.WriteLine("Disconnected");
                    //_client.Close();
                    //_client = null;
                    break;
                }
            }
        }
示例#4
0
        /// <summary>
        /// feladunk hálózatra adatot
        /// </summary>
        /// <param name="o">az adat</param>
        public bool SendObject(object o)
        {
            bool ret = false;

            //Log.logToFile("outging object: " + o.GetType().ToString(),false);
            try
            {
                NetworkStream clientStream = _tcpClient.GetStream();
                if (clientStream == null)
                {
                    Trace.WriteLine("nincs írható network stream...");
                    return(ret);
                }

                MemoryStream    ms = new MemoryStream();
                BinaryFormatter bf = new BinaryFormatter();

                bf.Serialize(ms, o);
                byte[] data = ms.GetBuffer();
                ms.Close();

                CNetworkMessage uzenet    = new CNetworkMessage(o.GetType(), data);
                IFormatter      formatter = new BinaryFormatter();
                //feladjuk a Stream-re
                formatter.Serialize(clientStream, uzenet);
                ret = true;
                if (o.GetType() == typeof(string))
                {
                    Trace.WriteLine(string.Format("send to client {0} : {1}", _kliensID.gepNev, (string)o));
                }
            }
            catch (System.Exception ex)
            {
                Trace.WriteLine(string.Format("send object ({1}) to client failed: ({0}) !", _kliensID.gepNev, o.GetType()));
            }

            return(ret);
        }
示例#5
0
        /// <summary>
        /// a hálózatot itt kezeljuk, szálban indítva (vegtelen ciklus)
        /// </summary>
        private void HandleComm()
        {
            MemoryStream    ms;
            BinaryFormatter bf;
            NetworkStream   clientStream = _tcpClient.GetStream();

            //az első kontakt időtúllépése nem sok
            clientStream.ReadTimeout = 10000;
            IFormatter formatter = new BinaryFormatter();



            try
            {
                //megprobaljuk kiolvasni
                _networkMessage = (CNetworkMessage)formatter.Deserialize(clientStream);
            }
            catch (Exception e)
            {
                Trace.WriteLine(string.Format("Nem Common.NetworkMessage üzenet jött innen: {0}", _hostName));
                //Debug.Assert(false, e.Message);
                CloseConnection();
                return;
            }

            clientStream.ReadTimeout = -1;
            //Trace.WriteLine("incoming object: " + networkmessage.objtipus);

            ms = new MemoryStream(_networkMessage.objData);
            bf = new BinaryFormatter();

            //elso uzenet: KliensID object
            //ebbol elbiraljuk, van-e joga a kliensnek belepni

            try
            {
                _kliensID = (CHalozatiKliensID)bf.Deserialize(ms);
            }
            catch
            {
                Trace.WriteLine("Az első üzenet az új klienstől nem logon, hanem valami más...");
                ms.Close();
                CloseConnection();
                return;
            }

            ms.Close();

            //Thread aaa = new Thread(new ThreadStart(abc));
            //aaa.Name = "aaa";
            //aaa.IsBackground = true;
            //aaa.Start();

            if (!(_parentTCPszerver.logonelbiralas(this)))
            {
                Trace.WriteLine(string.Format("login failed... {0} {1}", _kliensID.gepNev, _kliensID.mac));
                this.SendObject("auth_failed");
                Thread.Sleep(200);
                return;
            }
            else
            {
                this.SendObject("auth_ok");
            }

            //remote IP
            _kliensID.ip = ((IPEndPoint)_tcpClient.Client.RemoteEndPoint).Address.ToString();
            //remote port
            _kliensID.port = ((IPEndPoint)_tcpClient.Client.RemoteEndPoint).Port.ToString();
            //if (_kliensID.location=="")
            //    _kliensID.location = Properties.Settings.Default.Location;
            //if (_kliensID.location == null)
            //    _kliensID.location = Properties.Settings.Default.Location;

            _parentTCPszerver.reg(this);


            //es az ID-t is elküldjük
            SendObject(_kliensID);

            Trace.WriteLine("logon rendben: " + _kliensID.gepNev);

            //figyeljük a socketet
            while (true)
            {
                try
                {
                    _networkMessage = (CNetworkMessage)formatter.Deserialize(clientStream);
                    _lastMsgTime    = DateTime.Now;
                    //ami jött, azt átadjuk feldolgozásra
                    //kliensKezeloHalozatiEvent(this,"process");
                    _parentTCPszerver.process(this);
                }
                catch (Exception ex)
                {
                    Trace.WriteLine("Disconnected: " + _kliensID.gepNev);
                    CloseConnection();
                    break;
                }
            }
        }