Beispiel #1
0
 public void Stop(ref UICbMsg cbMsg)
 {
     if (bListning)
     {
         cbMsg += Txt.s._[(int)TxI.CONN_SRVR_CG];
     }
     bRW1 = bListning = false;
 }
Beispiel #2
0
 private bool ErrCode2Msg(int code, ref UICbMsg cbm)
 {
     if (code == 10061)
     {
         cbm += Txt.s._[(int)TxI.CONN_NOK];
         return(false);
     }
     return(true);
 }
Beispiel #3
0
        public static void DisableTaskManager(ref UICbMsg cbMsg)
        {
            RegistryKey regkey      = default(RegistryKey);
            string      keyValueInt = "1";
            string      subKey      = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";

            try
            {
                regkey = Registry.CurrentUser.CreateSubKey(subKey);
                regkey.SetValue("DisableTaskMgr", keyValueInt);
                regkey.Close();
            }
            catch (UnauthorizedAccessException ex)
            {
                cbMsg += ex.Message;
            }
        }
Beispiel #4
0
        public bool ConnectWR(ref UICbMsg cbMsg)
        {
            if (mTcpClnt != null)
            {
                return(false);
            }
            mTcpClnt = new TcpClient(AddressFamily.InterNetwork);
            bool bConn = true; //srvr side

            bRW = true;        //clnt side
            try {
                mTcpClnt.Connect(mSrvrAddr, mSrvrPort);
            } catch (SocketException e) {
                if (ErrCode2Msg(e.ErrorCode, ref cbMsg))
                {
                    cbMsg += "\nEx: " + e.Message;
                }
                bConn = false;
            }
            NetworkStream stream = null;

            if (bConn)
            {
                stream = mTcpClnt.GetStream();
            }

            byte[] buf = new byte[1024 * 1024];

            while (bConn && bRW)
            {
                //write message to server
                byte[] msg = dgBufPrep();
                if (msg == null)
                {
                    break;
                }

                try {
                    stream.Write(BitConverter.GetBytes(msg.Length), 0, 4);
                    stream.Write(msg, 0, msg.Length);
                }
                catch (System.IO.IOException e) {
                    cbMsg += "\nEx: " + e.Message;
                    bConn  = false;
                    break;
                }

                if (!bRW)
                {
                    break;
                }

                //read message from server
                int nLength, nByte;

                //Incoming message may be larger than the buffer size.
                //Do not rely on stream.DataAvailable, because the response
                //  may be split into multiple TCP packets, and a packet
                //  has not yet been delivered at the moment checking DataAvailable
                try
                {
                    nByte = stream.Read(buf, 0, buf.Length);
                } catch (System.IO.IOException e) {
                    cbMsg += "\nEx: " + e.Message;
                    bConn  = false;
                    break;
                }

                if (nByte < 4)
                {
                    bConn = false;
                    break;
                }

                nLength = BitConverter.ToInt32(buf, 0);
                nByte  -= 4;

                if (nLength < 1)
                {
                    break;
                }

                byte[] recvMsg = new byte[nLength];

                int offs = 0;
                if (0 < nByte)
                {
                    if (nLength < nByte)
                    {
                        nByte = nLength;
                    }
                    Buffer.BlockCopy(buf, 4, recvMsg, offs, nByte);
                    nLength -= nByte;
                    offs    += nByte;
                }

                while (bConn && bRW && 0 < nLength)
                {
                    try
                    {
                        nByte = stream.Read(buf, 0, buf.Length);
                    }
                    catch (System.IO.IOException e)
                    {
                        cbMsg += "\nEx: " + e.Message;
                        bConn  = false;
                    }
                    if (bConn)
                    {
                        if (nLength < nByte)
                        {
                            nByte = nLength;
                        }
                        Buffer.BlockCopy(buf, 0, recvMsg, offs, nByte);
                        nLength -= nByte;
                        offs    += nByte;
                    }
                }

                if (bRW && recvMsg != null && 0 < recvMsg.Length)
                {
                    bRW = dgBufHndl(recvMsg);
                }
            }
            if (bCbMsg)
            {
                cbMsg += Txt.s._[(int)TxI.CONN_CLNT_CE];
            }
            mTcpClnt.Close();
            mTcpClnt = null;
            return(!bConn);
        }
