public bool Connect(string ip, int port, int delay, int cycles) { _ip = ip; _port = port; var connectResult = conn.ConnectAsync(_port, _ip, delay, cycles); if (connectResult.Result.Failure) { return(false); } _clientSocket = connectResult.Result.Value; tcpClient = new Client(_clientSocket, 1024, dataGetter); tcpClient.Receiver.ClientDataReceived += Receiver_ClientDataReceived; return(_clientSocket.Connected); }
public async Task <Result> SendAndConnectMessageAsync() { var serverPort = ((_port > 0) ? _port : CliServDefaults.DfltPort); string address = _ip; bool greetingSent = false; var connectResult = await conn.ConnectAsync(_port, _ip, 3000, 10); Console.WriteLine("Connecting to IP: {0}, Port: {1}", (!String.IsNullOrEmpty(address) ? address : "localhost"), serverPort); // Connection failure. Just return if (connectResult.Failure) { return(Result.Fail("There was an error connecting to the server.")); } _clientSocket = connectResult.Value; // Register a name for this client if (reset) { Console.WriteLine("<<< Reset detected. Hit ENTER before typing name. >>>"); reset = false; } Console.Write("Enter a Name: "); name = Console.ReadLine(); MessageData sendData = new MessageData(); sendData.name = name; MessageData eventData = null; while (!done) { // Reset connection vars. ResetEvent?.Invoke(false); sndResult = null; rcvResult = null; EnableRead(); try { eventData = UserSendEvent(ref greetingSent); if (eventData != null) { string message = (string)eventData.message; if (String.Compare(message, "exit", true) == 0) { done = true; message = "I'm exiting. Goodbye."; } sendData.message = message; sendData.id = eventData.id; var sendResult = await SendMessageAsync(sendData); if (sendResult.Failure) { return(sendResult); } } else { Console.WriteLine("Empty Message."); } } catch (InvalidOperationException) { Console.WriteLine("Input Cancelled."); } catch (OperationCanceledException) { Console.WriteLine("Input Cancelled."); } } // Set vars here for possible reconnect greetingSent = false; // Report successful return(Result.Ok()); }