예제 #1
0
 private void ThreadConnectionProc(object state)
 {
     while (true)
     {
         try
         {
             Socket s = _srvSocket.Accept();
             if (s != null)
             {
                 IChannel ch = new SocketChannel(s, true);                         /// TODO: Implement a generic mechanism to decouple Port from the type of channel to be created
                 if (_channelMgr != null)
                 {
                     IPEndPoint p   = (IPEndPoint)s.RemoteEndPoint;
                     String     uri = TcpIpUri.AddressPortAsUri(p.Address.ToString(), p.Port);
                     ch.Uri = uri;
                     //>> LDRTEST
                     // Check if there is an old version of the channel
                     // This means that the closing of the socket was not detected
                     if (_channelMgr.GetChannel(uri) != null)
                     {
                         _channelMgr.CloseChannel(uri);
                         CommMain.Logger.AddLog("Port.ThreadConnectionProc: Forced to close channel: " + uri, LoggerSeverities.Error);
                     }
                     //<< LDRTEST
                     _channelMgr.AddChannel(uri, ch);
                 }
                 if (_connHandler != null)
                 {
                     _connHandler(ch);
                 }
                 else
                 {
                     CommMain.Logger.AddLog("WARNING: Port.NotifySendError: ThreadConnectionProc is null.", LoggerSeverities.Error);
                 }
                 //>> LDR 2004.07.16
                 SocketChannel sch = (SocketChannel)ch;
                 sch.Run();
                 //<< LDR 2004.07.16
             }
         }
         catch (SocketException sockEx)
         {
             CommMain.Logger.AddLog(sockEx);
             if (Closing)
             {
                 break;
             }
         }
         catch (Exception ex)
         {
             CommMain.Logger.AddLog(ex);
         }
     }
 }
예제 #2
0
        /// <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>
        /// Returns the URI for the destination specified in a message row
        /// </summary>
        /// <param name="row">The row containing data for a message</param>
        /// <returns>The URI of the message destination</returns>
        private string GetDestinationUri(DataRow row)
        {
            string destURI = null;
            string addr    = "0.0.0.0";
            int    port    = Convert.ToInt32(row[(int)MsgsColumns.PortAdapter]);

            if (!DBNull.Value.Equals(row[(int)MsgsColumns.IpAdapter]))
            {
                addr = (string)row[(int)MsgsColumns.IpAdapter];
            }
            else
            {
                addr = Configuration.AddressCache.GetAddressCache().GetUnitAddress(
                    (decimal)row[(int)MsgsColumns.UniId]);
            }
            destURI = TcpIpUri.AddressPortAsUri(addr, port);

            return(destURI);
        }
        /// <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);
                }
            }
        }
예제 #5
0
 /// <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);
 }