public virtual bool Connect() { try { Monitor.Enter(this); XYNetCommon.SetSocketPermission(); Reset(); m_socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint myEnd = null; try { myEnd = (m_sRemoteAddress == "")?(new IPEndPoint(Dns.GetHostByName(Dns.GetHostName()).AddressList[0], m_nRemotePort)):(new IPEndPoint(IPAddress.Parse(m_sRemoteAddress), m_nRemotePort)); } catch (Exception) {} if (myEnd == null) { myEnd = new IPEndPoint(Dns.GetHostByName(m_sRemoteAddress).AddressList[0], m_nRemotePort); } m_socketClient.Connect(myEnd); return(true); } catch (Exception oBug) { m_exception = oBug; try { m_socketClient.Shutdown(SocketShutdown.Both); m_socketClient.Close(); } catch (Exception) {} return(false); } finally { Monitor.Exit(this); } }
public bool StartServer() { try { Monitor.Enter(this); XYNetCommon.SetSocketPermission(); StopServer(); m_threadPool.SetThreadErrorHandler(new ThreadErrorHandlerDelegate(ThreadErrorHandler)); m_threadPool.StartThreadPool(m_nMinThreadCount, m_nMaxThreadCount); m_socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_socketServer.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); IPEndPoint myEnd = (m_sAddress == "")?(new IPEndPoint(Dns.GetHostByName(Dns.GetHostName()).AddressList[0], m_nPort)):(new IPEndPoint(IPAddress.Parse(m_sAddress), m_nPort)); m_socketServer.Bind(myEnd); m_socketServer.Listen(m_nListenBacklog); m_threadPool.InsertWorkItem("Accept Clients", new AcceptClientsDelegate(AcceptClients), null, false); m_threadPool.InsertWorkItem("Detect Input", new DetectInputDelegate(DetectInput), null, false); return(true); } catch (Exception oBug) { m_exception = oBug; return(false); } finally { Monitor.Exit(this); } }
public bool SendStringData(String sData) { Byte[] pData = new Byte[sData.Length * 2 + 4]; pData[0] = (Byte)(((2 * sData.Length) / 16777216) * 16); pData[1] = (Byte)((2 * sData.Length) % 256); pData[2] = (Byte)(((2 * sData.Length) % 65536) / 256); pData[3] = (Byte)(((2 * sData.Length) / 65536) % 256); XYNetCommon.StringToBinary(sData).CopyTo(pData, 4); return(SendRawData(pData)); }
public Object[] ReceiveData() { try { Monitor.Enter(this); m_socketClient.Blocking = false; long nStart = DateTime.Now.Ticks; int nRead = 0; int nTotal = 4; Byte[] pData = new Byte[4]; Byte[] pHeader = null; while (true) { try { Thread.Sleep(m_nClientPause); if (m_socketClient.Available > 0) { nRead += m_socketClient.Receive(pData, nRead, nTotal - nRead, SocketFlags.None); if ((pData[0] & 0x0000000F) == 2) { nRead = 0; } } } catch (Exception) {} if (pHeader == null && nRead == 4) { nTotal = (pData[1] & 0x000000FF) + (pData[2] & 0x000000FF) * 256 + (pData[3] & 0x000000FF) * 65536 + ((pData[0] & 0x000000FF) / 16) * 16777216; if ((pData[0] & 0x0000000F) > 1) { throw new Exception("Invalid input data type byte"); } if (nTotal > m_nMaxDataSize) { throw new Exception("Data size too large"); } pHeader = pData; nRead = 0; pData = new Byte[nTotal]; } if (pHeader != null && nRead == nTotal) { break; } if (((DateTime.Now.Ticks - nStart) / 10000) > m_nReadTimeout * 1000) { throw new Exception("Timeout while receiving incoming data"); } } if ((pHeader[0] & 0x0000000F) == 1) { return(new Object[2] { pData, null }); } else { if (pData.Length % 2 != 0) { throw new Exception("Invalid string data size"); } return(new Object[2] { null, XYNetCommon.BinaryToString(pData) }); } } catch (Exception oBug) { Connect(); m_exception = oBug; return(null); } finally { m_socketClient.Blocking = true; Monitor.Exit(this); } }
private void ProcessInput(Socket sock, IPEndPoint ipe) { try { Byte[] pHeader = new Byte[4]; int nPos = 0; long nStart = DateTime.Now.Ticks; while (nPos < 4) { if (sock.Available > 0) { nPos += sock.Receive(pHeader, nPos, Math.Min(sock.Available, (4 - nPos)), SocketFlags.None); if ((pHeader[0] & 0x000000FF) == 255) { sock.Shutdown(SocketShutdown.Both); sock.Close(); Monitor.Enter(this); m_htSockets.Remove(ipe.Address.ToString() + ":" + ipe.Port.ToString()); Monitor.Exit(this); return; } } else { Thread.Sleep(m_nServerPause); } if (nPos < 4 && ((DateTime.Now.Ticks - nStart) / 10000) > m_nReadTimeout * 1000) { throw new Exception("Timeout while receiving incoming data"); } } if ((pHeader[0] & 0x0000000F) != 2) { int nSize = pHeader[1] + pHeader[2] * 256 + pHeader[3] * 65536 + (pHeader[0] / 16) * 16777216; if (nSize > m_nMaxDataSize) { throw new Exception("Data size too large"); } Byte[] pData = new Byte[nSize]; nPos = 0; nStart = DateTime.Now.Ticks; while (nPos < nSize) { if (sock.Available > 0) { nPos += sock.Receive(pData, nPos, Math.Min(sock.Available, (nSize - nPos)), SocketFlags.None); } else { Thread.Sleep(m_nServerPause); } if (nPos < nSize && ((DateTime.Now.Ticks - nStart) / 10000) > m_nReadTimeout * 1000) { throw new Exception("Timeout while receiving incoming data"); } } Monitor.Enter(this); m_listSockets.Add(sock); Monitor.Exit(this); if ((pHeader[0] & 0x0000000F) == 1) { if (m_delegateBinaryInputHandler != null) { m_threadPool.InsertWorkItem("Handle Binary Input", new BinaryInputHandlerDelegate(m_delegateBinaryInputHandler), new Object[3] { ipe.Address.ToString(), ipe.Port, pData }, false); } else { throw new Exception("No binary input handler"); } } else if ((pHeader[0] & 0x0000000F) == 0) { if (m_delegateStringInputHandler != null) { m_threadPool.InsertWorkItem("Handle String Input", new StringInputHandlerDelegate(m_delegateStringInputHandler), new Object[3] { ipe.Address.ToString(), ipe.Port, XYNetCommon.BinaryToString(pData) }, false); } else { throw new Exception("No string input handler"); } } else { throw new Exception("Invalid string data size"); } } } catch (Exception oBug) { Monitor.Enter(this); m_htSockets.Remove(ipe.Address.ToString() + ":" + ipe.Port.ToString()); Monitor.Exit(this); try { sock.Shutdown(SocketShutdown.Both); sock.Close(); } catch (Exception) {} if (m_delegateExceptionHandler != null) { m_threadPool.InsertWorkItem("Handle Exception", m_delegateExceptionHandler, new Object[1] { oBug }, false); } else { Monitor.Enter(this); m_exception = oBug; Monitor.Exit(this); } } }