/Src/Core.TCPCommunicationClient/TCPClientBase.cs
C# | 196 lines | 177 code | 19 blank | 0 comment | 27 complexity | b4c4e33b3fe44ed4fc747e1b177b0ae9 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Collections;
- using System.Net;
- using System.IO;
- using Core.IO;
- using System.Threading;
- using Core.Util;
- using System.Reflection;
- using System.Net.Sockets;
- using System.Xml.Linq;
-
- #if !CF
- using Amib.Threading;
- using System.Xml.Linq;
- #endif
-
-
- namespace Core.TCPCommunicationClient
- {
- public abstract class TCPClientBase : EventPoint
- {
- public bool IsConnecting { get; protected set; }
- protected delegate void RequestAsyncDelegate(byte[] PostData);
-
- private const int listenByteBuffer = 10240;
-
- #if !CF
- protected static SmartThreadPool threadPool = new SmartThreadPool();
- #endif
- protected internal TCPClientBase()
- {
-
- }
-
- protected string HostKey { get; set; }
- protected IPEndPoint HostIPEndPoint { get; set; }
-
- public override void Initialization()
- {
- Connect();
-
- base.Initialization();
- }
- public void Connect()
- {
- if (_socket != null)
- {
- _socket.Close();
- }
-
- if (HostIPEndPoint == null)
- {
- XMLConfig xml = new XMLConfig(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\Config\\TCPClientHostConfig.xml");
- if (xml.Exists())
- {
- try
- {
- XElement xe = xml.GetElements("Host").Where(L =>
- L.Attribute("Key") != null && L.Attribute("Key").Value == HostKey).FirstOrDefault();
-
- IPAddress address = IPAddress.Parse(xe.Element("HostAddress").Value);
- HostIPEndPoint = new IPEndPoint(address, xe.Element("HostPort").Value.ToInt());
- }
- catch (Exception ex)
- {
- AppLog.Instance.LogMessage("TCPClient XML Error \r\n" + ex.Message, AppLog.LogType.ErrorLog);
- return;
- }
- }
- else
- {
- AppLog.Instance.LogMessage("TCPClient XML does not exist ", AppLog.LogType.ErrorLog);
- return;
- }
- }
-
- _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- _socket.Connect(HostIPEndPoint);
-
- IsConnecting = true;
- if (ConnectEvent != null)
- ConnectEvent(this, EventArgs.Empty);
- #if !CF
- TCPClientFactory.ThreadPool.QueueWorkItem(new WorkItemCallback(listen));
- #else
- Thread listenThread = new Thread(new ThreadStart(listen));
- listenThread.IsBackground = true;
- listenThread.Start();
- #endif
- }
- public void Disconnect()
- {
- if (_socket != null)
- {
- IsConnecting = false;
- _socket.Shutdown(SocketShutdown.Both);
- _socket.Close();
- if (DisconnectEvent != null)
- DisconnectEvent(this, EventArgs.Empty);
- }
- }
-
- private Socket _socket = null;
- protected Socket clientSocket
- {
- get
- {
- if (_socket == null)
- {
- Connect();
- }
- return _socket;
- }
- }
-
- private object listen(object o)
- {
- listen();
- return null;
- }
- private void listen()
- {
- while (clientSocket.Connected)
- {
- try
- {
- byte[] data = new byte[listenByteBuffer];
- int recv = clientSocket.Receive(data);
- if (recv == 0)
- {
- IsConnecting = false;
- clientSocket.Close();
- return;
- }
- processPostBackData(data, recv);
- }
- catch
- {
- IsConnecting = false;
- if (DisconnectEvent != null)
- DisconnectEvent(this, EventArgs.Empty);
- return;
- }
- }
- }
-
- public event GeneralEventHandler<byte[]> DataPostBackEvent;
- protected virtual void processPostBackData(byte[] data, int recv)
- {
- if (DataPostBackEvent != null)
- DataPostBackEvent(this, data);
- }
-
- private void _request(object o)
- {
- if (o is byte[])
- {
- Request((byte[])o);
- }
- }
- public void Request(byte[] PostData)
- {
- if (IsConnecting && clientSocket.Connected)
- {
- try
- {
- clientSocket.Send(PostData);
- }
- catch (Exception ex)
- {
- Disconnect();
-
- AppLog.Instance.LogMessage("TCPClient error : \r\n" + ex.Message, AppLog.LogType.WarningLog);
- }
- }
- }
- public void RequestAsync(byte[] PostData)
- {
- if (IsConnecting)
- {
- #if !CF
- RequestAsyncDelegate ra = new RequestAsyncDelegate(RequestAsync);
- ra.BeginInvoke(PostData, null, null);
- #else
- ThreadPool.QueueUserWorkItem(new WaitCallback(_request), PostData);
- #endif
- }
- }
-
- public event EventHandler ConnectEvent;
- public event EventHandler DisconnectEvent;
- }
- }