Example #1
0
        /// <summary>
        /// 发送线程
        /// </summary>
        private void JustServiceSendThread()
        {
            JustArgs    args    = null;
            JustAdapter adapter = this.mAdapter;

            while (true)
            {
                Queue <JustArgs> argsQueue = this.argsQueue;

                args = null;
                if (argsQueue.Count > 0)
                {
                    args = argsQueue.Dequeue();
                }

                if (args == null)
                {
                    mSendEvent.WaitOne();
                    continue;
                    //args = argsQueue.Dequeue();
                }

                adapter.mJustClientInterface.LowLevelSend(mAdapter, args);

                JustEventInterface JustEvent = adapter.mJustEventInterface;
                if (JustEvent != null)
                {
                    JustEvent.OnSendEvent(adapter, args);
                }
            }
        }
        /// <summary>
        /// 发送
        /// </summary>
        /// <param name="args"></param>
        public void LowLevelSend(JustAdapter adapter, JustArgs args)
        {
            //Console.WriteLine("发送一个数据包");
            //Thread.Sleep(1000);

            List <UdpClient> sockets = adapter.TAG  as List <UdpClient>;

            byte[] buf = args.buffer;

            adapter.LastEventType = JustEventType.Successful;
            foreach (UdpClient udpSocket in sockets)
            {
                try
                {
                    udpSocket.Send(buf, buf.Length, adapter.RemoteAddress, adapter.RemotePort);
                }
                catch (TimeoutException e)
                {
                    Console.WriteLine(e);
                    adapter.LastEventType = JustEventType.Timeout;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    adapter.LastEventType = JustEventType.Unknow;
                }
            }
        }
        /// <summary>
        /// 接收
        /// </summary>
        /// <returns></returns>
        public byte[] LowLevelRecv(JustAdapter adapter)
        {
            //Console.WriteLine("接收到一个数据包");

            List<UdpClient> sockets = adapter.TAG as List<UdpClient>;

            byte[] buffer = null;

            JustUdpBroadcastMessage msg = null;

            adapter.LastEventType = JustEventType.Successful;

            if (messages.Count > 0)
            {
                msg = this.messages.Dequeue();
                this.recvEvent.Reset();
            }
            else
            {
                this.recvEvent.WaitOne();
                msg = this.messages.Dequeue();
            }

            if(msg != null)
            {
                adapter.LastEventType = msg.Type;
                buffer = msg.buffer;
            }

            return buffer;
        }
        /// <summary>
        /// 接收
        /// </summary>
        /// <returns></returns>
        public byte[] LowLevelRecv(JustAdapter adapter)
        {
            //Console.WriteLine("接收到一个数据包");

            List <UdpClient> sockets = adapter.TAG as List <UdpClient>;

            byte[] buffer = null;

            JustUdpBroadcastMessage msg = null;

            adapter.LastEventType = JustEventType.Successful;

            if (messages.Count > 0)
            {
                msg = this.messages.Dequeue();
                this.recvEvent.Reset();
            }
            else
            {
                this.recvEvent.WaitOne();
                msg = this.messages.Dequeue();
            }

            if (msg != null)
            {
                adapter.LastEventType = msg.Type;
                buffer = msg.buffer;
            }

            return(buffer);
        }
        /// <summary>
        /// 接收
        /// </summary>
        /// <returns></returns>
        public byte[] LowLevelRecv(JustAdapter adapter)
        {
            Console.WriteLine("接收到一个数据包");
            SerialPort port = adapter.TAG as SerialPort;

            return null;
        }
        /// <summary>
        /// 接收
        /// </summary>
        /// <returns></returns>
        public byte[] LowLevelRecv(JustAdapter adapter)
        {
            Console.WriteLine("接收到一个数据包");

            byte[] buffer = null;
            IPEndPoint endport = new IPEndPoint(IPAddress.Any, 0);
            UdpClient udpSocket = adapter.TAG as UdpClient;

            adapter.LastEventType = JustEventType.Successful;
            try
            {
                buffer = udpSocket.Receive(ref endport);
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e);
                adapter.LastEventType = JustEventType.Timeout;
            }
            catch(Exception e)
            {
                Console.WriteLine(e);
                adapter.LastEventType = JustEventType.Unknow;
            }
            return buffer;
        }
