Esempio n. 1
0
 public virtual bool Connect()
 {
     try
     {
         Monitor.Enter(this);
         XyNetCommon.SetSocketPermission();
         Reset();
         _mSocketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         IPEndPoint myEnd = null;
         try
         {
             myEnd = (_mSRemoteAddress == "") ? (new IPEndPoint(Dns.GetHostByName(Dns.GetHostName()).AddressList[0], _mNRemotePort)) : (new IPEndPoint(IPAddress.Parse(_mSRemoteAddress), _mNRemotePort));
         }
         catch (Exception) { }
         if (myEnd == null)
         {
             myEnd = new IPEndPoint(Dns.GetHostByName(_mSRemoteAddress).AddressList[0], _mNRemotePort);
         }
         _mSocketClient.Connect(myEnd);
         return(true);
     }
     catch (Exception oBug)
     {
         _mException = oBug;
         try
         {
             _mSocketClient.Shutdown(SocketShutdown.Both);
             _mSocketClient.Close();
         }
         catch (Exception) { }
         return(false);
     }
     finally { Monitor.Exit(this); }
 }
Esempio n. 2
0
 public bool StartServer()
 {
     try
     {
         Monitor.Enter(this);
         XyNetCommon.SetSocketPermission();
         StopServer();
         _mThreadPool.SetThreadErrorHandler(ThreadErrorHandler);
         _mThreadPool.StartThreadPool(_mNMinThreadCount, _mNMaxThreadCount);
         _mSocketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         _mSocketServer.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
         var myEnd = (_mSAddress == "") ? (new IPEndPoint(Dns.GetHostByName(Dns.GetHostName()).AddressList[0], _mNPort)) : (new IPEndPoint(IPAddress.Parse(_mSAddress), _mNPort));
         _mSocketServer.Bind(myEnd);
         _mSocketServer.Listen(MnListenBacklog);
         _mThreadPool.InsertWorkItem("Accept Clients", new AcceptClientsDelegate(AcceptClients), null, false);
         _mThreadPool.InsertWorkItem("Detect Input", new DetectInputDelegate(DetectInput), null, false);
         return(true);
     }
     catch (Exception oBug)
     {
         _mException = oBug;
         return(false);
     }
     finally { Monitor.Exit(this); }
 }
Esempio n. 3
0
        public bool SendStringData(String sData)
        {
            var 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));
        }
Esempio n. 4
0
 public Object[] ReceiveData()
 {
     try
     {
         Monitor.Enter(this);
         _mSocketClient.Blocking = false;
         var    nStart  = DateTime.Now.Ticks;
         var    nRead   = 0;
         var    nTotal  = 4;
         var    pData   = new Byte[4];
         Byte[] pHeader = null;
         while (true)
         {
             try
             {
                 Thread.Sleep(MnClientPause);
                 if (_mSocketClient.Available > 0)
                 {
                     nRead += _mSocketClient.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 > _mNMaxDataSize)
                 {
                     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) > _mNReadTimeout * 1000)
             {
                 throw new Exception("Timeout while receiving incoming data");
             }
         }
         if ((pHeader[0] & 0x0000000F) == 1)
         {
             return(new Object[] { pData, null });
         }
         else
         {
             if (pData.Length % 2 != 0)
             {
                 throw new Exception("Invalid string data size");
             }
             return(new Object[] { null, XyNetCommon.BinaryToString(pData) });
         }
     }
     catch (Exception oBug)
     {
         Connect();
         _mException = oBug;
         return(null);
     }
     finally
     {
         _mSocketClient.Blocking = true;
         Monitor.Exit(this);
     }
 }
Esempio n. 5
0
        private void ProcessInput(Socket sock, IPEndPoint ipe)
        {
            try
            {
                var pHeader = new Byte[4];
                var nPos    = 0;
                var 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);
                            _mHtSockets.Remove(ipe.Address + ":" + ipe.Port);
                            Monitor.Exit(this);
                            return;
                        }
                    }
                    else
                    {
                        Thread.Sleep(MnServerPause);
                    }
                    if (nPos < 4 && ((DateTime.Now.Ticks - nStart) / 10000) > _mNReadTimeout * 1000)
                    {
                        throw new Exception("Timeout while receiving incoming data");
                    }
                }
                if ((pHeader[0] & 0x0000000F) != 2)
                {
                    var nSize = pHeader[1] + pHeader[2] * 256 + pHeader[3] * 65536 + (pHeader[0] / 16) * 16777216;
                    if (nSize > _mNMaxDataSize)
                    {
                        throw new Exception("Data size too large");
                    }
                    var 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(MnServerPause);
                        }
                        if (nPos < nSize && ((DateTime.Now.Ticks - nStart) / 10000) > _mNReadTimeout * 1000)
                        {
                            throw new Exception("Timeout while receiving incoming data");
                        }
                    }
                    Monitor.Enter(this);
                    _mListSockets.Add(sock);
                    Monitor.Exit(this);
                    switch ((pHeader[0] & 0x0000000F))
                    {
                    case 1:
                        if (_mDelegateBinaryInputHandler != null)
                        {
                            _mThreadPool.InsertWorkItem("Handle Binary Input", new BinaryInputHandlerDelegate(_mDelegateBinaryInputHandler), new Object[] { ipe.Address.ToString(), ipe.Port, pData }, false);
                        }
                        else
                        {
                            throw new Exception("No binary input handler");
                        }
                        break;

                    case 0:
                        if (_mDelegateStringInputHandler != null)
                        {
                            _mThreadPool.InsertWorkItem("Handle String Input", new StringInputHandlerDelegate(_mDelegateStringInputHandler), new Object[] { ipe.Address.ToString(), ipe.Port, XyNetCommon.BinaryToString(pData) }, false);
                        }
                        else
                        {
                            throw new Exception("No string input handler");
                        }
                        break;

                    default:
                        throw new Exception("Invalid string data size");
                    }
                }
            }
            catch (Exception oBug)
            {
                Monitor.Enter(this);
                _mHtSockets.Remove(ipe.Address + ":" + ipe.Port);
                Monitor.Exit(this);
                try
                {
                    sock.Shutdown(SocketShutdown.Both);
                    sock.Close();
                }
                catch (Exception) { }
                if (_mDelegateExceptionHandler != null)
                {
                    _mThreadPool.InsertWorkItem("Handle Exception", _mDelegateExceptionHandler, new Object[] { oBug }, false);
                }
                else
                {
                    Monitor.Enter(this);
                    _mException = oBug;
                    Monitor.Exit(this);
                }
            }
        }