Skip to content

Instantly share code, notes, and snippets.

@ToJans
Created January 14, 2011 13:13
Show Gist options
  • Save ToJans/779588 to your computer and use it in GitHub Desktop.
Save ToJans/779588 to your computer and use it in GitHub Desktop.
stukske relevante code
void UpdateWifiAndTime(object par)
{
// Logger.WriteLine(()=>"Begin update wifi and time");
while (true)
{
try {
var store = par as VMStore;
var s = DateTime.Now.ToShortTimeString();
var q = store.Load<UnsentOrders>().Count;
if (q>0)
{
s+=" ("+q.ToString()+")";
}
MySystemStatus.Time = s;
MySystemStatus.NetworkStatus = WifiHelper.Diagnose(store.Load<ServerSettings>().Address);
}
catch(Exception e)
{
Logger.WriteLine(()=>"Exception during Mainform.wifiandtime:\n" +e.ToString());
}
// Logger.WriteLine(()=>"End update wifi and time");
System.Threading.Thread.Sleep(1000);
}
// GC.Collect();
// Logger.WriteLine(()=>"Sleep update wifi and time");
System.Threading.ThreadPool.QueueUserWorkItem(UpdateWifiAndTime,par);
// Logger.WriteLine(()=>"Requeue update wifi and time");
}
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace HoTouch.Core.Services.Impl
{
/// <summary>
/// Description of TimeOutSocket.
/// </summary>
public class TimeOutSocket
{
private static bool IsConnectionSuccessful = false;
private static Exception socketexception;
private static ManualResetEvent TimeoutObject = new ManualResetEvent(false);
public static bool Connect(IPEndPoint remoteEndPoint, int timeoutMSec)
{
TimeoutObject.Reset();
socketexception = null;
string serverip = Convert.ToString(remoteEndPoint.Address);
int serverport = remoteEndPoint.Port;
Socket socket= new Socket(remoteEndPoint.AddressFamily, SocketType.Stream, ProtocolType.IP);
socket.BeginConnect(remoteEndPoint, new AsyncCallback(CallBackMethod), socket);
if (TimeoutObject.WaitOne(timeoutMSec, false))
{
if (IsConnectionSuccessful)
{
socket.Close();
return true;
}
else
{
socket.Close();
return false;
}
}
else
{
socket.Close();
return false;
}
}
private static void CallBackMethod(IAsyncResult asyncresult)
{
try
{
IsConnectionSuccessful = false;
Socket socket = asyncresult.AsyncState as Socket;
if (socket != null)
{
socket.EndConnect(asyncresult);
IsConnectionSuccessful = true;
}
}
catch (Exception ex)
{
IsConnectionSuccessful = false;
socketexception = ex;
}
finally
{
TimeoutObject.Set();
}
}
}
}
using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using Hotco.Interfaces;
using OpenNETCF.Net.NetworkInformation;
namespace HoTouch.Winforms.Services
{
/// <summary>
/// Description of WifiHelper.
/// </summary>
public class WifiHelper
{
static IPAddress ipa;
static IPEndPoint ipep;
static string oldaddress = null;
public WifiHelper()
{
}
public static NetworkStatus Diagnose(string address)
{
var q = address.Split(':');
try {
oldaddress = address;
var ip = q[0];
var port = q.Length==2?int.Parse(q[1]):5500;
ipa = new IPAddress(ip.Split('.').Select(n=>byte.Parse(n)).ToArray());
ipep = new IPEndPoint(ipa,port);
if (HoTouch.Core.Services.Impl.TimeOutSocket.Connect(ipep,5000))
return NetworkStatus.Ok;
else
return NetworkStatus.ServerNotAvailable;
}
catch(Exception)
{
if (false && System.Environment.OSVersion.Platform == PlatformID.WinCE)
{
var p = new Ping();
var ni=WirelessZeroConfigNetworkInterface.GetAllNetworkInterfaces().Where(x => x is IWirelessNetworkInterface)
.Cast<IWirelessNetworkInterface>()
.FirstOrDefault(x=>x.OperationalStatus!= OperationalStatus.Down);
if (ni == null || ni.SignalStrength.Strength== StrengthType.NoSignal)
return NetworkStatus.WifiNotAvailable;
try
{
if (p.Send(q[0],3).Status != IPStatus.Success)
return NetworkStatus.ServerNotAvailable;
}
catch(Exception)
{
return NetworkStatus.ServerNotAvailable;
}
}
}
return NetworkStatus.MobileServerNotAvailable;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment