Exemple #1
0
        /// <summary>
        /// Initialize InSim asynchronously.
        /// </summary>
        /// <param name="settings">The InSim settings.</param>
        /// <returns>A task object.</returns>
        public async Task InitializeAsync(InSimSettings settings)
        {
            PreInitialize(settings);

            try {
                if (Settings.IsRelayHost)
                {
                    await TcpSocket.ConnectAsync(RelayHost, RelayPort);
                }
                else
                {
                    await TcpSocket.ConnectAsync(Settings.Host, Settings.Port);

                    // Initialize InSim.
                    await SendAsync(GetInSimInitPacket());

                    PostInitialize();
                }

                OnInitialized(new InitializeEventArgs(Settings));
            }
            catch (SocketException ex) {
                if (ex.SocketErrorCode == SocketError.ConnectionRefused)
                {
                    throw new InSimException(StringResources.InSimCouldNotConnectMessage, ex);
                }
                throw;
            }
        }
Exemple #2
0
        /// <summary>
        /// Disconnects from LFS and releases all resources associated with the connection.
        /// </summary>
        public void Disconnect()
        {
            ThrowIfDisposed();
            ThrowIfNotConnected();

            TcpSocket.Disconnect();
            UdpSocket.Disconnect();

            OnDisconnected(new DisconnectedEventArgs(DisconnectReason.Request));
        }
Exemple #3
0
        /// <summary>
        /// Releases all resources used by the connection.
        /// </summary>
        /// <param name="disposing">Set true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!isDisposed && disposing)
            {
                isDisposed = true;

                TcpSocket.Dispose();
                UdpSocket.Dispose();
            }
        }
Exemple #4
0
 private void HandleKeepAlive(PacketType type, byte[] buffer)
 {
     if (type == PacketType.ISP_TINY)
     {
         IS_TINY tiny = new IS_TINY(buffer);
         if (tiny.SubT == TinyType.TINY_NONE)
         {
             TcpSocket.Send(buffer);
         }
     }
 }
Exemple #5
0
        /// <summary>
        /// Creates a new instance of the <see cref="InSim"/> class.
        /// </summary>
        public InSim()
        {
            TcpSocket = new TcpSocket();
            TcpSocket.PacketDataReceived += TcpSocket_PacketDataReceived;
            TcpSocket.ConnectionLost     += TcpSocket_ConnectionLost;
            TcpSocket.SocketError        += TcpSocket_SocketError;

            UdpSocket = new UdpSocket();
            UdpSocket.PacketDataReceived += UdpSocket_PacketDataReceived;
            UdpSocket.SocketError        += UdpSocket_SocketError;
        }
Exemple #6
0
        /// <summary>
        /// Sends the specified packet to LFS.
        /// </summary>
        /// <param name="packet">The<see cref="ISendable"/> packet to send.</param>
        public void Send(ISendable packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException("packet");
            }

            ThrowIfDisposed();
            ThrowIfNotConnected();

            TcpSocket.Send(packet.GetBuffer());
        }
Exemple #7
0
        /// <summary>
        /// Sends the specified sequence of packets to LFS asynchronously.
        /// </summary>
        /// <param name="packets">The sequence of <see cref="ISendable"/> packets to send.</param>
        /// <returns>An awaitable async task object.</returns>
        public Task SendAsync(params ISendable[] packets)
        {
            if (packets == null)
            {
                throw new ArgumentNullException("packets");
            }

            ThrowIfDisposed();
            ThrowIfNotConnected();

            return(TcpSocket.SendAsync(GetSendBuffer(packets)));
        }
Exemple #8
0
        /// <summary>
        /// Sends the specified sequence of packets to LFS.
        /// </summary>
        /// <param name="packets">The sequence of <see cref="ISendable"/> packets to send.</param>
        public void Send(params ISendable[] packets)
        {
            if (packets == null)
            {
                throw new ArgumentNullException("packets");
            }

            ThrowIfDisposed();
            ThrowIfNotConnected();

            TcpSocket.Send(GetSendBuffer(packets));
        }
