SafeSleep() 개인적인 정적인 메소드

private static SafeSleep ( int ms ) : void
ms int
리턴 void
예제 #1
0
            public void Run()
            {
                while (!exit)
                {
                    // reconnect
                    try
                    {
                        sock = new System.Net.Sockets.TcpClient(provider.inetAddr.ToString(), provider.inetPort);
                        Stream       _outs  = sock.GetStream();
                        BinaryWriter _douts = new BinaryWriter(_outs);
                        _douts.Write(TCPProvider.MAGIC_CLIENT);
                        _douts.Write(TCPProvider.VERSION);
                        _douts.Flush();
                        outs = _outs;
                        ins  = new BinaryReader(new BufferedStream(sock.GetStream()));

                        int magic = ins.ReadInt32();
                        if (magic != TCPProvider.MAGIC_SERVER)
                        {
                            sock.Close();
                            continue;
                        }

                        serverVersion = ins.ReadInt32();
                    }
                    catch (IOException)
                    {
                        Console.Error.WriteLine("LCM.TCPProvider: Unable to connect to " + provider.inetAddr + ":" + provider.inetPort);
                        TCPProvider.SafeSleep(500);

                        // try connecting again.
                        continue;
                    }

                    // read loop
                    try
                    {
                        while (!exit)
                        {
                            int type = ins.ReadInt32();

                            int    channelLen = ins.ReadInt32();
                            byte[] channel    = new byte[channelLen];
                            ReadInput(ins.BaseStream, channel, 0, channel.Length);

                            int    dataLen = ins.ReadInt32();
                            byte[] data    = new byte[dataLen];
                            ReadInput(ins.BaseStream, data, 0, data.Length);

                            provider.lcm.ReceiveMessage(System.Text.Encoding.GetEncoding("US-ASCII").GetString(channel), data, 0, data.Length);
                        }
                    }
                    catch (IOException)
                    {
                        // exit read loop so we'll create a new connection.
                    }
                }
            }
예제 #2
0
파일: TCPProvider.cs 프로젝트: obiben/lcm
            public void Run()
            {
                while (!exit)
                {
                    // reconnect
                    try
                    {
                        sock = new System.Net.Sockets.TcpClient(provider.inetAddr.ToString(), provider.inetPort);
                        Stream       _outs  = sock.GetStream();
                        BinaryWriter _douts = new BinaryWriter(_outs);
                        _douts.Write(TCPProvider.MAGIC_CLIENT);
                        _douts.Write(TCPProvider.VERSION);
                        _douts.Flush();
                        outs = _outs;
                        ins  = new BinaryReader(new BufferedStream(sock.GetStream()));

                        int magic = ins.ReadInt32();
                        if (magic != TCPProvider.MAGIC_SERVER)
                        {
                            sock.Close();
                            continue;
                        }

                        serverVersion = ins.ReadInt32();

                        Connected?.Invoke(this, EventArgs.Empty);
                    }
                    catch (IOException)
                    {
                        Disconnected?.Invoke(this, EventArgs.Empty);
                        Console.Error.WriteLine("LCM.TCPProvider: Unable to connect to " + provider.inetAddr + ":" + provider.inetPort);
                        TCPProvider.SafeSleep(500);

                        // try connecting again.
                        continue;
                    }
                    catch (SocketException)
                    {
                        Disconnected?.Invoke(this, EventArgs.Empty);
                        Console.Error.WriteLine("LCM.TCPProvider: Unable to connect to " + provider.inetAddr + ":" + provider.inetPort);
                        TCPProvider.SafeSleep(500);

                        // try connecting again.
                        continue;
                    }


                    // read loop
                    try
                    {
                        while (!exit)
                        {
                            int type = ins.ReadInt32();

                            if (type == MESSAGE_TYPE_PUBLISH || type == MESSAGE_TYPE_SUBSCRIBE || type == MESSAGE_TYPE_UNSUBSCRIBE)
                            {
                                int    channelLen = ins.ReadInt32();
                                byte[] channel    = new byte[channelLen];
                                ReadInput(ins.BaseStream, channel, 0, channel.Length);

                                int    dataLen = ins.ReadInt32();
                                byte[] data    = new byte[dataLen];
                                ReadInput(ins.BaseStream, data, 0, data.Length);

                                provider.lcm.ReceiveMessage(System.Text.Encoding.GetEncoding("US-ASCII").GetString(channel), data, 0, data.Length);
                            }
                            else
                            {
                                Console.Error.WriteLine("LCM.TCPProvider: Bad message type ({0}), throwing message away and reconnecting", type);
                                break;
                            }
                        }
                    }
                    catch (IOException)
                    {
                        // exit read loop so we'll create a new connection.
                    }
                    catch (NotSupportedException)
                    {
                        // exit read loop so we'll create a new connection.
                    }
                    catch (ObjectDisposedException)
                    {
                        // exit read loop so we'll create a new connection.
                    }
                    catch (OverflowException)
                    {
                        // exit read loop so we'll create a new connection.
                    }
                    finally
                    {
                        Console.WriteLine("Error in TCPProvider's reader loop, reconnecting");
                        Disconnected?.Invoke(this, EventArgs.Empty);
                    }
                }
                Console.WriteLine("Exiting in TCPProvider's reader loop");
                Disconnected?.Invoke(this, EventArgs.Empty);
            }