Beispiel #1
0
        public void SendDelayedPackets(NetBase netBase, double now)
        {
            foreach (DelayedPacket pk in m_delayed)
            {
                if (now >= pk.DelayedUntil)
                {
                    // send it!
                    m_delaySent.Add(pk);
                    try
                    {
                        //m_log.Debug("(sending delayed packet after " + (int)(pk.DelayAmount * 1000.0f) + " ms)");
                        netBase.m_socket.SendTo(pk.Data, 0, pk.Data.Length, SocketFlags.None, pk.RemoteEP);
                    }
                    catch (SocketException sex)
                    {
                        if (sex.SocketErrorCode == SocketError.ConnectionReset ||
                            sex.SocketErrorCode == SocketError.ConnectionRefused ||
                            sex.SocketErrorCode == SocketError.ConnectionAborted)
                        {
                            m_log.Warning("Remote socket forcefully closed: " + sex.SocketErrorCode);
                            //if (connection != null)
                            //	connection.Disconnect("Socket forcefully closed: " + sex.SocketErrorCode);
                            continue;
                        }

                        m_log.Warning("Execute SocketException: " + sex.SocketErrorCode);
                        continue;
                    }
                }
            }

            if (m_delaySent.Count > 0)
            {
                foreach (DelayedPacket pk in m_delaySent)
                {
                    m_delayed.Remove(pk);
                }
                m_delaySent.Clear();
            }
        }
Beispiel #2
0
        protected bool ReadPacket()
        {
            try
            {
                if (m_socket == null || m_socket.Available < 1)
                {
                    return(false);
                }

                int        bytesReceived = m_socket.ReceiveFrom(m_receiveBuffer.Data, 0, m_receiveBuffer.Data.Length, SocketFlags.None, ref m_senderRemote);
                IPEndPoint ipsender      = (IPEndPoint)m_senderRemote;
                //Log.Verbose("Read " + bytesReceived + " bytes from " + ipsender);
                m_receiveBuffer.SetDataLength(bytesReceived);

                if (bytesReceived < 0)
                {
                    return(true);
                }

                m_receiveBuffer.ResetReadPointer();
                HandlePacket(m_receiveBuffer, bytesReceived, ipsender);
                return(true);
            }
            catch (SocketException sex)
            {
                //if (sex.SocketErrorCode == SocketError.ConnectionReset)
                //{
                //	// who disconnected?
                //	HandleConnectionReset((IPEndPoint)m_senderRemote);
                //	return false;
                //}
                Log.Warning("ReadPacket socket exception: " + sex.SocketErrorCode);
                return(false);
            }
            catch (Exception ex)
            {
                throw new NetException("ReadPacket() exception", ex);
            }
        }
Beispiel #3
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;
        }
Beispiel #4
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;
        }