123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581 |
- using System;
- using System.Text;
- using System.Collections;
- using System.Net.Sockets;
- using System.Net;
- using System.Threading;
- using System.Windows.Forms;
- using System.Net.NetworkInformation;
- namespace FPER
- {
-
-
-
- public class _TcpComClient
- {
-
- MDIParent mdi = null;
-
- public const int WaitPollTime = 10000;
-
- public const int RxBufSize = 64;
-
- public delegate void SocketConnect_Inform_Message_Handler(object data, bool Connect);
- public event SocketConnect_Inform_Message_Handler SocketConnect_Inform_Event;
-
- public Hashtable TxConnect_Socket = new Hashtable();
-
- public Hashtable TxConnect_KeepTimer = new Hashtable();
-
- public _TcpComClient(MDIParent mdiparent)
- {
- mdi = mdiparent;
- }
-
- ~_TcpComClient()
- {
- }
-
- public void Create_TxConnect_Socket(string Key, Socket RxSocket, System.Threading.Timer Timer)
- {
- if (TxConnect_Socket.Contains(Key) == true)
- {
-
- }
- else
- {
-
-
- TxConnect_Socket.Add(Key, RxSocket);
- }
- }
-
- public void Delete_TxConnect_Socket(string Key)
- {
-
- if (Key == null)
- {
- foreach (DictionaryEntry d in TxConnect_Socket)
- ((Socket)d.Value).Dispose();
-
-
-
- TxConnect_Socket.Clear();
- }
-
- else
- {
- if (TxConnect_Socket.Contains(Key) == true)
- {
-
- if (((Socket)TxConnect_Socket[Key]) != null)
- ((Socket)TxConnect_Socket[Key]).Close();
-
-
-
-
-
- TxConnect_Socket.Remove(Key);
- }
- }
- }
-
- public Queue RxQueue = new Queue();
-
- public Queue TxQueue = new Queue();
-
- public Object CS_TxQueue = new Object();
- public Object CS_RxQueue = new Object();
-
- public class EndPointFormat
- {
- public string IP = null;
- public string Port = null;
- public byte[] Msg = null;
- }
-
- public class SendFormat
- {
- public string IP = null;
- public string Port = null;
- public string Msg = null;
- }
-
- public class SendFormat_CardHolder
- {
- public string IP = null;
- public string Port = null;
- public string Msg = null;
- public string Protocol = null;
- public string UID= null;
- }
-
-
-
-
- public void RxQueue_ADD(EndPointFormat Data)
- {
- lock (CS_RxQueue)
- {
- RxQueue.Enqueue(Data);
- }
- }
- public void TxQueue_ADD(SendFormat Data)
- {
- lock (CS_TxQueue)
- {
- TxQueue.Enqueue(Data);
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public bool Socket_Connect(string DestIP, string DestPort)
- {
-
- string TxConnect_Socket_Key = DestIP + ":" + DestPort;
- try
- {
-
- if (TxConnect_Socket.ContainsKey(TxConnect_Socket_Key) == false)
- {
-
- Socket TxSocket = Send_Check(DestIP, DestPort, WaitPollTime);
- if (TxSocket != null)
- {
-
- EndPointFormat RxQueFormat = new EndPointFormat();
-
- RxQueFormat.IP = DestIP;
-
- RxQueFormat.Port = DestPort;
-
- mdi.Thread.Abort(TxConnect_Socket_Key);
- mdi.Thread.Create(TxConnect_Socket_Key, Socket_Receive_byThread, RxQueFormat);
-
- return true;
- }
- else
- return false;
- }
- else
- {
-
-
- if (Ping_SyncCheck(DestIP) == false)
- {
-
- Socket_Close(TxConnect_Socket_Key);
- return false;
- }
- else
- {
- return true;
- }
- }
- }
- catch (Exception ex)
- {
- Util.UErrorMessage(ex, 0, 0);
-
-
-
-
-
-
-
-
-
- return false;
- }
- }
-
- public void Socket_Receive_byThread(object QueFormat)
- {
- EndPointFormat Dest = (EndPointFormat)QueFormat;
- string TxConnect_Socket_Key = Dest.IP + ":" + Dest.Port;
-
- while (true)
- {
- try
- {
-
- Byte[] ReceiveByte = new Byte[RxBufSize];
-
- ((Socket)TxConnect_Socket[TxConnect_Socket_Key]).Blocking = true;
-
- int RxDataCnt = ((Socket)TxConnect_Socket[TxConnect_Socket_Key]).Receive(ReceiveByte, ReceiveByte.Length, SocketFlags.None);
-
- if (RxDataCnt > 0)
- {
-
- EndPointFormat RxQueFormat = new EndPointFormat();
-
- RxQueFormat.IP = Dest.IP;
-
- RxQueFormat.Port = Dest.Port;
-
- RxQueFormat.Msg = new Byte[RxDataCnt];
-
- for (int i = 0; i < RxDataCnt; i++)
- RxQueFormat.Msg[i] = ReceiveByte[i];
-
-
-
- string Rx = _Convert.DecodingData(_Convert.Coding.UTF8, RxQueFormat.Msg);
-
- mdi.Event.ClientSocketReceive_SendMessage_Write(Rx);
- }
-
- if (RxDataCnt == 0)
- {
- throw new Exception("server shutdown. not socket close");
- }
- }
- catch (Exception ex)
- {
- Util.UErrorMessage(ex, 0, 0);
-
-
-
- Socket_Close(TxConnect_Socket_Key);
- break;
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void Socket_Send(string IP, string Port, byte[] Message)
- {
-
- EndPointFormat TxQueFormat = new EndPointFormat();
-
- TxQueFormat.IP = IP;
-
- TxQueFormat.Port = Port;
-
- TxQueFormat.Msg = new Byte[Message.Length];
-
- TxQueFormat.Msg = Message;
-
- Thread Socket_Send_Thread = new Thread(new ParameterizedThreadStart(Socket_Send_byThread));
-
- Socket_Send_Thread.Start(TxQueFormat);
- }
-
- public void Socket_Send_byThread(object QueFormat)
- {
-
- EndPointFormat TxQueFormat = (EndPointFormat)QueFormat;
- Socket TxSocket = null;
- try
- {
-
- TxSocket = Send_Check(TxQueFormat.IP, TxQueFormat.Port, WaitPollTime);
-
- if (TxSocket != null)
- {
-
- Send_Data(TxSocket, TxQueFormat);
- }
- else
- {
-
-
- }
- }
- catch
- {
-
-
- }
- }
-
- public Socket Send_Check(string DestIP, string DestPort, int timeoutMs)
- {
-
- string TxConnect_Socket_Key = DestIP + ":" + DestPort;
- try
- {
-
- if (TxConnect_Socket.ContainsKey(TxConnect_Socket_Key) == true)
- {
- Socket socket = (Socket)TxConnect_Socket[TxConnect_Socket_Key];
-
- if (Ping_SyncCheck(DestIP) == false)
- {
-
- Socket_Close(TxConnect_Socket_Key);
- return null;
- }
- else
- return socket;
- }
- else
- {
-
- Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
-
- IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(DestIP), Int32.Parse(DestPort));
-
-
-
- socket.Connect(endPoint);
-
-
-
-
- Create_TxConnect_Socket(TxConnect_Socket_Key, socket, null);
-
- if (SocketConnect_Inform_Event != null) SocketConnect_Inform_Event(TxConnect_Socket_Key, true);
- return socket;
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- }
- catch
- {
-
- Socket_Close(TxConnect_Socket_Key);
- return null;
- }
- }
-
- public void Send_Data(Socket TxSocket, EndPointFormat TxQueFormat)
- {
-
- string TxConnect_Socket_Key = TxQueFormat.IP + ":" + TxQueFormat.Port;
- try
- {
- int TxDataCnt = 0;
-
- TxSocket.Blocking = true;
-
-
- TxDataCnt = TxSocket.Send(TxQueFormat.Msg, 0, TxQueFormat.Msg.Length, 0);
-
- if (TxDataCnt == TxQueFormat.Msg.Length)
- {
-
- string Tx = _Convert.DecodingData(_Convert.Coding.UTF8, TxQueFormat.Msg);
-
- }
- else
- {
-
-
- }
- }
- catch
- {
-
- Socket_Close(TxConnect_Socket_Key);
- }
- }
-
-
-
- public PingOptions options = new PingOptions();
- public bool Ping_SyncCheck(string DestIP)
- {
- try
- {
- if (DestIP == null || DestIP.Trim().Length == 0) return false;
- using (Ping pingSender = new Ping())
- {
- options.DontFragment = true;
- string data = "0";
- byte[] buffer = Encoding.ASCII.GetBytes(data);
-
- PingReply reply = pingSender.Send(DestIP, 500, buffer, options);
- if (reply.Status == IPStatus.Success)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- catch (Exception ex)
- {
- Util.UErrorMessage(ex, 0, 0);
- return false;
- }
- }
-
-
- public void Socket_Close(string TxConnect_Socket_Key)
- {
- try
- {
-
- Delete_TxConnect_Socket(TxConnect_Socket_Key);
-
- if (SocketConnect_Inform_Event != null) SocketConnect_Inform_Event(TxConnect_Socket_Key, false);
-
-
-
-
-
-
-
- mdi.Thread.Abort(TxConnect_Socket_Key);
- }
- catch (Exception ex)
- {
- Util.UErrorMessage(ex, 0, 0);
- }
- }
-
- }
- }
|