Example #7
0
        /// <summary>
        /// 接收
        /// </summary>
        /// <returns></returns>
        public byte[] LowLevelRecv(JustAdapter adapter)
        {
            Console.WriteLine("接收到一个数据包");

            byte[]     buffer    = null;
            IPEndPoint endport   = new IPEndPoint(IPAddress.Any, 0);
            UdpClient  udpSocket = adapter.TAG as UdpClient;

            adapter.LastEventType = JustEventType.Successful;
            try
            {
                buffer = udpSocket.Receive(ref endport);
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e);
                adapter.LastEventType = JustEventType.Timeout;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                adapter.LastEventType = JustEventType.Unknow;
            }
            return(buffer);
        }
        /// <summary>
        /// 发送
        /// </summary>
        /// <param name="args"></param>
        public void LowLevelSend(JustAdapter adapter, JustArgs args)
        {
            Console.WriteLine("发送一个数据包");
            //Thread.Sleep(1000);

            UdpClient udpSocket = adapter.TAG as UdpClient;

            byte[] buf = args.buffer;

            adapter.LastEventType = JustEventType.Successful;
            try
            {
                udpSocket.Send(buf, buf.Length, adapter.RemoteAddress, adapter.RemotePort);
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e);
                adapter.LastEventType = JustEventType.Timeout;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                adapter.LastEventType = JustEventType.Unknow;
            }
        }
        /// <summary>
        /// 接收
        /// </summary>
        /// <returns></returns>
        public byte[] LowLevelRecv(JustAdapter adapter)
        {
            Console.WriteLine("接收到一个数据包");
            SerialPort port = adapter.TAG as SerialPort;


            return(null);
        }
        public void StopClient(JustAdapter adapter)
        {
            //argsQueue.Clear();
            Console.WriteLine("StopClient");

            SerialPort port = adapter.TAG as SerialPort;
            port.Close();
        }
Example #11
0
        /// <summary>
        /// 关闭
        /// </summary>
        public void StopClient(JustAdapter adapter)
        {
            //argsQueue.Clear();
            Console.WriteLine("StopClient");

            UdpClient udpSocket = adapter.TAG as UdpClient;

            udpSocket.Close();
        }
        public void StopClient(JustAdapter adapter)
        {
            //argsQueue.Clear();
            Console.WriteLine("StopClient");

            SerialPort port = adapter.TAG as SerialPort;

            port.Close();
        }
Example #13
0
        /// <summary>
        /// 当服务开启的时候执行
        /// </summary>
        public void OnServiceStart()
        {
            JustAdapter adapter = this.mAdapter;

            if (adapter != null)
            {
                adapter.mJustClientInterface.OpenClient(adapter);
                adapter.mJustEventInterface.OnOpenEvent(adapter);
                mSendEvent.Reset();
            }
        }
 /// <summary>
 /// 接收
 /// </summary>
 /// <returns></returns>
 public byte[] LowLevelRecv(JustAdapter adapter)
 {
     Console.WriteLine("接收到一个数据包");
     while (true)
     {
         Thread.Sleep(1000);
         if(isBreak)
         {
             break;
         }
     }
     return null;
 }
Example #15
0
        /// <summary>
        /// 当服务关闭的时候执行
        /// </summary>
        public void OnServiceStop()
        {
            JustAdapter adapter = this.mAdapter;

            if (adapter != null)
            {
                adapter.mJustClientInterface.StopClient(adapter);
                //关闭线程
                StopServiceThread();

                adapter = null;
            }
        }
        /// <summary>
        /// 关闭
        /// </summary>
        public void StopClient(JustAdapter adapter)
        {
            //argsQueue.Clear();
            Console.WriteLine("StopClient");

            List <UdpClient> sockets = adapter.TAG as List <UdpClient>;

            isStartRecv = false;
            foreach (UdpClient udpSocket in sockets)
            {
                udpSocket.Close();
            }
        }
 /// <summary>
 /// 接收
 /// </summary>
 /// <returns></returns>
 public byte[] LowLevelRecv(JustAdapter adapter)
 {
     Console.WriteLine("接收到一个数据包");
     while (true)
     {
         Thread.Sleep(1000);
         if (isBreak)
         {
             break;
         }
     }
     return(null);
 }
        /// <summary>
        /// 初始化,并打开客户端
        /// </summary>
        public void OpenClient(JustAdapter adapter)
        {
            //throw new NotImplementedException();
            Console.WriteLine("InitClient");

            IPEndPoint endport = new IPEndPoint(IPAddress.Any, 0);
            UdpClient udpSocket = new UdpClient(endport);

            //设置超时时间
            udpSocket.Client.ReceiveTimeout = adapter.ReceiveTimeout;
            udpSocket.Client.SendTimeout = adapter.SendTimeout;

            adapter.TAG = udpSocket;
        }
Example #19
0
        /// <summary>
        /// 发送Just数据包
        /// </summary>
        /// <param name="args"></param>
        public void SendJust(JustArgs args)
        {
            JustAdapter adapter = this.mAdapter;

            if (adapter == null)
            {
                throw new JustException("");
            }

            argsQueue.Enqueue(args);
            StartServiceThread();

            this.mSendEvent.Set();
        }