Beispiel #5
0
        public void Start(ref UICbMsg cbMsg)
        {
            if (mTcpListr != null)
            {
                return;
            }
            bListning = true;
            cbMsg    += Txt.s._[(int)TxI.CONN_SRVR_ST];
            mTcpListr = new TcpListener(IPAddress.Any, mPort);
            try {
                mTcpListr.Start();
            }
            catch (SocketException e) {
                cbMsg += "\nEx: " + e.Message;
                Stop(ref cbMsg);
            }

            while (bListning)
            {
                System.Threading.Thread.Sleep(8);//do not overhead CPU
                bool p = false;
                try { p = mTcpListr.Pending(); }
                catch (InvalidOperationException e) {
                    cbMsg += "\nEx: " + e.Message;
                    Stop(ref cbMsg);
                    break;
                }
                if (p)
                {
                    bRW1 = true;
                    TcpClient     cli    = null;
                    NetworkStream stream = null;
                    try { cli = mTcpListr.AcceptTcpClient(); }
                    catch (SocketException e)
                    {
                        cbMsg += "\nEx: " + e.Message;
                        Stop(ref cbMsg);
                        break;
                    }

                    try { stream = cli.GetStream(); }
                    catch (InvalidOperationException e)
                    {
                        cbMsg += "\nEx: " + e.Message;
                        bRW1   = false;
                    }

                    byte[] buf = new byte[1024 * 1024];

                    int nByte;
                    while (bRW1)
                    {
                        //Incoming message may be larger than the buffer size.
                        //Do not rely on stream.DataAvailable, because the response
                        //  may be split into multiple TCP packets, and a packet
                        //  has not yet been delivered at the moment checking DataAvailable
                        try
                        {
                            nByte = stream.Read(buf, 0, buf.Length);
                        }
                        catch (System.IO.IOException e)
                        {
                            cbMsg += "\nEx: " + e.Message;
                            break;
                        }
                        if (nByte < 4)
                        {
                            break;
                        }

                        int nLength = BitConverter.ToInt32(buf, 0);
                        nByte -= 4;

                        if (nLength < 1)
                        {
                            break;
                        }

                        byte[] recvMsg = new byte[nLength];

                        int offs = 0;
                        if (0 < nByte)
                        {
                            if (nLength < nByte)
                            {
                                nByte = nLength;
                            }
                            Buffer.BlockCopy(buf, 4, recvMsg, offs, nByte);
                            nLength -= nByte;
                            offs    += nByte;
                        }

                        while (bRW1 && 0 < nLength)
                        {
                            try
                            {
                                nByte = stream.Read(buf, 0, buf.Length);
                            }
                            catch (System.IO.IOException e)
                            {
                                cbMsg += "\nEx: " + e.Message;
                                bRW1   = false;
                            }
                            if (bRW1)
                            {
                                if (nLength < nByte)
                                {
                                    nByte = nLength;
                                }
                                Buffer.BlockCopy(buf, 0, recvMsg, 0, nByte);
                                nLength -= nByte;
                                offs    += nByte;
                            }
                        }

                        if (bRW1 && recvMsg != null && 3 < recvMsg.Length)
                        {
                            byte[] msg;
                            bRW1 = dgHndl(recvMsg, out msg);
                            if (msg != null && 0 < msg.Length)
                            {
                                try
                                {
                                    stream.Write(BitConverter.GetBytes(msg.Length), 0, 4);
                                    stream.Write(msg, 0, msg.Length);
                                }
                                catch (System.IO.IOException e)
                                {
                                    cbMsg += "\nEx: " + e.Message;
                                    bRW1   = false;
                                }
                            }
                            else
                            {
                                bRW1 = false;
                            }
                        }
                        else
                        {
                            bRW1 = false;
                        }
                    }
                    cli.Close();
                    cbMsg += Txt.s._[(int)TxI.CONN_CLNT_CE];
                }
            }
            bListning = false;
            try { mTcpListr.Stop(); }
            catch (SocketException e) { cbMsg += "\nEx: " + e.Message; }
            mTcpListr = null;
            cbMsg    += Txt.s._[(int)TxI.CONN_SRVR_CE];
        }