public void Connect() { try { this.m_client = new TcpClient(); TcpClient client = this.m_client; ServerConnectionDetails connectionDetails = this.ConnectionDetails; string serverAddress = connectionDetails.ServerAddress; connectionDetails = this.ConnectionDetails; int serverPort = connectionDetails.ServerPort; client.ConnectAsync(serverAddress, serverPort).ContinueWith((Action <Task>)(t => this.OnConnected(t))); } catch (ArgumentNullException ex) { this.OnConnectionProblems("Provided hostname is null."); } catch (ArgumentOutOfRangeException ex) { this.OnConnectionProblems("Invalid portnumber provided."); } catch (SocketException ex) { this.OnConnectionProblems("The provided address and port are inaccessible."); } }
public LocallyInitiatedIheTransactionTests() { ServerConnectionDetails serverConnection = new ServerConnectionDetails(new IPEndPoint(IPAddress.Loopback, _port)); _server = new MllpServer(serverConnection, new TestMiddleware()); _server.Start(); }
public ServerSession(ServerConnectionDetails serverConnectionDetails) { this.ConnectionDetails = serverConnectionDetails; this.m_cachedGetterData = new Dictionary <RconGetter, string[]>(); this.m_communicationMutex = new Mutex(); this.Connect(); }
/// <inheritdoc /> public override bool TryGetWorldProvider(ServerConnectionDetails connectionDetails, PlayerProfile profile, out WorldProvider worldProvider, out NetworkProvider networkProvider) { worldProvider = new BedrockWorldProvider(Alex, connectionDetails.EndPoint, profile, out networkProvider); return(true); }
/// <inheritdoc /> public override bool TryGetWorldProvider(ServerConnectionDetails connectionDetails, PlayerProfile profile, out WorldProvider worldProvider, out NetworkProvider networkProvider) { worldProvider = new BedrockWorldProvider(Alex, connectionDetails.EndPoint, profile, new DedicatedThreadPool(new DedicatedThreadPoolSettings(4, "Bedrock Thread")), out networkProvider); return(true); }
private MllpServer StartupMllpServer(bool useSsl = false, bool forceClientAuthentication = false, RemoteCertificateValidationCallback clientCertificateValidationCallback = null) { string address = IPAddress.Loopback.ToString(); int uniquePort = Interlocked.Increment(ref port); ServerSecurityDetails serverSecurityDetails = useSsl ? new ServerSecurityDetails(serverCertificate, forceClientAuthentication, clientCertificateValidationCallback) : null; ServerConnectionDetails serverConnectionDetails = new ServerConnectionDetails(address, uniquePort, null, serverSecurityDetails); MllpServer mllpServer = new MllpServer(serverConnectionDetails, new TestMiddleware()); mllpServer.Start(); return(mllpServer); }
/// <inheritdoc /> public override bool TryGetWorldProvider(ServerConnectionDetails connectionDetails, PlayerProfile playerProfile, out WorldProvider worldProvider, out NetworkProvider networkProvider) { worldProvider = new JavaWorldProvider(Alex, connectionDetails.EndPoint, playerProfile, new DedicatedThreadPool(new DedicatedThreadPoolSettings(2)), out networkProvider) { Hostname = connectionDetails.Hostname }; return(true); }
public ConnectDialog(ServerConnectionDetails connectionDetails = default(ServerConnectionDetails)) { this.InitializeComponent(); if (connectionDetails.Equals((object)new ServerConnectionDetails())) { this.txtAddress.Text = Settings.Default.LastUsedServerAddress; this.txtPort.Text = Settings.Default.LastUsedServerPort.ToString(); } else { this.txtAddress.Text = connectionDetails.ServerAddress; this.txtPort.Text = connectionDetails.ServerPort.ToString(); this.txtPassword.Password = connectionDetails.ServerPassword; } this.txtblockError.Text = this.VersionString(); }
public virtual bool TryGetWorldProvider(ServerConnectionDetails connectionDetails, PlayerProfile playerProfile, out WorldProvider worldProvider, out NetworkProvider networkProvider) { worldProvider = null; networkProvider = null; return(false); }
/// <inheritdoc /> public async Task QueryServerAsync(ServerConnectionDetails connectionDetails, PingServerDelegate pingCallback, ServerStatusDelegate statusCallBack) { ManualResetEventSlim ar = new ManualResetEventSlim(false); Stopwatch sw = new Stopwatch(); long pingTime = 0; /*var result = await ResolveHostnameAsync(hostname); * if (!result.Success) * { * statusCallBack?.Invoke(new ServerQueryResponse(false, "multiplayer.status.cannot_resolve", new ServerQueryStatus() * { * Delay = sw.ElapsedMilliseconds, * Success = false, * * EndPoint = null, * Address = hostname, * Port = port * })); * * return; * }*/ using (MiNET.Utils.DedicatedThreadPool threadPool = new MiNET.Utils.DedicatedThreadPool(new MiNET.Utils.DedicatedThreadPoolSettings(1, MiNET.Utils.ThreadType.Background, "ServerPingThread"))) { BedrockClient client = null; try { IPEndPoint serverEndpoint = new IPEndPoint(connectionDetails.EndPoint.Address, (int)connectionDetails.EndPoint.Port); client = new BedrockClient(Game, null, serverEndpoint, new PlayerProfile(string.Empty, $"Pinger{serverEndpoint.ToString()}", $"Pinger{serverEndpoint.ToString()}", null, null, null, "xbox"), threadPool, null) { //IgnoreUnConnectedPong = true }; client.Connection.AutoConnect = false; BedrockMotd motd = new BedrockMotd(string.Empty); client.OnMotdReceivedHandler += (sender, m) => { motd = m; pingTime = m.Latency; ar.Set(); pingCallback.Invoke(new ServerPingResponse(true, pingTime)); }; client.Start(ar); //client.SendUnconnectedPing(); sw.Restart(); //ar.WaitAsync().Wait(TimeSpan.FromMilliseconds(10000)); if (await WaitHandleHelpers.FromWaitHandle(ar.WaitHandle, TimeSpan.FromMilliseconds(10000))) { client.Close(); // var m = new BedrockMotd(client.Connection.RemoteServerName); var compatability = CompatibilityResult.Unknown; if (motd.ProtocolVersion == McpeProtocolInfo.ProtocolVersion) { compatability = CompatibilityResult.Compatible; } /*else if (MathF.Abs(motd.ProtocolVersion - McpeProtocolInfo.ProtocolVersion) < 3) //Ehh? * { * compatability = CompatibilityResult.Unknown; * }*/ else if (motd.ProtocolVersion < McpeProtocolInfo.ProtocolVersion) { // compatability = CompatibilityResult.OutdatedServer; } else if (motd.ProtocolVersion > McpeProtocolInfo.ProtocolVersion) { // compatability = CompatibilityResult.OutdatedClient; } statusCallBack?.Invoke(new ServerQueryResponse(true, new ServerQueryStatus() { EndPoint = motd.ServerEndpoint, Delay = pingTime, Success = true, Address = connectionDetails.Hostname, Port = (ushort)connectionDetails.EndPoint.Port, WaitingOnPing = false, Query = new ServerQuery() { Players = new Players() { Max = motd.MaxPlayers, Online = motd.Players }, Version = new API.Services.Version() { Protocol = motd.ProtocolVersion, Name = motd.ClientVersion, Compatibility = compatability }, Description = new Description() { Text = motd.MOTD }, Modinfo = null, Favicon = null } })); } else { statusCallBack?.Invoke(new ServerQueryResponse(false, "multiplayer.status.cannot_connect", new ServerQueryStatus() { EndPoint = serverEndpoint, Delay = sw.ElapsedMilliseconds, Success = false, Address = connectionDetails.Hostname, Port = (ushort)connectionDetails.EndPoint.Port, WaitingOnPing = false })); } } catch (Exception e) { Log.Error($"Could not get bedrock query: {e.ToString()}"); statusCallBack?.Invoke(new ServerQueryResponse(false, "Failed to connect...", new ServerQueryStatus() { Success = false })); } finally { client?.Dispose(); } } }
private static async Task QueryJavaServerAsync(ServerConnectionDetails connectionDetails, PingServerDelegate pingCallback, ServerStatusDelegate statusCallBack) { IPEndPoint endPoint = null; var sw = Stopwatch.StartNew(); string jsonResponse = null; try { /*var result = await ResolveHostnameAsync(hostname); * if (!result.Success) * { * statusCallBack?.Invoke(new ServerQueryResponse(false, "multiplayer.status.cannot_resolve", new ServerQueryStatus() * { * Delay = sw.ElapsedMilliseconds, * Success = false, * * EndPoint = null, * Address = hostname, * Port = port * })); * * return; * }*/ bool waitingOnPing = true; using (TcpClient client = new TcpClient()) { await client.ConnectAsync(connectionDetails.EndPoint.Address, connectionDetails.EndPoint.Port); endPoint = client.Client.RemoteEndPoint as IPEndPoint; if (client.Connected) { //conn = new NetConnection(Direction.ClientBound, client.Client); //conn.LogExceptions = false; using (var conn = new NetConnection(Direction.ClientBound, client.Client) { LogExceptions = true }) { long pingId = Rnd.NextUInt(); long pingResult = 0; EventWaitHandle ar = new EventWaitHandle(false, EventResetMode.AutoReset); conn.OnPacketReceived += (sender, args) => { if (args.Packet is ResponsePacket responsePacket) { jsonResponse = responsePacket.ResponseMsg; if (pingCallback != null) { conn.SendPacket(new PingPacket() { Payload = pingId, }); sw.Restart(); } ar.Set(); } else if (args.Packet is PingPacket pong) { pingResult = sw.ElapsedMilliseconds; if (pong.Payload == pingId) { waitingOnPing = false; pingCallback?.Invoke(new ServerPingResponse(true, sw.ElapsedMilliseconds)); } else { waitingOnPing = false; pingCallback?.Invoke(new ServerPingResponse(true, sw.ElapsedMilliseconds)); } ar.Set(); } }; bool connectionClosed = false; conn.OnConnectionClosed += (sender, args) => { connectionClosed = true; ar.Set(); }; conn.Initialize(); conn.SendPacket(new HandshakePacket() { NextState = ConnectionState.Status, ServerAddress = connectionDetails.Hostname, ServerPort = (ushort)connectionDetails.EndPoint.Port, ProtocolVersion = JavaProtocol.ProtocolVersion }); conn.ConnectionState = ConnectionState.Status; conn.SendPacket(new RequestPacket()); if (await WaitHandleHelpers.FromWaitHandle(ar, TimeSpan.FromMilliseconds(10000)) && !connectionClosed && jsonResponse != null) { long timeElapsed = sw.ElapsedMilliseconds; // Log.Debug($"Server json: " + jsonResponse); var query = ServerQuery.FromJson(jsonResponse); if (query.Version.Protocol == JavaProtocol.ProtocolVersion) { query.Version.Compatibility = CompatibilityResult.Compatible; } else if (query.Version.Protocol < JavaProtocol.ProtocolVersion) { query.Version.Compatibility = CompatibilityResult.OutdatedServer; } else if (query.Version.Protocol > JavaProtocol.ProtocolVersion) { query.Version.Compatibility = CompatibilityResult.OutdatedClient; } var r = new ServerQueryStatus() { Delay = timeElapsed, Success = true, WaitingOnPing = pingCallback != null && waitingOnPing, EndPoint = endPoint, Address = connectionDetails.Hostname, Port = (ushort)connectionDetails.EndPoint.Port, Query = query }; statusCallBack?.Invoke(new ServerQueryResponse(true, r)); if (waitingOnPing && pingCallback != null) { await WaitHandleHelpers.FromWaitHandle(ar, TimeSpan.FromSeconds(1000)); } } else { statusCallBack?.Invoke(new ServerQueryResponse(false, "multiplayer.status.cannot_connect", new ServerQueryStatus() { EndPoint = endPoint, Delay = sw.ElapsedMilliseconds, Success = false, /* Motd = motd.MOTD, * ProtocolVersion = motd.ProtocolVersion, * MaxNumberOfPlayers = motd.MaxPlayers, * Version = motd.ClientVersion, * NumberOfPlayers = motd.Players,*/ Address = connectionDetails.Hostname, Port = (ushort)connectionDetails.EndPoint.Port, WaitingOnPing = false })); } } } } } catch (Exception ex) { if (sw.IsRunning) { sw.Stop(); } Log.Error($"Could not get server query result! Exception: {ex.ToString()}"); string msg = ex.Message; if (ex is SocketException) { msg = $"multiplayer.status.cannot_connect"; } statusCallBack?.Invoke(new ServerQueryResponse(false, msg, new ServerQueryStatus() { Delay = sw.ElapsedMilliseconds, Success = false, EndPoint = endPoint, Address = connectionDetails.Hostname, Port = (ushort)connectionDetails.EndPoint.Port })); } finally { //conn?.Stop(); //conn?.Dispose(); //conn?.Dispose(); // client?.Close(); } }
public async Task QueryServerAsync(ServerConnectionDetails connectionDetails, PingServerDelegate pingCallback, ServerStatusDelegate statusCallBack) { await QueryJavaServerAsync(connectionDetails, pingCallback, statusCallBack); }
/// <inheritdoc /> public async Task QueryServerAsync(ServerConnectionDetails connectionDetails, PingServerDelegate pingCallback, ServerStatusDelegate statusCallBack) { ManualResetEventSlim ar = new ManualResetEventSlim(false); Stopwatch sw = new Stopwatch(); long pingTime = 0; BedrockClient client = null; try { IPEndPoint serverEndpoint = new IPEndPoint( connectionDetails.EndPoint.Address, (int)connectionDetails.EndPoint.Port); client = new BedrockClient( Game, serverEndpoint, new PlayerProfile( string.Empty, $"Pinger{serverEndpoint.ToString()}", $"Pinger{serverEndpoint.ToString()}", null, null, null), null) { //IgnoreUnConnectedPong = true }; client.Connection.AutoConnect = false; BedrockMotd motd = new BedrockMotd(string.Empty); client.OnMotdReceivedHandler += (sender, m) => { motd = m; pingTime = m.Latency; ar.Set(); pingCallback.Invoke(new ServerPingResponse(true, pingTime)); }; client.Start(ar); sw.Restart(); if (await WaitHandleHelpers.FromWaitHandle(ar.WaitHandle, TimeSpan.FromMilliseconds(2500))) { client.Close(); var compatability = CompatibilityResult.Unknown; if (motd.ProtocolVersion == McpeProtocolInfo.ProtocolVersion) { compatability = CompatibilityResult.Compatible; } statusCallBack?.Invoke( new ServerQueryResponse( true, new ServerQueryStatus() { EndPoint = motd.ServerEndpoint, Delay = pingTime, Success = true, Address = connectionDetails.Hostname, Port = (ushort)connectionDetails.EndPoint.Port, WaitingOnPing = false, Query = new ServerQuery() { Players = new Players() { Max = motd.MaxPlayers, Online = motd.Players }, Version = new API.Services.Version() { Protocol = motd.ProtocolVersion, Name = motd.ClientVersion, Compatibility = compatability }, Description = new Description() { Text = motd.MOTD }, Modinfo = null, Favicon = null } })); } else { statusCallBack?.Invoke( new ServerQueryResponse( false, "multiplayer.status.cannot_connect", new ServerQueryStatus() { EndPoint = serverEndpoint, Delay = sw.ElapsedMilliseconds, Success = false, Address = connectionDetails.Hostname, Port = (ushort)connectionDetails.EndPoint.Port, WaitingOnPing = false })); } } catch (Exception e) { Log.Error($"Could not get bedrock query: {e.ToString()}"); statusCallBack?.Invoke( new ServerQueryResponse(false, "Failed to connect...", new ServerQueryStatus() { Success = false })); } finally { client?.Close(); client?.Dispose(); } }