Esempio n. 1
0
        /// <summary>
        /// Begins listening for clients.
        /// </summary>
        /// <param name="port">Port to listen for clients on.</param>
        public void Listen(ushort port)
        {
            if (PacketTypes.Count == 0)
            {
                throw new Exception("No packet types added");
            }

            this.Port = port;
            try
            {
                if (!Listening)
                {
                    lock (_clientsLock)
                    {
                        _clients = new List <Client>();
                    }

                    _item            = new SocketAsyncEventArgs();
                    _item.Completed += AcceptClient;

                    if (_handle != null)
                    {
                        try
                        {
                            _handle.Close();
                        }
                        catch
                        {
                        }
                    }

                    if (BufferManager == null)
                    {
                        BufferManager = new PooledBufferManager(BUFFER_SIZE, 1)
                        {
                            ClearOnReturn = true
                        }
                    }
                    ;

                    _handle = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    _handle.Bind(new IPEndPoint(IPAddress.Any, port));
                    _handle.Listen(1000);

                    Processing = false;

                    Listening = true;
                    OnServerState(true);

                    if (!_handle.AcceptAsync(_item))
                    {
                        AcceptClient(null, _item);
                    }
                }
            }
            catch (Exception)
            {
                Disconnect();
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Constructor of the server, initializes variables.
 /// </summary>
 protected Server()
 {
     _clients      = new List <Client>();
     BufferManager = new PooledBufferManager(BUFFER_SIZE, 1)
     {
         ClearOnReturn = false
     };
 }
Esempio n. 3
0
 /// <summary>
 /// Constructor of the server, initializes variables.
 /// </summary>
 protected Server()
 {
     _clients      = new List <Client>();
     BufferManager = new PooledBufferManager(BUFFER_SIZE, 1)
     {
         ClearOnReturn = false
     };
     AddTypesToSerializer(typeof(IMessage), PacketRegistry.GetPacketTypes(typeof(IMessage)).ToArray());
 }
Esempio n. 4
0
        /// <summary>
        /// Begins listening for clients.
        /// </summary>
        /// <param name="port">Port to listen for clients on.</param>
        public void Listen(ushort port)
        {
            this.Port = port;
            try
            {
                if (!Listening)
                {
                    lock (_clientsLock)
                    {
                        _clients = new List<Client>();
                    }

                    _item = new SocketAsyncEventArgs();
                    _item.Completed += AcceptClient;

                    if (_handle != null)
                    {
                        try
                        {
                            _handle.Close();
                        }
                        catch
                        {
                        }
                    }

                    if (BufferManager == null)
                        BufferManager = new PooledBufferManager(BUFFER_SIZE, 1) { ClearOnReturn = true };

                    _handle = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    _handle.Bind(new IPEndPoint(IPAddress.Any, port));
                    _handle.Listen(1000);

                    Processing = false;

                    Listening = true;
                    OnServerState(true);

                    if (!_handle.AcceptAsync(_item))
                        AcceptClient(null, _item);
                }
            }
            catch (Exception)
            {
                Disconnect();
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Begins listening for clients.
        /// </summary>
        /// <param name="port">Port to listen for clients on.</param>
        public void Listen(ushort port)
        {
            if (PacketTypes.Count == 0)
            {
                throw new Exception("No packet types added");
            }

            this.Port = port;
            try
            {
                if (!Listening)
                {
                    lock (_clientsLock)
                    {
                        _clients = new List <Client>();
                    }

                    _item            = new SocketAsyncEventArgs();
                    _item.Completed += AcceptClient;

                    if (_handle != null)
                    {
                        try
                        {
                            _handle.Close();
                        }
                        catch
                        {
                        }
                    }

                    if (BufferManager == null)
                    {
                        BufferManager = new PooledBufferManager(BUFFER_SIZE, 1)
                        {
                            ClearOnReturn = true
                        }
                    }
                    ;

                    _handle = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    _handle.Bind(new IPEndPoint(IPAddress.Any, port));
                    _handle.Listen(1000);

                    Processing = false;

                    Listening = true;
                    OnServerState(true);

                    if (!_handle.AcceptAsync(_item))
                    {
                        AcceptClient(null, _item);
                    }
                }
            }
            catch (SocketException ex)
            {
                if (ex.ErrorCode == 10048)
                {
                    MessageBox.Show("The port is already in use.", "Listen Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show(
                        string.Format(
                            "An unexpected socket error occurred: {0}\n\nError Code: {1}\n\nPlease report this as fast as possible here:\n{2}/issues",
                            ex.Message, ex.ErrorCode, Settings.RepositoryURL), "Unexpected Listen Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                Disconnect();
            }
            catch (Exception)
            {
                Disconnect();
            }
        }