Example #20
0
        /// <summary>
        /// 初始化,并打开客户端
        /// </summary>
        public void OpenClient(JustAdapter adapter)
        {
            //throw new NotImplementedException();
            Console.WriteLine("InitClient");

            IPEndPoint endport   = new IPEndPoint(IPAddress.Any, 0);
            UdpClient  udpSocket = new UdpClient(endport);

            //设置超时时间
            udpSocket.Client.ReceiveTimeout = adapter.ReceiveTimeout;
            udpSocket.Client.SendTimeout    = adapter.SendTimeout;

            adapter.TAG = udpSocket;
        }
Example #21
0
        /// <summary>
        /// 接收线程
        /// </summary>
        private void JustServiceRecvThread()
        {
            byte[]      buffer  = null;
            JustAdapter adapter = this.mAdapter;

            while (true)
            {
                buffer = adapter.mJustClientInterface.LowLevelRecv(mAdapter);

                JustEventInterface JustEvent = adapter.mJustEventInterface;
                if (JustEvent != null)
                {
                    JustEvent.OnRecvEvent(adapter, buffer);

                    //TODO:根据返回值,决定是否需要继续接收
                }
            }
        }
        /// <summary>
        /// 发送
        /// </summary>
        /// <param name="args"></param>
        public void LowLevelSend(JustAdapter adapter, JustArgs args)
        {
            Console.WriteLine("发送一个数据包");

            TcpClient tcpSocket = new TcpClient();

            byte[] buffer = null;

            adapter.LastEventType = JustEventType.Successful;
            try
            {
                buffer = args.buffer;

                tcpSocket.SendTimeout    = adapter.SendTimeout;
                tcpSocket.ReceiveTimeout = adapter.ReceiveTimeout;
                tcpSocket.Connect(adapter.RemoteAddress, adapter.RemotePort);
                tcpSocket.GetStream().Write(buffer, 0, buffer.Length);
                tcpSocket.GetStream().Flush();
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e);
                adapter.LastEventType = JustEventType.Timeout;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                adapter.LastEventType = JustEventType.Unknow;
            }
            finally
            {
                try
                {
                    tcpSocket.Close();
                    Console.WriteLine("关闭连接");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }
        /// <summary>
        /// 发送
        /// </summary>
        /// <param name="args"></param>
        public void LowLevelSend(JustAdapter adapter, JustArgs args)
        {
            Console.WriteLine("发送一个数据包");

            TcpClient tcpSocket = new TcpClient();
            byte[] buffer = null;

            adapter.LastEventType = JustEventType.Successful;
            try
            {
                buffer = args.buffer;

                tcpSocket.SendTimeout = adapter.SendTimeout;
                tcpSocket.ReceiveTimeout = adapter.ReceiveTimeout;
                tcpSocket.Connect(adapter.RemoteAddress, adapter.RemotePort);
                tcpSocket.GetStream().Write(buffer, 0, buffer.Length);
                tcpSocket.GetStream().Flush();
            }
            catch(TimeoutException e)
            {
                Console.WriteLine(e);
                adapter.LastEventType = JustEventType.Timeout;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                adapter.LastEventType = JustEventType.Unknow;
            }
            finally
            {
                try
                {
                    tcpSocket.Close();
                    Console.WriteLine("关闭连接");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }
        /// <summary>
        /// 初始化,并打开客户端
        /// </summary>
        public void OpenClient(JustAdapter adapter)
        {
            //throw new NotImplementedException();
            Console.WriteLine("InitClient");
            //SerialPort port = new SerialPort();

            SerialPort port = null;
            adapter.LastEventType = JustEventType.Successful;
            try
            {
                port = new SerialPort(adapter.RemoteAddress, COM_BAUDRATE, Parity.None, 8);
                port.Open();
            }
            catch (Exception e)
            {
                adapter.LastEventType = JustEventType.Notfound;
                Console.WriteLine(e);
            }

            adapter.TAG = port;
        }
        /// <summary>
        /// 初始化,并打开客户端
        /// </summary>
        public void OpenClient(JustAdapter adapter)
        {
            List <UdpClient> sockets = new List <UdpClient>();

            //throw new NotImplementedException();
            Console.WriteLine("InitClient");

            NetworkInterface[] networks = NetworkInterface.GetAllNetworkInterfaces();

            recvEvent.Reset();
            foreach (NetworkInterface net in networks)
            {
                IPInterfaceProperties IPInterfaceProperties = net.GetIPProperties();
                UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses;
                foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)
                {
                    if (UnicastIPAddressInformation.Address.AddressFamily.ToString() == ProtocolFamily.InterNetwork.ToString())
                    {
                        Console.WriteLine("ipaddr = " + UnicastIPAddressInformation.Address.ToString());

                        IPEndPoint endport   = new IPEndPoint(UnicastIPAddressInformation.Address, 0);
                        UdpClient  udpSocket = new UdpClient(endport);

                        //设置超时时间
                        udpSocket.Client.ReceiveTimeout = adapter.ReceiveTimeout;
                        udpSocket.Client.SendTimeout    = adapter.SendTimeout;

                        sockets.Add(udpSocket);

                        //开启接收
                        udpSocket.BeginReceive(ReceiveCallback, udpSocket);
                    }
                }
            }

            adapter.TAG = sockets;
        }
        /// <summary>
        /// 初始化,并打开客户端
        /// </summary>
        public void OpenClient(JustAdapter adapter)
        {
            //throw new NotImplementedException();
            Console.WriteLine("InitClient");
            //SerialPort port = new SerialPort();



            SerialPort port = null;

            adapter.LastEventType = JustEventType.Successful;
            try
            {
                port = new SerialPort(adapter.RemoteAddress, COM_BAUDRATE, Parity.None, 8);
                port.Open();
            }
            catch (Exception e)
            {
                adapter.LastEventType = JustEventType.Notfound;
                Console.WriteLine(e);
            }

            adapter.TAG = port;
        }
 /// <summary>
 /// 发送
 /// </summary>
 /// <param name="args"></param>
 public void LowLevelSend(JustAdapter adapter, JustArgs args)
 {
     Console.WriteLine("发送一个数据包");
     SerialPort port = adapter.TAG as SerialPort;
 }
 /// <summary>
 /// 初始化,并打开客户端
 /// </summary>
 public void OpenClient(JustAdapter adapter)
 {
     //throw new NotImplementedException();
     Console.WriteLine("InitClient");
 }
 public void StopClient(JustAdapter adapter)
 {
     //argsQueue.Clear();
     Console.WriteLine("StopClient");
 }
        /// <summary>
        /// 初始化,并打开客户端
        /// </summary>
        public void OpenClient(JustAdapter adapter)
        {
            List<UdpClient> sockets = new List<UdpClient>();
            //throw new NotImplementedException();
            Console.WriteLine("InitClient");

            NetworkInterface[] networks = NetworkInterface.GetAllNetworkInterfaces();

            recvEvent.Reset();
            foreach (NetworkInterface net in networks)
            {
                IPInterfaceProperties IPInterfaceProperties = net.GetIPProperties();
                UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses;
                foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)
                {
                    if (UnicastIPAddressInformation.Address.AddressFamily.ToString() == ProtocolFamily.InterNetwork.ToString())
                    {
                        Console.WriteLine("ipaddr = " + UnicastIPAddressInformation.Address.ToString());

                        IPEndPoint endport = new IPEndPoint(UnicastIPAddressInformation.Address, 0);
                        UdpClient udpSocket = new UdpClient(endport);

                        //设置超时时间
                        udpSocket.Client.ReceiveTimeout = adapter.ReceiveTimeout;
                        udpSocket.Client.SendTimeout = adapter.SendTimeout;

                        sockets.Add(udpSocket);

                        //开启接收
                        udpSocket.BeginReceive(ReceiveCallback, udpSocket);
                    }
                }
            }

            adapter.TAG = sockets;
        }
        /// <summary>
        /// 关闭
        /// </summary>
        public void StopClient(JustAdapter adapter)
        {
            //argsQueue.Clear();
            Console.WriteLine("StopClient");

            List<UdpClient> sockets = adapter.TAG as List<UdpClient>;
            isStartRecv = false;
            foreach (UdpClient udpSocket in sockets)
            {
                udpSocket.Close();
            }
        }
 public void StopClient(JustAdapter adapter)
 {
     //argsQueue.Clear();
     Console.WriteLine("StopClient");
 }
Example #33
0
        /// <summary>
        /// 设置Adapter
        /// </summary>
        /// <param name="adapter">适配器</param>
        public void SetAdapter(JustAdapter adapter)
        {
            this.mAdapter = adapter;

            OnServiceStart();
        }
 /// <summary>
 /// 初始化,并打开客户端
 /// </summary>
 public void OpenClient(JustAdapter adapter)
 {
     //throw new NotImplementedException();
     Console.WriteLine("InitClient");
 }
 /// <summary>
 /// 发送
 /// </summary>
 /// <param name="args"></param>
 public void LowLevelSend(JustAdapter adapter, JustArgs args)
 {
     Console.WriteLine("发送一个数据包");
     SerialPort port = adapter.TAG as SerialPort;
 }
        /// <summary>
        /// 关闭
        /// </summary>
        public void StopClient(JustAdapter adapter)
        {
            //argsQueue.Clear();
            Console.WriteLine("StopClient");

            UdpClient udpSocket = adapter.TAG as UdpClient;
            udpSocket.Close();
        }
Example #37
0
        /// <summary>
        /// 设置Adapter
        /// </summary>
        /// <param name="adapter">适配器</param>
        public void SetAdapter(JustAdapter adapter)
        {
            this.mAdapter = adapter;

            OnServiceStart();
        }