Пример #1
0
        private void ReadCallback(IAsyncResult result)
        {
            int           read;
            NetworkStream networkStream;

            try
            {
                networkStream = clientSocket.GetStream();
                read          = networkStream.EndRead(result);
            }
            catch (Exception ex)
            {
                //clsLOG.SetLOG(LOGTYPE.SYSTEMERROR, ex.ToString());
                //An error has occured when reading...
                clientSocket.Close();
                if (OnDisconnected != null)
                {
                    OnDisconnected(this);
                }
                return;
            }

            if (read == 0)
            {
                //The connection has been closed.
                clientSocket.Close();

                if (OnDisconnected != null)
                {
                    OnDisconnected(this);
                }

                return;
            }

            byte[] buffer        = result.AsyncState as byte[];
            byte[] ResizedBuffer = TrimEnd(buffer);
            //string sdata = Encoding.ASCII.GetString(buffer).Trim('\0');
            _recvBuffer.AddRange(ResizedBuffer);

            byte[] data = new byte[_recvBuffer.Count];
            _recvBuffer.CopyTo(0, data, 0, _recvBuffer.Count);

            ReciveQueue.Enqueue(data);

            buffer = new byte[clientSocket.ReceiveBufferSize];

            networkStream.BeginRead(buffer, 0, buffer.Length, ReadCallback, buffer);
        }
Пример #2
0
        void PollCheck()
        {
            clsMsg UMSG = null;

            while (true)
            {
                if (clientSocket.Connected)
                {
                    try
                    {
                        UMSG = null;

                        //송신부
                        if (Uqueue.Count > 0)
                        {
                            UMSG = Uqueue.Dequeue();
                            byte[] CmdTemp = null;
                            int    nbufCnt = 0;

                            if (UMSG != null)
                            {
                                //CmdTemp = StringToByte(UMSG.S_DATA + "\n\0");
                                CmdTemp = Encoding.UTF8.GetBytes(UMSG.S_DATA);
                                nbufCnt = Encoding.UTF8.GetByteCount(UMSG.S_DATA);
                            }
                            else
                            {
                                continue;
                            }

                            Thread.Sleep(100);
                            networkStream.Write(CmdTemp, 0, nbufCnt);

                            clsLOG.SetLOG(LOGTYPE.Message, string.Format(" [SEND->>] :{0}\t{1}", UMSG.CLIENT_IP, UMSG.S_DATA));
                        }

                        //수신부
                        if (ReciveQueue.Count > 0)
                        {
                            // GPS / BLE Data
                            // 이번에는 구분자가 STX, ETX 재대로 들어오니까 수정.
                            byte[] data = ReciveQueue.Dequeue();

                            string sdata = Encoding.ASCII.GetString(data).Trim('\0');

                            string[] arr_sdata = sdata.Split(new char[] { '\u0002', '\u0003' },
                                                             StringSplitOptions.RemoveEmptyEntries);

                            foreach (string temp in arr_sdata)
                            {
                                OnReceiveData(IP, PORT, temp, data);
                            }


                            //int nStartIdx = -1;
                            //int nEndIdx = -1;

                            //byte[] data = ReciveQueue.Dequeue();

                            //string sdata = Encoding.ASCII.GetString(data).Trim('\0');

                            //if (OnReceiveData != null)
                            //{
                            //    for(int nloop = 0; nloop < sdata.Length; nloop+=2)
                            //    {
                            //        if(sdata[nloop].ToString() + sdata[nloop + 1].ToString() == "02")
                            //        {
                            //            nStartIdx = nloop;
                            //        }
                            //        if (sdata[nloop].ToString() + sdata[nloop + 1].ToString() == "03")
                            //        {
                            //            nEndIdx = nloop + 1;
                            //        }

                            //        if(nStartIdx != -1 && nEndIdx != -1)
                            //        {
                            //            OnReceiveData(IP, PORT, sdata.Substring(nStartIdx, (nEndIdx + 1) - nStartIdx), data);
                            //            nStartIdx = -1;
                            //            nEndIdx = -1;
                            //        }
                            //    }

                            //string[] msgCode = sdata.Split(new string[] { "02", "03" }, StringSplitOptions.RemoveEmptyEntries);

                            //for (int nloop = 0; nloop < msgCode.Length; ++nloop)
                            //{
                            //    msgCode[nloop] = msgCode[nloop].Replace("-", "");
                            //    if (msgCode[nloop] != string.Empty)
                            //    {
                            //        if (msgCode[nloop].Length > 10)
                            //        {
                            //            OnReceiveData(IP, PORT, msgCode[nloop], data);
                            //        }
                            //    }
                            //}

                            _recvBuffer.Clear();
                            //}
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message + ex.StackTrace);
                    }
                }
                else
                {
                    connected = false;
                    break;
                }
                Thread.Sleep(150);
            }

            connected = false;

            clientSocket.Close();

            if (OnDisconnected != null)
            {
                OnDisconnected(this);
            }
        }