Exemple #1
0
        protected void InitBase(NetAppConfiguration config, NetLog log)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (log == null)
            {
                throw new ArgumentNullException("log");
            }

            IsLittleEndian = BitConverter.IsLittleEndian;
            //if (BitConverter.IsLittleEndian)
            BitWriter = new LittleEndianBitWriter();
            //else
            //	BitWriter = new BigEndianBitWriter();

            Configuration = config;
            Log           = log;

            Configuration.m_isLocked = true;             // prevent changes

            // validate config
            if (config.ApplicationIdentifier == NetConstants.DefaultApplicationIdentifier)
            {
                log.Error("Warning! ApplicationIdentifier not set in configuration!");
            }

            if (this is NetServer)
            {
                if (config.MaximumConnections == -1)
                {
                    throw new ArgumentException("MaximumConnections must be set in configuration!");
                }
                if (config.ServerName == NetConstants.DefaultServerName)
                {
                    log.Warning("Warning! Server name not set!");
                }
            }

            // create buffers
            m_sendBuffer    = new NetBuffer(config.SendBufferSize);
            m_receiveBuffer = new NetBuffer(config.ReceiveBufferSize);

            // Bind to port
            try
            {
                IPEndPoint iep = new IPEndPoint(IPAddress.Any, config.Port);
                EndPoint   ep  = (EndPoint)iep;

                m_socket          = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                m_socket.Blocking = false;
                m_socket.Bind(ep);
                if (iep.Port != 0)
                {
                    Log.Info("Bound to port " + iep.Port);
                }
            }
            catch (SocketException sex)
            {
                if (sex.SocketErrorCode != SocketError.AddressAlreadyInUse)
                {
                    throw new NetException("Failed to bind to port " + config.Port + " - Address already in use!", sex);
                }
            }
            catch (Exception ex)
            {
                throw new NetException("Failed to bind to port " + config.Port, ex);
            }

            m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, config.ReceiveBufferSize);
            m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, config.SendBufferSize);

            m_senderRemote = (EndPoint) new IPEndPoint(IPAddress.Any, 0);
#if DEBUG
            m_lagLoss = new NetLogLossInducer(log);
#endif

            return;
        }
Exemple #2
0
        protected void InitBase(NetAppConfiguration config, NetLog log)
        {
            if (config == null)
                throw new ArgumentNullException("config");

            if (log == null)
                throw new ArgumentNullException("log");

            IsLittleEndian = BitConverter.IsLittleEndian;
            //if (BitConverter.IsLittleEndian)
            BitWriter = new LittleEndianBitWriter();
            //else
            //	BitWriter = new BigEndianBitWriter();

            Configuration = config;
            Log = log;

            Configuration.m_isLocked = true; // prevent changes

            // validate config
            if (config.ApplicationIdentifier == NetConstants.DefaultApplicationIdentifier)
                log.Error("Warning! ApplicationIdentifier not set in configuration!");

            if (this is NetServer)
            {
                if (config.MaximumConnections == -1)
                    throw new ArgumentException("MaximumConnections must be set in configuration!");
                if (config.ServerName == NetConstants.DefaultServerName)
                    log.Warning("Warning! Server name not set!");
            }

            // create buffers
            m_sendBuffer = new NetBuffer(config.SendBufferSize);
            m_receiveBuffer = new NetBuffer(config.ReceiveBufferSize);

            // Bind to port
            try
            {
                IPEndPoint iep = new IPEndPoint(IPAddress.Any, config.Port);
                EndPoint ep = (EndPoint)iep;

                m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                m_socket.Blocking = false;
                m_socket.Bind(ep);
                if (iep.Port != 0)
                    Log.Info("Bound to port " + iep.Port);
            }
            catch (SocketException sex)
            {
                if (sex.SocketErrorCode != SocketError.AddressAlreadyInUse)
                    throw new NetException("Failed to bind to port " + config.Port + " - Address already in use!", sex);
            }
            catch (Exception ex)
            {
                throw new NetException("Failed to bind to port " + config.Port, ex);
            }

            m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, config.ReceiveBufferSize);
            m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, config.SendBufferSize);

            m_senderRemote = (EndPoint)new IPEndPoint(IPAddress.Any, 0);
            #if DEBUG
            m_lagLoss = new NetLogLossInducer(log);
            #endif

            return;
        }