Exemplo n.º 1
0
 public ConnectionManager(int receiveBufferSize, IConnectionHandlerFactory connectionHandlerFactory)
 {
     _receiveBufferSize        = receiveBufferSize;
     _connectionHandlerFactory = connectionHandlerFactory;
 }
Exemplo n.º 2
0
        public async void Listen(IProtocol protocol)
        {
            Console.WriteLine($"Started listening on {protocol.GetType().Name.ToUpper()}.");

            _protocolPort = protocol switch
            {
                SimpleProtocol => 50001,
                XmlProtocol => 50002,
                SymmetricProtocol => 50003,
                AsymmetricProtocol => 50004,
                _ => _protocolPort
            };

            try
            {
                if (BindSocket())
                {
                    while (true)
                    {
                        try
                        {
                            Socket newConnectionSocket = _socket.Accept();
                            _handlerFactory = new SocketHandlerFactory(newConnectionSocket, protocol);

                            try
                            {
                                var socketHandler = _handlerFactory.Create("SocketHandler");
                                await socketHandler.HandleConnection();
                            }
                            catch (NotSupportedException notSuppE)
                            {
                                //Console.WriteLine($"Socket Handler Factory : Failed Initializing.");
                                throw notSuppE;
                            }
                        }
                        catch (SocketException se)
                        {
                            throw se;
                            //Console.WriteLine($"Socket Listener Exception : An error occurred when attempting to access the socket");
                        }
                        catch (ObjectDisposedException objDisE)
                        {
                            throw objDisE;
                            //Console.WriteLine($"Socket Listener Exception : Socket has been closed.");
                        }
                        catch (InvalidOperationException invOpeE)
                        {
                            throw invOpeE;
                            //Console.WriteLine($"Socket Listener Exception : Socket is not listenening for connections.");
                        }
                        catch (Exception e)
                        {
                            throw e;
                            //Console.WriteLine($"Socket Listener Exception : Something blocked the listener.");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
                //Console.WriteLine($"Socket Listener Exception : Something went completely wrong.");
            }
        }

        /*public Task NewClientConnection()
         * {
         *  if (!((List<SocketUser>) _socketUsersConnected).Any(u =>
         *      u.DestinationFrom == (IPEndPoint) _socket.RemoteEndPoint))
         *  {
         *      Console.WriteLine($"Client connected {((IPEndPoint) _socket.RemoteEndPoint).Address}");
         *      var newUser = UserBaseFactory.Create("SocketUser") as SocketUser;
         *      newUser.DestinationFrom = (IPEndPoint) _socket.RemoteEndPoint;
         *      _socketUsersConnected.Add(newUser);
         *      Console.WriteLine("a");
         *  }
         *  else
         *  {
         *      Console.WriteLine("Client already connected");
         *  }
         *
         *  return Task.CompletedTask;
         * }*/
    }