/// <summary> /// Creates a channel to exchange data with the end-point represented by /// the supplied uniform resource identifier. If a channel to this end-point is already open, /// it returns the existing channel /// </summary> /// <param name="uri"></param> /// <param name="type">The type of channel to open. From the ChannelType enumeration</param> /// <returns>The channel requested or null if an error occurs</returns> /// <remarks>In this version it only handles SocketChannel</remarks> public virtual IChannel OpenChannel(string uri, ChannelType type) { String ipAddr; int port; IChannel ch = null; lock (_channels.SyncRoot) { try { if (type.Equals(ChannelType.Socket)) { if (!_channels.ContainsKey(uri)) { Socket socket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); TcpIpUri.UriAsAddressPort(uri, out ipAddr, out port); socket.Connect(new IPEndPoint(IPAddress.Parse(ipAddr), port)); if (socket.Connected) { ch = new SocketChannel(socket, true); /// TODO: Implement a generic mechanism to decouple ChannelManager from the type of channel to be created ch.Uri = uri; AddToChannels(uri, ch); } } else { ch = (IChannel)_channels[uri]; } } } catch (SocketException /*sockEx*/) { //Debug.WriteLine("OpenChannel socket error - " + sockEx.Message); //CommMain.Logger.AddLog(sockEx); --> not necessary as we throw the exception throw; } catch (Exception /*ex*/) { //Debug.WriteLine("OpenChannel error - " + ex.Message); //CommMain.Logger.AddLog(ex); --> not necessary as we throw the exception throw; } } return(ch); }
/// <summary> /// Updates the address cache with the unit of a packet source /// </summary> /// <param name="packet">A received packet</param> /// <param name="uri">The uri of the channel through which the packet came</param> private void UpdateAddressCache(OPSTelegrama packet, string uri) { string ip; int port; TcpIpUri.UriAsAddressPort(uri, out ip, out port); OPSPacketizer packetizer = new OPSPacketizer(packet.XmlData); PacketData packData = packetizer.PacketInfo; if (packData != null) { string src = packData.SourceId; if ((src != null) && (src != "")) { Configuration.AddressCache.GetAddressCache().CacheUnitAddress( decimal.Parse(src), ip); } } }
/// <summary> /// Constructor that takes the URI where the port listens for connections /// </summary> /// <param name="uri">The uniform resource identifier where the port listens for /// connections to arrive</param> /// <remarks>The current version only supports TCP/IP URIs and SocketChannel /// </remarks> public Port(String uri) { TcpIpUri.UriAsAddressPort(uri, out _ipAddress, out _port); }