I've been going crazy over this particular problem. What I am trying to do is make an HTTPS Call to the server after a user clicks the sign up button. The server must add the new user to the database and send me back a user key. I've tested the httpClient class with a local LAMP server without HTTPS. Works fine. When I try to connect to the productions server with SSL I GET A 404 - NOT FOUND. I have double checked that the URL, CONTENT and Authorization is formatted well. As a matter of facted I codded it in a console application and it connects to the production server every time. It just does not work with the windows phone emulator or my windows phone. Been monitoring everything with fiddler. I've also used the WebClient class to no avail. PLEASE HELP!
Code below!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JailBird_ProtoType.Models
{
// CLASS USED TO FORMAT OBJECT TO JSON STRING
public class TestUser
{
public string Name { set; get; }
public string Email { get; set; }
public string Password { set; get; }
public List<string> Roles = new List<string>();
}
}
using Microsoft.Phone.Net.NetworkInformation;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace JailBird_ProtoType.Services
{
public class UploadJsonService
{
private BackgroundWorker bw = new BackgroundWorker();
public BackgroundWorker GetBackGroundWorker { get { return bw; } }
private HttpClient client = new HttpClient();
private HttpClient GetClient { get { return client; } }
private HttpResponseMessage response;
public HttpResponseMessage GetResponseMessage { get { return response; } }
// SET THE UPLOAD URL
private string uploadURL;
public string SetUploadURL { set { uploadURL = value; } }
// SET THE STRING DATA UPLOAD VALUE
private string jsonValue ="";
public string SetJsonValue { set { jsonValue = value; } }
private HttpContent httpContent;
public HttpContent GetHttpContent { get { return httpContent; } set { httpContent = value; } }
// SET THE METHOD TYPE UPLOAD VALUE
private string Method = "POST";
public string SetMethod { set { Method = value; } }
// CONSRUCTOR
public UploadJsonService()
{
SetUpClass();
}
public UploadJsonService(string url, string data)
{
SetUploadURL = url;
SetJsonValue = data;
SetUpClass();
}
public UploadJsonService(string url, string method, string data)
{
SetUploadURL = url;
SetJsonValue = data;
SetMethod = method;
SetUpClass();
}
private void SetUpClass()
{
bw.DoWork += new DoWorkEventHandler(DoWork);
httpContent = new StringContent(jsonValue);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
}
public void StartUpload()
{
try
{
bw.RunWorkerAsync(); // RUN BACKGROUND WORKER
}
catch (Exception) { }
}
public void CancelUpload()
{
// CANCEL ALL WORKER TASKS
try
{
client.Dispose();
httpContent = null;
bw.CancelAsync();
}
catch (Exception) { }
}
public void SetHeader(string header, string value)
{
client.DefaultRequestHeaders.Add(header, value);
}
private async void DoWork(object sender, DoWorkEventArgs e)
{
if (isConnectionReady())
{
try
{
if (Method.ToLower() == "post")
{
response = await client.PostAsync(new Uri(uploadURL, UriKind.Absolute), httpContent);
}
else if (Method.ToLower() == "push")
{
response = await client.PutAsync(new Uri(uploadURL, UriKind.Absolute), httpContent);
}
// DO SOMETHING WITH THE RESPONSE HERE
}
catch (Exception)
{
//UPDATE THE UI THREAD HERE
Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
{
MessageBox.Show("Upload did not complete successfully. Check your connection settings.", "Something Went Wrong", MessageBoxButton.OK);
}));
}
}
else
{
//UPDATE THE UI THREAD HERE
Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
{
MessageBox.Show("Check your phone's connection settings", "No Network Connection", MessageBoxButton.OK);
}));
}
}
// METHOD USED TO CHECK FOR NETWORK CONNECTION
private bool isConnectionReady()
{
bool internet = false;
//Check if network is available
if (DeviceNetworkInformation.IsNetworkAvailable)
{
internet = true;
}
return internet;
}
}
}
// THIS METHOD IS ACTIVATED IN THE SIGNUP BUTTON EVENT HANDLER
public void SendToServer()
{
// string user = JsonConvert.SerializeObject(App.ViewModel.Users[0]); // Get User Information
TestUser me = new TestUser() { Name="testName2", Password="password",Email="mail#mail.com"};
me.Roles.Add("User");
string meString = JsonConvert.SerializeObject(me);
if (App.Settings.Contains("FirstSignUp"))
{
service = new UploadJsonService(ConnectToServer.USER_UPLOAD_URL,"PUT",meString);
}
else
{
service = new UploadJsonService(ConnectToServer.USER_UPLOAD_URL,"POST",meString);
}
service.SetHeader("Authorization", "Basic " + ConnectToServer.Get64BitEncoding(ConnectToServer.SERVER_ADMIN));
service.GetBackGroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(RunWorkerServiceCompleted);
service.StartUpload();
}
private void RunWorkerServiceCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// DO WORK AFTER THE BACKGROUND WORKER COMPLETES
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JailBird_ProtoType.Services
{
// THIS CLASS HOLDS ALL SERVER INFORMATION
// FOR QUICK ACCESS AND CHANGE
public static class ConnectToServer
{
public static string SERVER_ADMIN = "admin:password";
// UPLOAD USER TO DB
public static string USER_UPLOAD_URL = "https://xx.xxx.xx.xxx:443/api/users";
public static string Get64BitEncoding(string key) {
byte[] convert = System.Text.Encoding.UTF8.GetBytes(key);
return System.Convert.ToBase64String(convert);
}
}
}
omitted real server address for security reasons.
Sorry my code is not structured Right. Been changing it around to get this to work.
Related
I am new to Xamarin. I want to confirm if the database is created and if the data is being inserted to the SQLite database. Thank you for the help
Questions:
1. How to check if database exist/created or not?
2. How to check if the user are inserted successfully or it failed?
3. Where do these file go in my phone?
Below is my code:
App.xaml.cs
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using TBSMobileApplication.Views;
using TBSMobileApplication.Data;
[assembly: XamlCompilation (XamlCompilationOptions.Compile)]
namespace TBSMobileApplication
{
public partial class App : Application
{
static TokenDatabaseController tokenDatabase;
static UserDatabaseController userDatabase;
public App ()
{
InitializeComponent();
MainPage = new LoginPage();
}
protected override void OnStart ()
{
// Handle when your app starts
}
protected override void OnSleep ()
{
// Handle when your app sleeps
}
protected override void OnResume ()
{
// Handle when your app resumes
}
public static UserDatabaseController UserDatabase
{
get
{
if(userDatabase == null)
{
userDatabase = new UserDatabaseController();
}
return userDatabase;
}
}
public static TokenDatabaseController TokenDatabase
{
get
{
if (tokenDatabase == null)
{
tokenDatabase = new TokenDatabaseController();
}
return tokenDatabase;
}
}
}
}
LoginPage.xaml.cs (Basically this is my Code behind in my login page)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TBSMobileApplication.Models;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace TBSMobileApplication.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LoginPage : ContentPage
{
public LoginPage ()
{
InitializeComponent ();
}
void LoginProcedure(object sender, EventArgs e)
{
User user = new User(entUser.Text, entPassword.Text);
if (user.CheckInformation())
{
//DisplayAlert("Login Message", "Login Success", "Ok");
try
{
App.UserDatabase.SaveUser(user);
DisplayAlert("Database Message", "User Saved", "Ok");
}
catch(Exception ex)
{
DisplayAlert("Message", ex.Message, "Ok");
}
}
else
{
DisplayAlert("Login Message", "Login Failed", "Ok");
}
}
}
}
ISQLite.cs (This is where you get the connection to the database)
using SQLite;
using System;
using System.Collections.Generic;
using System.Text;
namespace TBSMobileApplication.Data
{
public interface ISQLite
{
SQLiteConnection GetConnection();
}
}
User.cs (This is where the parameters of the User table)
using SQLite;
using System;
using System.Collections.Generic;
using System.Text;
namespace TBSMobileApplication.Models
{
public class User
{
[PrimaryKey, AutoIncrement]
public int ContactID { get; set; }
[Unique]
public string UserID { get; set; }
public string UserPassword { get; set; }
public User() { }
public User(string Username, string Password)
{
this.UserID = Username;
this.UserPassword = Password;
}
public bool CheckInformation()
{
if(!this.UserID.Equals("") || !this.UserPassword.Equals(""))
{
return true;
}
else
{
return false;
}
}
}
}
SQLite_Android.cs (This is where I created the database)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using TBSMobileApplication.Data;
using TBSMobileApplication.Droid.Data;
using Xamarin.Forms;
[assembly: Dependency(typeof(SQLite_Android))]
namespace TBSMobileApplication.Droid.Data
{
public class SQLite_Android : ISQLite
{
public SQLite_Android() { }
public SQLite.SQLiteConnection GetConnection()
{
var DBFileName = "backend.db3";
string DocumentPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = Path.Combine(DocumentPath, DBFileName);
var conn = new SQLite.SQLiteConnection(path);
return conn;
}
}
}
UserDatabaseController.cs (This is where I control User table like adding, deleting or getting data from User table)
using SQLite;
using System;
using System.Collections.Generic;
using System.Text;
using TBSMobileApplication.Models;
using Xamarin.Forms;
namespace TBSMobileApplication.Data
{
public class UserDatabaseController
{
static object locker = new object();
SQLiteConnection database;
public UserDatabaseController()
{
database = DependencyService.Get<ISQLite>().GetConnection();
database.CreateTable<User>();
}
public User GetUser()
{
lock (locker)
{
if(database.Table<User>().Count() == 0)
{
return null;
}
else
{
return database.Table<User>().First();
}
}
}
public int SaveUser(User user)
{
lock (locker)
{
if (user.ContactID != 0)
{
database.Update(user);
return user.ContactID;
}
else
{
return database.Insert(user);
}
}
}
public int DeleteUser(int contactid)
{
lock (locker)
{
return database.Delete<User>(contactid);
}
}
}
}
Starting with part 3 of your question - where is the database file? - it's here:
var DBFileName = "backend.db3";
string DocumentPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
That equates to:
/data/data/[your.package.name]/files/backend.db3
For the first part of your question, to check whether the database has been created, just check whether the file exists:
public static bool DBExists()
{
string DocumentPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = Path.Combine(DocumentPath, "backend.db3");
return File.Exists(path);
}
Accessing the file there is somewhere between difficult and impossible without a rooted device. You're not supposed to be able to access files there - only your app can access them. It's a safety measure.
Your application doesn't have any trouble accessing the database file, though, so you can implement a method in your application to copy it somewhere more accessible (e.g. the Downloads directory).
Put this in your Android project:
public static void CopyDBToDownloadsDirectory()
{
var path = System.IO.Path.Combine(
Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath,
"backend.db3");
string DocumentPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var safePath = Path.Combine(DocumentPath, "backend.db3");
File.Copy(safePath, path, true);
}
Call it to create a copy of the database you can readily access on the phone's built-in file browser.
So, for part 2 of the question, whether a transaction succeeded or failed, you can either run queries against your database in code to check, or open a copy of the database file in a GUI and browse the data.
First time posting so sorry for the formatting if it is incorrect.
I am having trouble filtering an observable collection based upon a date given. The application will have a calendar where the user can click a date and below will appear the appointments for that date.
There are two classes, one being the dataManager which will get the data from Azure, and the appointment page itself.
Here is the appointmentPage Class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using XamForms.Controls;
namespace TodoAzure
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class AppointmentPage : ContentPage
{
TodoItemManager manager;
CalendarVM vm = new CalendarVM();
public AppointmentPage()
{
InitializeComponent();
manager = TodoItemManager.DefaultManager;
calendar.DateClicked += (sender, e) =>
{
System.Diagnostics.Debug.WriteLine(calendar.SelectedDates);
};
calendar.SetBinding(Calendar.DateCommandProperty, nameof(vm.DateChosen));
calendar.SetBinding(Calendar.SelectedDateProperty, nameof(vm.DateSelected));
calendar.BindingContext = vm;
}
protected override async void OnAppearing()
{
base.OnAppearing();
// Set syncItems to true in order to synchronize the data on startup when running in offline mode
await RefreshItems(true, syncItems: false);
}
//PULL TO REFRESH
public async void OnRefresh(object sender, EventArgs e)
{
var list = (ListView)sender;
Exception error = null;
try
{
await RefreshItems(false, true);
}
catch (Exception ex)
{
error = ex;
}
finally
{
list.EndRefresh();
}
if (error != null)
{
await DisplayAlert("Refresh Error", "Couldn't refresh data (" + error.Message + ")", "OK");
}
}
public async void OnSyncItems(object sender, EventArgs e)
{
await RefreshItems(true, true);
}
private async Task RefreshItems(bool showActivityIndicator, bool syncItems)
{
using (var scope = new ActivityIndicatorScope(syncIndicator, showActivityIndicator))
{
appointmentPage.ItemsSource = await manager.GetAppointmentItemsAsync(syncItems);
}
}
private class ActivityIndicatorScope : IDisposable
{
private bool showIndicator;
private ActivityIndicator indicator;
private Task indicatorDelay;
public ActivityIndicatorScope(ActivityIndicator indicator, bool showIndicator)
{
this.indicator = indicator;
this.showIndicator = showIndicator;
if (showIndicator)
{
indicatorDelay = Task.Delay(2000);
SetIndicatorActivity(true);
}
else
{
indicatorDelay = Task.FromResult(0);
}
}
private void SetIndicatorActivity(bool isActive)
{
this.indicator.IsVisible = isActive;
this.indicator.IsRunning = isActive;
}
public void Dispose()
{
if (showIndicator)
{
indicatorDelay.ContinueWith(t => SetIndicatorActivity(false), TaskScheduler.FromCurrentSynchronizationContext());
}
}
}
}
And here is the data manger class:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.MobileServices;
using Microsoft.WindowsAzure.MobileServices.Sync;
#if OFFLINE_SYNC_ENABLED
using Microsoft.WindowsAzure.MobileServices.SQLiteStore;
using Microsoft.WindowsAzure.MobileServices.Sync;
#endif
namespace TodoAzure
{
public partial class TodoItemManager
{
static TodoItemManager defaultInstance = new TodoItemManager ();
MobileServiceClient client;
IMobileServiceTable<TodoItem> todoTable;
IMobileServiceTable<AppointmentItem> appointmentTable;
private TodoItemManager ()
{
this.client = new MobileServiceClient (
Constants.ApplicationURL);
this.todoTable = client.GetTable<TodoItem> ();
this.appointmentTable = client.GetTable<AppointmentItem>();
}
public static TodoItemManager DefaultManager
{
get { return defaultInstance; }
private set { defaultInstance = value; }
}
public MobileServiceClient CurrentClient
{
get { return client; }
}
public bool IsOfflineEnabled
{
get { return appointmentTable is Microsoft.WindowsAzure.MobileServices.Sync.IMobileServiceSyncTable<AppointmentItem>; }
}
// INSERT AND UPDATE METHODS
public async Task SaveTaskAsync (TodoItem item)
{
if (item.Id == null)
await todoTable.InsertAsync (item);
else
await todoTable.UpdateAsync (item);
}
public async Task SaveTaskAsync(AppointmentItem appointment)
{
if (appointment.Id == null)
await appointmentTable.InsertAsync(appointment);
else
await appointmentTable.UpdateAsync(appointment);
}
public async Task<ObservableCollection<AppointmentItem>> GetAppointmentItemsAsync(bool syncItems = false)
{
try
{
IEnumerable<AppointmentItem> items = await appointmentTable
.ToEnumerableAsync();
return new ObservableCollection<AppointmentItem>(items);
}
catch (MobileServiceInvalidOperationException msioe)
{
Debug.WriteLine(#"Invalid sync operation: {0}", msioe.Message);
}
catch (Exception e)
{
Debug.WriteLine(#"Sync error: {0}", e.Message);
}
return null;
}
}
Any Help will be greatly appreciated.
to filter an IEnumerable by Date, try this
// items is ObservableCollection<AppointmentItem>
var filtered = items.Where(x => x.Date == SelectedDate);
I am making a trade offer bot in C# using SteamKit2, and most of the time it is successfully connecting to steam. But some of the time it just freezes when I have it output "Connecting to Steam..." right before client.connect(); is called. It happens often enough that it needs to be fixed, but I don't know what the problem is. Here is my code (a lot was taken from a SteamKit2 tutorial):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SteamKit2;
namespace ATO
{
class OfferSender
{
string username;
string password;
SteamClient client;
CallbackManager manager;
SteamUser user;
bool isRunning = false;
public OfferSender()
{
}
public void login()
{
Console.Write("Please enter your username: ");
username = Console.ReadLine();
Console.Write("Please enter your password: ");
password = Console.ReadLine();
client = new SteamClient();
manager = new CallbackManager(client);
user = client.GetHandler<SteamUser>();
new Callback<SteamClient.ConnectedCallback>(OnConnected, manager);
new Callback<SteamUser.LoggedOnCallback>(OnLoggedOn, manager);
isRunning = true;
Console.WriteLine("\nConnecting to Steam...\n");
client.Connect();
while(isRunning)
{
manager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
}
Console.ReadKey();
}
public void OnConnected(SteamClient.ConnectedCallback callback)
{
if (callback.Result != EResult.OK)
{
Console.WriteLine("Error connecting to Steam: {0}", callback.Result);
isRunning = false;
return;
}
Console.WriteLine("Connected to steam.\nLogging in {0}...\n", username);
user.LogOn(new SteamUser.LogOnDetails {
Username = username,
Password = password
});
}
public void OnLoggedOn(SteamUser.LoggedOnCallback callback)
{
if (callback.Result == EResult.AccountLogonDenied)
{
Console.WriteLine("This account is SteamGuard protected.");
return;
}
if(callback.Result != EResult.OK)
{
Console.WriteLine("Unable to log in to steam {0}\n", callback.Result);
isRunning = false;
return;
}
Console.WriteLine("successfully logged in!");
Environment.Exit(0);
}
}
}
How do I fix this?
Well, after banging my head against the keyboard I finaly spoted that Steam would be requesting an AuthCode like any other new computer or browser login.
And that's it...
The code below handles either AuthCode and TwoFactor authentications:
https://github.com/SteamRE/SteamKit/tree/master/Samples/5.SteamGuard
You need to handle some more callbacks.
Here is how do I log into steam with my bot.
SteamBot.cs
using System;
using SteamKit2;
using System.Collections.Generic;
namespace StackOverflow
{
class SteamBot
{
CallbackManager m_CallbackManager;
SteamUser m_SteamUser;
SteamClient m_SteamClient;
SteamFriends m_SteamFriends;
SteamID m_SteamID;
Dictionary<string, string> m_Config;
public bool isLoggedOn { get; private set; }
public bool isReady { get; private set; }
public SteamBot(string pUsername, string pPassword)
{
isLoggedOn = false;
isReady = false;
m_Config = new Dictionary<string, string>();
m_Config.Add("username", pUsername);
m_Config.Add("password", pPassword);
Init();
RegisterCallbacks();
Connect();
}
private void Init()
{
m_SteamClient = new SteamClient();
m_CallbackManager = new CallbackManager(m_SteamClient);
m_SteamFriends = m_SteamClient.GetHandler<SteamFriends>();
m_SteamUser = m_SteamClient.GetHandler<SteamUser>();
}
private void RegisterCallbacks()
{
m_CallbackManager.Subscribe<SteamClient.ConnectedCallback>(OnConnected);
m_CallbackManager.Subscribe<SteamClient.DisconnectedCallback>(OnDisconnected);
m_CallbackManager.Subscribe<SteamUser.LoggedOnCallback>(OnLoggedOn);
m_CallbackManager.Subscribe<SteamUser.LoggedOffCallback>(OnLoggedOff);
m_CallbackManager.Subscribe<SteamUser.AccountInfoCallback>(OnAccountInfo);
}
public void WaitForCallbacks()
{
m_CallbackManager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
}
public string GetConfigValue(string pKey)
{
return m_Config[pKey];
}
public void Connect()
{
m_SteamClient.Connect();
isReady = true;
}
void OnConnected(SteamClient.ConnectedCallback pData)
{
Console.WriteLine("Connected to Steam! Logging in '{0}'...", GetConfigValue("username"));
SteamUser.LogOnDetails details = new SteamUser.LogOnDetails
{
Username = GetConfigValue("username"),
Password = GetConfigValue("password"),
};
m_SteamUser.LogOn(details);
m_SteamID = m_SteamClient.SteamID;
}
void OnDisconnected(SteamClient.DisconnectedCallback pData)
{
m_SteamClient.Disconnect();
}
void OnLoggedOff(SteamUser.LoggedOffCallback pData)
{
isLoggedOn = false;
}
void OnLoggedOn(SteamUser.LoggedOnCallback pData)
{
if (pData.Result != EResult.OK)
{
Console.WriteLine("Unable to login to Steam: {0} / {1}", pData.Result, pData.ExtendedResult);
return;
}
Console.WriteLine("Successfully logged on!");
isLoggedOn = true;
}
void OnAccountInfo(SteamUser.AccountInfoCallback pData)
{
//Announce to all friends that we are online
m_SteamFriends.SetPersonaState(EPersonaState.Online);
}
}
}
Program.cs
namespace StackOverflow
{
class Program
{
static void Main(string[] args)
{
SteamBot bot = new SteamBot("botname", "password");
while(true)
{
if(bot.isReady)
{
bot.WaitForCallbacks();
}
}
}
}
}
You need to add Steam Directory.Initialize().Wait(); before you attempt to connect so SteamKit can update its internal list of servers, as explained here.
I wrote this C# code to have namedPipeServer and NamedPipeClient, with Asynchronous read and write settings, connect to each other. Both code runs perfectly on visual studio 2010 that I am using with read and write working well without any application freeze during runtime.
But I want the client side running in unity3d. The problem I encounter is in client side code implemented in Unity3D. When I use Write_to_Server_Async(string message), read in the server side is not invoked and is only invoked when I quit Unity3d (I have end its process typically). I can tell something wrong with Unity3D, because the exact code works perfectly in visual studio, so I know my code is implemented the right way. I have heard about how unity3d does not really use real threads unless user manually creates one but even that has not solved the problem. My speculation is Unity3D developers might have created their version of .NET library 3.5 (sounds bizzare (does explain why they still haven't adopted 4.5)) or somehow they must have structured their library in a way that by default functions like NamedPipeClientStream.BeginWrite cannot create its own real thread. But then again I am not sure if its the problem with threads.
At the moment, I would like anyone to come up with good explanation.
Make sure to replace Debug.WriteLine to UnityEngine.Debug.Log in unity3d.
Below is Client Main method code and class
class PipeClient
{
private static Asynchronus_NamedPipe_Client client;
static void Main(string[] args)
{
client = new Asynchronus_NamedPipe_Client("mypipe7055");
while (client.Is_connected_to_server()) {
if (Console.ReadKey().Key == ConsoleKey.T)
{
client.Write_to_Server_Async("NEX CLIENT");
}
}
}
}
Asynchronus_NamedPipe_Client class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System;
using System.IO;
using System.IO.Pipes;
using System.Text;
using System.Security.Principal;
using System.Diagnostics;
using System.Threading;
namespace NamedPipes_CLIENT
{
public class Asynchronus_NamedPipe_Client
{
public readonly string pipe_address;
private System.IO.Pipes.NamedPipeClientStream clientStream;
public bool filter_message = true;
private string Server_Message = null;
public event ASYNC_pipe_status_callback ASYNC_external_Write_Completed;
public event ASYNC_pipe_status_callback ASYNC_external_Read_Completed;
public delegate void ASYNC_pipe_status_callback(string message);
private byte[] read_buffer = new byte[1024];
private byte[] write_buffer = new byte[1024];
private IAsyncResult read_result;
private IAsyncResult write_result;
private int read_id = 1;
public Asynchronus_NamedPipe_Client(string pipe_address)
{
try
{
this.pipe_address = pipe_address;
// if(clientStream.IsConnected){UnityEngine.Debug.Log("Server Already Running");}else{}
clientStream = new NamedPipeClientStream(".", this.pipe_address, PipeDirection.InOut, PipeOptions.Asynchronous);
clientStream.Connect(1);
if (clientStream.IsConnected)
{
Console.WriteLine("Connected to Server");
Read_from_Server_Async();
}
else { Console.WriteLine("Could NOT connect to Server"); }
}
catch (Exception oEX) { Console.WriteLine("Application Pipe Error: "+oEX.Message); }
}
public void Write_to_Server_Async(string message)
{
if (clientStream != null)
{
if (clientStream.CanWrite && clientStream.IsConnected)
{
clientStream.WaitForPipeDrain();
ASCIIEncoding.ASCII.GetBytes(message).CopyTo(write_buffer,0);
clientStream.BeginWrite(write_buffer, 0, write_buffer.Length, new AsyncCallback(Async_Write_Completed), 1);
} else { close_pipe(); }
}
}
public void Read_from_Server_Async()
{
if (clientStream.CanRead && clientStream.IsConnected)
{
clientStream.BeginRead(read_buffer, 0, read_buffer.Length, new AsyncCallback(Async_Read_Completed), 2);
} else { close_pipe(); }
}
private void Async_Write_Completed(IAsyncResult result)
{
clientStream.EndWrite(result);
Debug.WriteLine("Written To Server => " + ASCIIEncoding.ASCII.GetString(write_buffer));
// close_pipe();
}
private void Async_Read_Completed(IAsyncResult result)
{
clientStream.EndRead(result);
Server_Message = ASCIIEncoding.ASCII.GetString(read_buffer);
this.Server_Message.Trim();
Console.WriteLine("Received from Server => " + Server_Message);
Debug.WriteLine("Received from Server => " + Server_Message);
if (clientStream.CanRead && clientStream.IsConnected)
{
Read_from_Server_Async();
}
else { close_pipe(); }
}
public Boolean Is_connected_to_server() {
return clientStream.IsConnected;
}
public void close_pipe()
{
if (clientStream != null)
{
if (clientStream.IsConnected)
{
clientStream.Close();
clientStream.Dispose();
Debug.WriteLine(" Pipe Closed");
}
}
}
}
}
Server side Main method implementation
static void Main(string[] args)
{
Asynchronus_NamedPipe_Server Async_server = new Asynchronus_NamedPipe_Server("mypipe7055");
while (true)
{
do
{
Async_server.Write_to_Client_Async("yeye");
Console.WriteLine("escape key");
} while (Console.ReadKey(true).Key != ConsoleKey.Escape);
}
}
Server Side Class -----
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Pipes;
using System.IO;
using System.ComponentModel;
using System.Diagnostics;
namespace Application_Pipe
{
public class Asynchronus_NamedPipe_Server
{
public readonly string pipe_address;
private System.IO.Pipes.NamedPipeServerStream namedPipeServerStream;
private string Server_Message;
public delegate void ASYNC_pipe_status_callback(string message);
private byte[] read_buffer = new byte[1024];
private byte[] write_buffer = new byte[1024];
public Asynchronus_NamedPipe_Server(string pipe_address)
{
try
{
this.pipe_address = pipe_address;
namedPipeServerStream = new NamedPipeServerStream(this.pipe_address,
PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous); //new NamedPipeServerStream(pipe_address);
Console.WriteLine("Connecting to Client...");
namedPipeServerStream.WaitForConnection();
Console.WriteLine("Connected to Client");
Read_from_Client_Async();
}
catch (Exception oEX) { Console.WriteLine(oEX.Message); }
}
public void Write_to_Client_Async(string message)
{
if (namedPipeServerStream != null)
{
if (namedPipeServerStream.CanWrite && namedPipeServerStream.IsConnected)
{
namedPipeServerStream.WaitForPipeDrain();
ASCIIEncoding.ASCII.GetBytes(message).CopyTo(write_buffer,0);
namedPipeServerStream.BeginWrite(write_buffer, 0, write_buffer.Length, new AsyncCallback(Async_Write_Completed), 2);
}
else { close_pipe(); }
}
}
public void Read_from_Client_Async()
{
if (namedPipeServerStream != null)
{
if (namedPipeServerStream.CanRead && namedPipeServerStream.IsConnected)
{
namedPipeServerStream.BeginRead(read_buffer, 0, read_buffer.Length, new AsyncCallback(Async_Read_Completed), 1);
} else { close_pipe(); }
}
}
private void Async_Read_Completed(IAsyncResult result)
{
namedPipeServerStream.EndRead(result);
this.Server_Message = ASCIIEncoding.ASCII.GetString(read_buffer);
this.Server_Message.Trim();
Debug.WriteLine("Received from Client => " + this.Server_Message+" <=REnd");
Read_from_Client_Async();
}
private void Async_Write_Completed(IAsyncResult result)
{
namedPipeServerStream.EndWrite(result);
Debug.WriteLine("Written To Client => " + ASCIIEncoding.ASCII.GetString(write_buffer));
}
public Boolean Is_connected_to_server()
{
return this.namedPipeServerStream.IsConnected;
}
public void close_pipe()
{
if(namedPipeServerStream.IsConnected){
namedPipeServerStream.Disconnect();
}
namedPipeServerStream.Close();
namedPipeServerStream.Dispose();
Debug.WriteLine(" Pipe Closed");
}
} //------class End
}
I have some big trouble with serial requests.
Description from what i want:
establish a serial connection, send serial requests to 6 temperature
sensors one by one (this is done every 0,5 second in a loop)
the question and answer-destination is stored in a List array
every request is started in a separate thread so the gui does not bug
while the programme waits for the sensor-hardware to answer
My problem:
The connection and the request is working fine, but if I am browsing data at the local hard drive the answer from the sensor-unit gets destroyed (negative algebraic sign or value from other sensor or simply wrong value).
How does this happen or how can I solve this?
Where I guess the problem might be:
In the private void ReceiveThread() of class SerialCommunication
Here is my code:
Class CommunicationArray:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hardwarecommunication
{
public class CommunicationArray
{
public string request { get; set; }
public object myObject { get; set; }
public string objectType { get; set; }
}
}
Class SerialCommunication
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO;
using System.IO.Ports;
using System.Windows.Forms;
namespace Hardwarecommunication
{
class SerialCommunication
{
Thread t2;
Thread t;
private SerialPort serialPort = new SerialPort("COM2", 115200, Parity.Even, 8, StopBits.One);
string serialAnswer = "";
private volatile bool _shouldStop;
private int counter;
List<CommunicationArray> ar = new List<CommunicationArray>();
object[] o = new object[3];
public void addListener(string request, object myObject, string objectType)
{
CommunicationArray sa = new CommunicationArray();
sa.request = request;
sa.myObject = myObject;
sa.objectType = objectType;
ar.Add(sa);
}
public void startListen()
{
t2 = new Thread(() => writeSerialPortThread());
t2.Start();
}
public void startSerialPort2()
{
try
{
serialPort.Open();
//MessageBox.Show("Connection opend!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
public void stopSerialPort2()
{
try
{
if (serialPort.IsOpen == true)
// Connection closed
serialPort.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void writeSerialPortThread()
{
string request = "";
for (int i = 0; i < ar.Count(); i++)
{
request = ar[i].request;
//request = ((object[])ar[0])[0].ToString();
//if (!t.IsAlive)
//{
try
{
t = new Thread(ReceiveThread);
_shouldStop = false;
//MessageBox.Show("start thread");
t.Start();
serialPort.Write(request);
Thread.Sleep(50);
_shouldStop = true;
t.Join();
}
catch
{
}
Label tmpLabelObject = (Label)ar[i].myObject;
serialAnswer = serialAnswer.Replace("=", "");
if (tmpLabelObject.InvokeRequired)
{
MethodInvoker UpdateLabel = delegate
{
tmpLabelObject.Text = serialAnswer;
};
try
{
tmpLabelObject.Invoke(UpdateLabel);
}
catch
{
}
}
}
}
private void ReceiveThread()
{
//MessageBox.Show("in thread");
while (!_shouldStop)
{
serialAnswer = "";
try
{
//MessageBox.Show("in thread");
serialAnswer = serialPort.ReadTo("\r");
if (serialAnswer != "")
{
}
return;
}
catch (TimeoutException) { }
}
}
}
}
Class Form1 //to establish the connection and to start the Sensor request
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Hardwarecommunication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SerialCommunication serialCommunication1 = new SerialCommunication();
private void Form1_Load(object sender, EventArgs e)
{
//start up serial connection
serialCommunication1.startSerialPort2();
}
private void buttonStart_Click(object sender, EventArgs e)
{
timerRecord.Enabled = true;
if (this.buttonStart.Text == "Start")
this.buttonStart.Text = "Stop";
else
this.buttonStart.Text = "Start";
}
private void timerRecord_Tick(object sender, EventArgs e)
{
if (this.buttonStart.Text == "Stop")
{
this.serialCommunication1.startListen();
}
}
private void buttonFillRequestArray_Click(object sender, EventArgs e)
{
this.serialCommunication1.addListener("$0BR00\r" + "\r", this.labelResult0, "label0"); //request to the hardware
this.serialCommunication1.addListener("$0BR01\r" + "\r", this.labelResult1, "label1");
this.serialCommunication1.addListener("$01R00\r" + "\r", this.labelResult2, "label2");
this.serialCommunication1.addListener("$01R01\r" + "\r", this.labelResult3, "label3");
this.serialCommunication1.addListener("$01R02\r" + "\r", this.labelResult4, "label4");
}
}
}
I woud be happy about any try to fix the problem.
I coud also upload the solution as .zip but you can't test it at all because you do not have the sensor hardware.
Note: serialPort.Write(string) is a non-blocking store into the output buffer.
That means the following won't guarantee you've even finished writing your request before you stop listening for a response:
serialPort.Write(request);
Thread.Sleep(50);
_shouldStop = true;
You could add:
while( serialPort.BytesToWrite > 0 ) Thread.Sleep(1); // force blocking
but it's ill advised.
One thing I'm wondering. There is only a single serial port here. Why do you want many different threads to work with it when you could manage the entire serial port interaction with a single thread? (Or at worse, 1 thread for input 1 thread for output)
To me it makes a lot more sense to store up requests into a queue of some kind and then peel them off one at a time for processing in a single thread. Responses could be similarly queued up or fired as events back to the caller.
EDIT: If you don't mind one read/write cycle at a time you could try:
string response;
lock(serialPort) {
// serialPort.DiscardInBuffer(); // only if garbage in buffer.
serialPort.Write(request);
response = serialPort.ReadTo("\r"); // this call will block till \r is read.
// be sure \r ends response (only 1)
}