Exemple #9
0
        /// <summary>
        /// Sends the specified packet to LFS asynchronously.
        /// </summary>
        /// <param name="packet">The<see cref="ISendable"/> packet to send.</param>
        /// <returns>An awaitable async task object.</returns>
        public Task SendAsync(ISendable packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException("packet");
            }

            ThrowIfDisposed();
            ThrowIfNotConnected();

            return(TcpSocket.SendAsync(packet.GetBuffer()));
        }
Exemple #10
0
        /// <summary>
        /// Initializes the connection with LFS.
        /// </summary>
        /// <param name="settings">A <see cref="InSimSettings"/> object containing information to initialize the connection with.</param>
        public void Initialize(InSimSettings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            ThrowIfDisposed();
            ThrowIfConnected();

            Settings = new ReadOnlyInSimSettings(settings);

            try {
                if (Settings.IsRelayHost)
                {
                    TcpSocket.Connect(RelayHost, RelayPort);
                }
                else
                {
                    TcpSocket.Connect(Settings.Host, Settings.Port);

                    Send(new IS_ISI {
                        Admin    = Settings.Admin,
                        Flags    = Settings.Flags,
                        IName    = Settings.IName,
                        Interval = Settings.Interval,
                        Prefix   = Settings.Prefix,
                        ReqI     = 1, // Request version.
                        UDPPort  = Settings.UdpPort,
                    });

                    if (Settings.UdpPort > 0)
                    {
                        UdpSocket.Bind(Settings.Host, Settings.UdpPort);
                    }
                }

                OnInitialized(new InitializeEventArgs(Settings));
            }
            catch (SocketException ex) {
                if (ex.SocketErrorCode == SocketError.ConnectionRefused)
                {
                    throw new InSimException(StringResources.InSimCouldNotConnectMessage, ex);
                }
                throw;
            }
        }
Exemple #11
0
        /// <summary>
        /// Sends the specified sequence of packets to LFS.
        /// </summary>
        /// <param name="packets">The sequence of <see cref="ISendable"/> packets to send.</param>
        public void Send(IEnumerable <ISendable> packets)
        {
            if (packets == null)
            {
                throw new ArgumentNullException("packets");
            }

            ThrowIfDisposed();
            ThrowIfNotConnected();

            int         size   = packets.Sum(p => p.Size);
            List <byte> buffer = new List <byte>(size);

            foreach (ISendable packet in packets)
            {
                buffer.AddRange(packet.GetBuffer());
            }
            TcpSocket.Send(buffer.ToArray());
        }
Exemple #12
0
        private void InitializeSockets()
        {
            if (TcpSocket != null && TcpSocket.IsDisposed)
            {
                TcpSocket.PacketDataReceived -= TcpSocket_PacketDataReceived;
                TcpSocket.ConnectionLost     -= TcpSocket_ConnectionLost;
                TcpSocket.SocketError        -= TcpSocket_SocketError;
            }

            TcpSocket = new TcpSocket();
            TcpSocket.PacketDataReceived += TcpSocket_PacketDataReceived;
            TcpSocket.ConnectionLost     += TcpSocket_ConnectionLost;
            TcpSocket.SocketError        += TcpSocket_SocketError;

            if (UdpSocket != null && UdpSocket.IsDisposed)
            {
                UdpSocket.PacketDataReceived -= UdpSocket_PacketDataReceived;
                UdpSocket.SocketError        -= UdpSocket_SocketError;
            }

            UdpSocket = new UdpSocket();
            UdpSocket.PacketDataReceived += UdpSocket_PacketDataReceived;
            UdpSocket.SocketError        += UdpSocket_SocketError;
        }
Exemple #13
0
 private void UdpSocket_SocketError(object sender, InSimErrorEventArgs e)
 {
     TcpSocket.Disconnect();
     OnInSimError(e);
 }