using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows;
using System.Text.RegularExpressions;
using System.Windows.Controls;
using System.Windows.Shapes;
namespace CardAccess.Mapping.Components.APBAreas
{
class ApbAreaHelper
{
#region givingtext
public static VisualBrush TextBrush(FrameworkElement element, string text, Brush background)
{
Border border = new Border();
border.Background = Brushes.Transparent;
border.BorderThickness = new Thickness(1);
border.BorderBrush = Brushes.Transparent;
Binding heightBinding = new Binding("ActualHeight");
heightBinding.Source = element;
Binding widthBinding = new Binding("ActualWidth");
widthBinding.Source = element;
border.SetBinding(Border.WidthProperty, widthBinding);
border.SetBinding(Border.HeightProperty, heightBinding);
TextBlock textBlock = new TextBlock();
textBlock.Padding = new Thickness(2);
textBlock.Text = text;
textBlock.Foreground = Brushes.LightSlateGray;
textBlock.Opacity = .5;
textBlock.HorizontalAlignment = HorizontalAlignment.Center;
textBlock.VerticalAlignment = VerticalAlignment.Center;
Binding FontSizeBinding = new Binding("ActualWidth");
FontSizeBinding.Source = element;
FontSizeBinding.Converter = new ArithmeticConverter();
//if (element is Polygon) FontSizeBinding.ConverterParameter = "*0.07";
// else
FontSizeBinding.ConverterParameter = "*0.1";
textBlock.SetBinding(TextBlock.FontSizeProperty , FontSizeBinding);
// textBlock.SetBinding( TextBlock.WidthProperty , FontSizeBinding);
if (element is Polygon) textBlock.FontSize = 14;
border.Child = textBlock;
VisualBrush brush = new VisualBrush();
brush.Stretch = Stretch.None;
brush.Visual = border;
return brush;
}
#endregion
}
class ArithmeticConverter : IValueConverter
{
private const string ArithmeticParseExpression = "([+\\-*/]{1,1})\\s{0,}(\\-?[\\d\\.]+)";
private Regex arithmeticRegex = new Regex(ArithmeticParseExpression);
#region IValueConverter Members
object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is double && parameter != null)
{
string param = parameter.ToString();
if (param.Length > 0)
{
Match match = arithmeticRegex.Match(param);
if (match != null && match.Groups.Count == 3)
{
string operation = match.Groups[1].Value.Trim();
string numericValue = match.Groups[2].Value;
double number = 0;
if (double.TryParse(numericValue, out number)) // this should always succeed or our regex is broken
{
double valueAsDouble = (double)value;
double returnValue = 0;
switch (operation)
{
case "+":
returnValue = valueAsDouble + number;
break;
case "-":
returnValue = valueAsDouble - number;
break;
case "*":
returnValue = valueAsDouble * number;
break;
case "/":
returnValue = valueAsDouble / number;
break;
}
return returnValue;
}
}
}
}
return null;
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
}
}
Tuesday, November 6, 2007
Sunday, November 4, 2007
tcp socket connect timeout implementation.
_NetworkClient = Connect(remoteEndPoint, 4000);
private static ManualResetEvent waitob = new ManualResetEvent(false);
private static Exception asynexception;
public static TcpClient Connect(IPEndPoint remoteEndPoint, int timeoutMSec)
{
string host = Convert.ToString(remoteEndPoint.Address);
int portNo = remoteEndPoint.Port;
waitob.Reset();
asynexception = null;
TcpClient tc = new TcpClient();
IAsyncResult iar = tc.BeginConnect(host, portNo, new AsyncCallback(ConnectCallBack), tc);
if (waitob.WaitOne(timeoutMSec, false))
{
if (asynexception == null)
{
return tc;
}
else
{
throw asynexception;
}
}
else
{
tc.Close();
throw new TimeoutException("Timeout");
}
}
private static void ConnectCallBack(IAsyncResult ar)
{
try
{
TcpClient client = ar.AsyncState as TcpClient;
if (client.Client != null)
{
client.EndConnect(ar);
}
else
{
}
}
catch (Exception ex)
{
asynexception = ex;
}
finally
{
waitob.Set();
}
}
private static ManualResetEvent waitob = new ManualResetEvent(false);
private static Exception asynexception;
public static TcpClient Connect(IPEndPoint remoteEndPoint, int timeoutMSec)
{
string host = Convert.ToString(remoteEndPoint.Address);
int portNo = remoteEndPoint.Port;
waitob.Reset();
asynexception = null;
TcpClient tc = new TcpClient();
IAsyncResult iar = tc.BeginConnect(host, portNo, new AsyncCallback(ConnectCallBack), tc);
if (waitob.WaitOne(timeoutMSec, false))
{
if (asynexception == null)
{
return tc;
}
else
{
throw asynexception;
}
}
else
{
tc.Close();
throw new TimeoutException("Timeout");
}
}
private static void ConnectCallBack(IAsyncResult ar)
{
try
{
TcpClient client = ar.AsyncState as TcpClient;
if (client.Client != null)
{
client.EndConnect(ar);
}
else
{
}
}
catch (Exception ex)
{
asynexception = ex;
}
finally
{
waitob.Set();
}
}
WSDualHttpBinding cross machine problem solved.
public static class WsDualProxyHelper
{
public static void SetClientBaseAddress(DuplexClientBase proxy, int port)
where T : class
{
WSDualHttpBinding binding = proxy.Endpoint.Binding as WSDualHttpBinding;
Debug.Assert(binding != null);
string strHostName = Dns.GetHostName();
// Then using host name, get the IP address list..
IPHostEntry ipEntry = Dns.GetHostByName(strHostName);
IPAddress [] addr = ipEntry.AddressList;
binding.ClientBaseAddress = new Uri("http://" + addr[0].ToString() + ":" + port + "/");
}
public static void SetClientBaseAddress(DuplexClientBase proxy)
where T : class
{
lock (typeof(WsDualProxyHelper))
{
int portNumber = FindPort();
SetClientBaseAddress(proxy, portNumber);
try
{
proxy.Open();
}
catch
{
}
}
}
internal static int FindPort()
{
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
using (Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp))
{
socket.Bind(endPoint);
IPEndPoint local = (IPEndPoint)socket.LocalEndPoint;
return local.Port;
}
}
}
{
public static void SetClientBaseAddress
where T : class
{
WSDualHttpBinding binding = proxy.Endpoint.Binding as WSDualHttpBinding;
Debug.Assert(binding != null);
string strHostName = Dns.GetHostName();
// Then using host name, get the IP address list..
IPHostEntry ipEntry = Dns.GetHostByName(strHostName);
IPAddress [] addr = ipEntry.AddressList;
binding.ClientBaseAddress = new Uri("http://" + addr[0].ToString() + ":" + port + "/");
}
public static void SetClientBaseAddress
where T : class
{
lock (typeof(WsDualProxyHelper))
{
int portNumber = FindPort();
SetClientBaseAddress(proxy, portNumber);
try
{
proxy.Open();
}
catch
{
}
}
}
internal static int FindPort()
{
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
using (Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp))
{
socket.Bind(endPoint);
IPEndPoint local = (IPEndPoint)socket.LocalEndPoint;
return local.Port;
}
}
}
Subscribe to:
Posts (Atom)