/// <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; } }
/// <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)); }
/// <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(); } }
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); } } }
/// <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; }
/// <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()); }
/// <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))); }
/// <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)); }
/// <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())); }
/// <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; } }
/// <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()); }
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; }
private void UdpSocket_SocketError(object sender, InSimErrorEventArgs e) { TcpSocket.Disconnect(); OnInSimError(e); }