public ITransport Connect(string address, string port, IDictionary<string, string> capabilities) { IPAddress[] addr = Dns.GetHostAddresses(address); TcpClient client = null; IPEndPoint endPoint = null; //try to connect to the address CannotConnectException error = null; for (int i = 0; i < addr.Length; i++) { try { endPoint = new IPEndPoint(addr[0], Int32.Parse(port)); client = new TcpClient(); client.NoDelay = true; client.ReceiveTimeout = 1; client.SendTimeout = 1; client.Connect(endPoint); client.Client.Blocking = false; error = null; break; } catch (Exception e) { error = new CannotConnectException(String.Format("Cannot connect to {0}/{1}: {2}", address, port, e.Message), e); error.SourceComponent = this; } } if (error != null) { throw error; } // FIXME: a handshake is between two people; we assume that if they don't want // to talk to us then they'll close the connexion. // This is the GT (UDP) protocol 1.0: // bytes 0 - 3: the protocol version (ASCII for "GT10") // bytes 4 - n: the number of bytes in the capability dictionary (see ByteUtils.EncodeLength) // bytes n+1 - end: the capability dictionary MemoryStream ms = new MemoryStream(4 + 60); // approx: 4 bytes for protocol, 50 for capabilities Debug.Assert(ProtocolDescriptor.Length == 4); ms.Write(ProtocolDescriptor, 0, 4); ByteUtils.EncodeLength(ByteUtils.EncodedDictionaryByteCount(capabilities), ms); ByteUtils.EncodeDictionary(capabilities, ms); client.Client.Send(ms.GetBuffer(), 0, (int)ms.Length, SocketFlags.None); log.Debug("Now connected via TCP: " + endPoint); return new TcpTransport(client); }
public ITransport Connect(string address, string portDescription, IDictionary<string, string> capabilities) { IPAddress[] addr; try { addr = Dns.GetHostAddresses(address); } catch (SocketException e) { throw new CannotConnectException("Cannot resolve hostname: " + address, e); } int port; try { port = Int32.Parse(portDescription); } catch (FormatException e) { throw new CannotConnectException("invalid port: " + portDescription, e); } //try to connect to the address CannotConnectException error = null; for (int i = 0; i < addr.Length; i++) { UdpClient client = null; try { IPEndPoint endPoint = new IPEndPoint(addr[i], port); client = new UdpClient(); client.DontFragment = true; // FIXME: what are the implications of setting this flag? client.Client.Blocking = false; client.Client.SendTimeout = 1; client.Client.ReceiveTimeout = 1; client.Connect(endPoint); ShakeHands(client, capabilities); log.Debug("Now connected to UDP: " + client.Client.RemoteEndPoint); return factory.CreateTransport(client); } catch (SocketException e) { if (client != null) { client.Close(); client = null; } error = new CannotConnectException( String.Format("Cannot connect to {0}/{1}: {2}", address, port, e.Message)); log.Info(e.Message); error.SourceComponent = this; } catch (CannotConnectException e) { log.Info(e.Message); error = e; } } if (error != null) { throw error; } throw new CannotConnectException( String.Format("Unable to connect to remote {0}/{1}", address, port)); }
public ITransport Connect(string address, string portDescription, IDictionary<string, string> capabilities) { IPAddress[] addresses; int port; try { addresses = Dns.GetHostAddresses(address); } catch (SocketException e) { throw new CannotConnectException("Cannot resolve hostname: " + address, e); } if (addresses.Length == 0) { throw new CannotConnectException( String.Format("Cannot resolve address", address)); } try { port = Int32.Parse(portDescription); } catch (FormatException e) { throw new CannotConnectException("invalid port: " + portDescription, e); } //try to connect to the address TcpClient client = null; IPEndPoint endPoint = null; CannotConnectException error = null; foreach (IPAddress addr in addresses) { // in case hanging around from last iteration if (client != null) { try { client.Close(); client = null; } catch (Exception) {/*ignore*/} } try { endPoint = new IPEndPoint(addr, port); client = new TcpClient(); client.NoDelay = true; client.ReceiveTimeout = 1; client.SendTimeout = 1; client.Connect(endPoint); // must set to blocking *after* the connect client.Client.Blocking = false; error = null; break; } catch (SocketException e) { String errorMsg = String.Format("Cannot connect to {0}: {1}", endPoint, e.Message); error = new CannotConnectException(errorMsg); error.SourceComponent = this; log.Debug(errorMsg); } } if (error != null) { log.Info(error.Message); throw error; } // FIXME: a handshake is between two people; we assume that if they don't want // to talk to us then they'll close the connexion. // This is the GT (UDP) protocol 1.0: // bytes 0 - 3: the protocol version (ASCII for "GT10") // bytes 4 - n: the number of bytes in the capability dictionary (see ByteUtils.EncodeLength) // bytes n+1 - end: the capability dictionary MemoryStream ms = new MemoryStream(4 + 60); // approx: 4 bytes for protocol, 50 for capabilities Debug.Assert(ProtocolDescriptor.Length == 4); ms.Write(ProtocolDescriptor, 0, 4); ByteUtils.EncodeLength(ByteUtils.EncodedDictionaryByteCount(capabilities), ms); ByteUtils.EncodeDictionary(capabilities, ms); client.Client.Send(ms.GetBuffer(), 0, (int)ms.Length, SocketFlags.None); log.Debug("Now connected via TCP: " + endPoint); return new TcpTransport(client); }