예제 #1
0
        public void Setup(Socket socket, string clientName, EndPoint clientEp, INetChannelHandler handler)
        {
            _socket        = socket;
            _remoteAddress = clientEp;

            _lastReceived = (float)Networking.NetTime;
            _connectTime  = (float)Networking.NetTime;

            _clientName     = clientName;
            _messageHandler = handler;

            _timeout = Networking.SignonTimeout;
            _rate    = Networking.DefaultRate;

            // Prevent the first message from getting dropped after connection is set up.
            _outSequenceNr    = 1;
            _inSequenceNr     = 0;
            _outSequenceNrAck = 0;
            _inReliableState  = 0; // last remote reliable state
            _outReliableState = 0; // our current reliable state
            _chokedPackets    = 0;

            _challengeNr   = 0;
            _chokedPackets = 0;

            handler.ConnectionStart(this);
        }
예제 #2
0
        public void Shutdown(string reason = null)
        {
            if (_socket == null)
            {
                return;
            }

            Clear();

            if (reason != null)
            {
                StreamUnreliable.WriteUShort((ushort)ENetCommand.NetDisconnect);
                StreamUnreliable.WriteString(reason);
                Transmit();
            }

            _socket        = null;
            _remoteAddress = null;

            if (_messageHandler != null)
            {
                _messageHandler.ConnectionClosing(reason);
                _messageHandler = null;
            }

            _netMesages.Clear();
            Networking.RemoveChannel(this);
        }
예제 #3
0
        public TcpChannel(string name, INetChannelHandler handler, int receivePacketHeaderLength)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Name is invalid.", nameof(name));
            }

            if (receivePacketHeaderLength <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(receivePacketHeaderLength), "Must be positive.");
            }

            Name    = name;
            Handler = handler ?? throw new ArgumentNullException(nameof(handler));
            ReceivePacketHeaderLength = receivePacketHeaderLength;
        }
        public INetChannel AddChannel(string name, string typeKey, INetChannelHandler handler, int receivePacketHeaderLength)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Invalid name.", nameof(name));
            }

            if (HasChannel(name))
            {
                throw new InvalidOperationException($"Already exists a channel with name '{name}'.");
            }

            var channel = ChannelFactory.Create(name, typeKey, handler, receivePacketHeaderLength);

            m_Channels.Add(channel);
            return(channel);
        }
예제 #5
0
        public static NetChannel CreateChannel(Socket sock, string clientName, EndPoint clientEp, INetChannelHandler handler, bool forceNewChannel = false)
        {
            NetChannel channel = null;

            if (!forceNewChannel && clientEp != null)
            {
                if ((channel = Networking.FindNetChannelFor(sock, clientEp)) != null)
                {
                    channel.Clear();
                }
            }

            if (channel == null)
            {
                channel = new NetChannel();
                _netChannels.Add(channel);
            }

            channel.Setup(sock, clientName, clientEp, handler);
            return(channel);
        }
 public INetChannel AddChannel(string name, string typeKey, INetChannelHandler handler, int receivePacketHeaderLength)
 {
     return(Module.AddChannel(name, typeKey, handler, receivePacketHeaderLength));
 }
예제 #7
0
 public INetChannel Create(string name, string typeKey, INetChannelHandler handler, int receivePacketHeaderLength)
 {
     return(new TcpChannel(name, handler, receivePacketHeaderLength));
 }