コード例 #1
0
        internal void UDPReciever()
        {
            try
            {
                timerforchecklogin          = new System.Timers.Timer();
                timerforchecklogin.Interval = 30000;
                timerforchecklogin.Start();
                timerforchecklogin.Elapsed += timerforchecklogin_Elapsed;
                Task.Factory.StartNew(() =>
                {
                    while (true)
                    {
                        //		string address = subscriber.Receive (Encoding.Unicode);
                        //	    byte[] buffer = new byte[512];
                        //	    int bufferSize = subscriber.Receive (buffer);

                        var buffer = subscriber.Receive();
                        if (buffer == null)
                        {
                            this.OnDataStatusChange.Raise(OnDataStatusChange, OnDataStatusChange.CreateReadOnlyArgs("STOP"));
                            continue;
                        }
                        FinalPrice _obj = (FinalPrice)DataPacket.RawDeserialize(buffer.Skip(4).Take(buffer.Length - 4).ToArray(), typeof(FinalPrice));

                        //			Console.WriteLine("Received");
                        //		if(_obj.Token==37454)
                        //			Console.Title="Token: "+_obj.Token+", Bid: "+_obj.MAXBID+", Ask: "+_obj.MINASK+" LTP: "+_obj.LTP;
                        //		else// if (_obj.Token==66039)
                        //		Console.WriteLine("Token {0} Bid {1} Ask {2} LTP {3}",_obj.Token,_obj.MAXBID,_obj.MINASK,_obj.LTP);

                        if (_obj.Token == 111)
                        {
                            Console.WriteLine(" SomeThing Wrong in DATA Server");
                            this.OnDataStatusChange.Raise(OnDataStatusChange, OnDataStatusChange.CreateReadOnlyArgs("START"));
                            //	OnDataError.Invoke();
                            continue;
                        }

                        if (_iSubscribe.Contains(_obj.Token))
                        {
                            this.OnDataStatusChange.Raise(OnDataStatusChange, OnDataStatusChange.CreateReadOnlyArgs("START"));
                            OnDataArrived.Raise(OnDataArrived, OnDataArrived.CreateReadOnlyArgs(_obj));
                        }
                    }
                });
            }
            catch (OperationCanceledException e)
            {
                Console.WriteLine("Cancellation invoked");
            }
            catch (AggregateException e)
            {
                Console.WriteLine("Some unexpected exception ");
            }
            catch (Exception Ex)
            {
                Console.WriteLine("Exception Raised " + Ex.StackTrace);
            }
        }
コード例 #2
0
ファイル: udp reciever.cs プロジェクト: rfkhan708/11jansan
        internal void UDPReciever(String LanIP = "127.0.0.1", string McastIp = "226.1.1.1" /* "239.70.70.21"*/, int port = 5050)   //10821)   //  internal void UDPReciever(String LanIP = "127.0.0.1", string McastIp = "233.1.2.5", int port = 34330)
        {
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            s.ExclusiveAddressUse = false;
            s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, Convert.ToInt32(port));

            s.Bind(ipep);
            IPAddress ip = IPAddress.Parse(McastIp);

            s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, IPAddress.Parse("127.0.0.1")));

            byte[] r_req = new byte[BufferSize];
            try
            {
                //Task.Run(() =>
                Task.Factory.StartNew(() =>
                {
                    while (!cts.IsCancellationRequested)
                    {
                        int size = s.Receive(r_req);
                        if (size > 0)
                        {
                            FinalPrice _obj = (FinalPrice)DataPacket.RawDeserialize(r_req, typeof(FinalPrice));
                            //Console.WriteLine("Token {0} Bid {1} Ask {2} LTP {3}",_obj.Token,_obj.MAXBID,_obj.MINASK,_obj.LTP);

                            if (_iSubscribe.Contains(_obj.Token))
                            {
                                OnDataArrived.Raise(OnDataArrived, OnDataArrived.CreateReadOnlyArgs(_obj));
                            }
                        }
                        cts.Token.ThrowIfCancellationRequested();
                    }
                }, cts.Token);
            }
            catch (OperationCanceledException)
            {
                Console.WriteLine("Cancellation invoked");
            }
            catch (AggregateException e)
            {
                if (e.InnerException is OperationCanceledException)
                {
                    if (s != null)
                    {
                        if (s.Connected)
                        {
                            s.Shutdown(SocketShutdown.Both);
                            s.Close();
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Some unexpected exception ");
                }
            }
            catch (Exception Ex)
            {
                Console.WriteLine("Exception Raised " + Ex.Message);
            }
        }