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. } } }
/// <summary> /// Create a new LCM object, connecting to one or more URLs. If /// no URL is specified, the environment variable LCM_DEFAULT_URL is /// used. If that environment variable is not defined, then the /// default URL is used. /// </summary> /// <param name="urls">URL array</param> public LCM(params string[] urls) { if (urls.Length == 0) { string env = Environment.GetEnvironmentVariable("LCM_DEFAULT_URL"); if (env == null) { urls = new string[] { "udpm://239.255.76.67:7667" }; } else { urls = new string[] { env }; } } foreach (string url in urls) { URLParser up = new URLParser(url); string protocol = up.Get("protocol"); if (protocol.Equals("udpm")) { providers.Add(new UDPMulticastProvider(this, up)); } else if (protocol.Equals("tcpq")) { TCPProvider prov = new TCPProvider(this, up); prov.Connected += OnTCPConnected; prov.Disconnected += OnTCPDisconnected; providers.Add(prov); }/* * else if (protocol.Equals("file")) * { * providers.Add(new LogFileProvider(this, up)); * }*/ else { Console.Error.WriteLine("LCM: Unknown URL protocol: " + protocol); } } }
public ReaderThread(TCPProvider provider) { this.provider = provider; }
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); }
public ReaderThread(TCPProvider provider) { this.provider = provider; }