Example #1
0
        /// <inheritdoc/>
        protected override IEnumerable <EndPoint> BindInternal(IEnumerable <EndPoint> localEndPoints)
        {
            HashSet <EndPoint> newLocalEPs = new HashSet <EndPoint>();

            lock (BoundHandlers)
            {
                foreach (EndPoint ep in localEndPoints)
                {
                    LoopbackEndPoint localEP = ep as LoopbackEndPoint;
                    if (localEP == null || localEP.Port == 0)
                    {
                        localEP = null;
                        for (Int32 i = 10000; i < Int32.MaxValue; i++)
                        {
                            LoopbackEndPoint newLocalEP = new LoopbackEndPoint(i);
                            if (!BoundHandlers.ContainsKey(newLocalEP) && !newLocalEPs.Contains(newLocalEP))
                            {
                                localEP = newLocalEP;
                                break;
                            }
                        }

                        if (localEP == null)
                        {
                            throw new IOException("No port available.");
                        }
                    }
                    else if (localEP.Port < 0)
                    {
                        throw new IOException("Bind port number must be 0 or above.");
                    }
                    else if (BoundHandlers.ContainsKey(localEP))
                    {
                        throw new IOException("Address already bound: " + localEP);
                    }

                    newLocalEPs.Add(localEP);
                }

                foreach (LoopbackEndPoint localEP in newLocalEPs)
                {
                    if (BoundHandlers.ContainsKey(localEP))
                    {
                        foreach (LoopbackEndPoint ep in newLocalEPs)
                        {
                            BoundHandlers.Remove(ep);
                        }
                        throw new IOException("Duplicate local address: " + localEP);
                    }
                    else
                    {
                        BoundHandlers[localEP] = new LoopbackPipe(this, localEP, Handler);
                    }
                }
            }

            _idleStatusChecker.Start();

            return(newLocalEPs);
        }
Example #2
0
        protected override IConnectFuture Connect0(EndPoint remoteEP, EndPoint localEP,
                                                   Action <IoSession, IConnectFuture> sessionInitializer)
        {
            endPoint = remoteEP as FileEndPoint;
            if (endPoint == null)
            {
                throw new ArgumentException("EndPoint must be FileEndPoint!");
            }
            IConnectFuture future  = new DefaultConnectFuture();
            var            session = new FileSession(this, endPoint);

            InitSession(session, future, sessionInitializer);

            try
            {
                session.Processor.Add(session);
            }
            catch (IOException ex)
            {
                return(DefaultConnectFuture.NewFailedFuture(ex));
            }

            idleStatusChecker.Start();

            return(future);
        }
Example #3
0
        /// <inheritdoc/>
        protected override IEnumerable <EndPoint> BindInternal(IEnumerable <EndPoint> localEndPoints)
        {
            Dictionary <EndPoint, System.Net.Sockets.Socket> newListeners = new Dictionary <EndPoint, System.Net.Sockets.Socket>();

            try
            {
                // Process all the addresses
                foreach (EndPoint localEP in localEndPoints)
                {
                    EndPoint ep = localEP;
                    if (ep == null)
                    {
                        ep = new IPEndPoint(IPAddress.Any, 0);
                    }
                    System.Net.Sockets.Socket listenSocket = new System.Net.Sockets.Socket(ep.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
                    new DatagramSessionConfigImpl(listenSocket).SetAll(SessionConfig);
                    listenSocket.Bind(ep);
                    newListeners[listenSocket.LocalEndPoint] = listenSocket;
                }
            }
            catch (Exception)
            {
                // Roll back if failed to bind all addresses
                foreach (System.Net.Sockets.Socket listenSocket in newListeners.Values)
                {
                    try
                    {
                        listenSocket.Close();
                    }
                    catch (Exception ex)
                    {
                        ExceptionMonitor.Instance.ExceptionCaught(ex);
                    }
                }

                throw;
            }

            foreach (KeyValuePair <EndPoint, System.Net.Sockets.Socket> pair in newListeners)
            {
                SocketContext ctx = new SocketContext(pair.Value, SessionConfig);
                _listenSockets[pair.Key] = ctx;
                BeginReceive(ctx);
            }

            _idleStatusChecker.Start();

            return(newListeners.Keys);
        }
Example #4
0
        /// <inheritdoc/>
        protected override IConnectFuture Connect0(EndPoint remoteEP, EndPoint localEP, Action <IoSession, IConnectFuture> sessionInitializer)
        {
            ISerialSessionConfig config = (ISerialSessionConfig)SessionConfig;
            SerialEndPoint       sep    = (SerialEndPoint)remoteEP;

            SerialPort serialPort = new SerialPort(sep.PortName, sep.BaudRate, sep.Parity, sep.DataBits, sep.StopBits);

            if (config.ReadBufferSize > 0)
            {
                serialPort.ReadBufferSize = config.ReadBufferSize;
            }
            if (config.ReadTimeout > 0)
            {
                serialPort.ReadTimeout = config.ReadTimeout * 1000;
            }
            if (config.WriteBufferSize > 0)
            {
                serialPort.WriteBufferSize = config.WriteBufferSize;
            }
            if (config.WriteTimeout > 0)
            {
                serialPort.WriteTimeout = config.WriteTimeout * 1000;
            }
            if (config.ReceivedBytesThreshold > 0)
            {
                serialPort.ReceivedBytesThreshold = config.ReceivedBytesThreshold;
            }

            IConnectFuture future  = new DefaultConnectFuture();
            SerialSession  session = new SerialSession(this, sep, serialPort);

            InitSession(session, future, sessionInitializer);

            try
            {
                session.Processor.Add(session);
            }
            catch (IOException ex)
            {
                return(DefaultConnectFuture.NewFailedFuture(ex));
            }

            _idleStatusChecker.Start();

            return(future);
        }