Beispiel #1
0
 private void proccessCommonValues(DataHoldingUserToken userToken, string receivedData, int startIndex)
 {
     userToken.uImageHolder.IMEI                = receivedData.Substring(startIndex + 2, 15);
     userToken.uImageHolder.PictureNumber       = Convert.ToInt32(receivedData.Substring(startIndex + 17, 5));
     userToken.uImageHolder.TotalPackages       = Convert.ToInt32(receivedData.Substring(startIndex + 22, 3));
     userToken.uImageHolder.LastPackageSequence = Convert.ToInt32(receivedData.Substring(startIndex + 25, 3));
 }
        public int handleMessage(SocketAsyncEventArgs saea, DataHoldingUserToken userToken, int bytesToProcess, out byte[] readyMessage)
        {
            if (userToken.messageBytesDoneCount == 0)
            {
                userToken.messageBytes = new byte[userToken.messageLength];
            }

            int length = Math.Min(userToken.messageLength - userToken.messageBytesDoneCount, bytesToProcess);

            Buffer.BlockCopy(saea.Buffer, saea.Offset + userToken.bytesDoneThisOp,
                             userToken.messageBytes, userToken.messageBytesDoneCount, length);

            userToken.messageBytesDoneCount += length;
            userToken.bytesDoneThisOp       += length;

            readyMessage = null;
            if (userToken.messageBytesDoneCount == userToken.messageLength)
            {
                //// сообщение готово
                readyMessage = new byte[userToken.prefixBytes.Length + userToken.messageBytes.Length];
                Buffer.BlockCopy(userToken.prefixBytes, 0, readyMessage, 0, userToken.prefixBytes.Length);
                Buffer.BlockCopy(userToken.messageBytes, 0, readyMessage, userToken.prefixBytes.Length, userToken.messageBytes.Length);
            }
            //return bytesToProcess - length;
            return(0);
        }
Beispiel #3
0
        private void saveImageToDisc(DataHoldingUserToken userToken)
        {
            string     fileName      = userToken.uImageHolder.IMEI + DateTime.UtcNow.ToString("yyyy_MM_dd_HH_mm") + ".jpg";
            string     imageFilePath = Path.Combine(Settings.Default.ImageSaveDirectory, fileName);
            FileStream fs            = new FileStream(imageFilePath, FileMode.Create);

            for (int i = 0; i < userToken.uImageHolder.ImageBytes.Length; i++)
            {
                fs.Write(userToken.uImageHolder.ImageBytes[i], 0, userToken.uImageHolder.ImageBytes[i].Length);
            }
            fs.Close();
        }
        public int handle(SocketAsyncEventArgs saea, DataHoldingUserToken userToken, out byte[] readyMessage)
        {
            readyMessage = new byte[userToken.prefixBytes.Length + saea.BytesTransferred - userToken.bytesDoneThisOp];
            Buffer.BlockCopy(userToken.prefixBytes, 0, readyMessage, 0, userToken.prefixBytes.Length);
            Buffer.BlockCopy(saea.Buffer, userToken.bytesDoneThisOp, readyMessage,
                             userToken.prefixBytes.Length, saea.BytesTransferred - userToken.bytesDoneThisOp);

            userToken.bytesDoneThisOp = saea.BytesTransferred;
            userToken.messageBytes    = readyMessage;
            userToken.messageLength   = readyMessage.Length;

            return(0);
        }
Beispiel #5
0
        public void defineTypeData(SocketAsyncEventArgs saea, DataHoldingUserToken userToken)
        {
            var prefix = Encoding.ASCII.GetString(userToken.prefixBytes);

            if (prefix.StartsWith("$$"))
            {
                // это пакет, проверяем его, если он нормальный устанавливаем длину ожидаемого сообщения
                try
                {
                    userToken.messageLength = Convert.ToInt32(prefix.Substring(2), 16) - 4;
                    if (userToken.messageLength > 0)
                    {
                        userToken.dataTypeId = 1;
                    }
                }
                catch
                {
                    log.WarnFormat("Someone sended us a bad packet size prefix={0} his IP={1}", prefix,
                                   ((IPEndPoint)saea.AcceptSocket.RemoteEndPoint).Address);
                }
            }
            else if (prefix.StartsWith("Rece"))
            {
                // это ответ на команду
                // например Receive:'015'ok*000000,015,0,195.206.252.247,40181#
                userToken.dataTypeId = 2;
            }
            else if (prefix.StartsWith("AT+C"))
            {
                userToken.dataTypeId = 3;
            }
            else if (prefix.StartsWith("IMEI"))
            {
                userToken.dataTypeId = 4;
            }
            else if (prefix.StartsWith("$U"))
            {
                userToken.dataTypeId = 7;
            }
            else
            {
                userToken.dataTypeId = 5;
                // непонятно что прислали
                log.WarnFormat("Someone sended us a bad packet with prefix={0} his IP={1}", prefix,
                               ((IPEndPoint)saea.AcceptSocket.RemoteEndPoint).Address);
            }
        }
Beispiel #6
0
        public void clearFromCRLF(SocketAsyncEventArgs saea, DataHoldingUserToken userToken)
        {
            // допустим что это старт, начало новой команды или новой посылки от блока

            while (saea.BytesTransferred > userToken.bytesDoneThisOp)
            {
                if (saea.Buffer[userToken.bytesDoneThisOp] == 0x0D ||
                    saea.Buffer[userToken.bytesDoneThisOp] == 0x0A)
                {
                    userToken.bytesDoneThisOp++;
                }
                else
                {
                    break;
                }
            }
        }
Beispiel #7
0
        public int handleResponse(SocketAsyncEventArgs saea, DataHoldingUserToken userToken, out byte[] readyMessage)
        {
            readyMessage = null;
            int cnt = 0;

            while (saea.BytesTransferred > (userToken.bytesDoneThisOp + cnt))
            {
                if (saea.Buffer[userToken.bytesDoneThisOp + cnt] == 0x23)
                {
                    cnt++;
                    break;
                }
                cnt++;

                if (cnt > 200)
                {
                    // гипотетически ответ не больше 200 байт, нужно закрыть сокет ином случае
                    return(-1);
                }
            }

            byte[] message = new byte[cnt + userToken.messageBytesDoneCount];

            if (userToken.messageBytesDoneCount != 0)
            {
                Buffer.BlockCopy(userToken.messageBytes, 0, message, 0, userToken.messageBytesDoneCount);
            }
            Buffer.BlockCopy(saea.Buffer, userToken.bytesDoneThisOp, message, userToken.messageBytesDoneCount, cnt);
            userToken.messageBytes = message;

            userToken.messageBytesDoneCount += cnt;
            userToken.bytesDoneThisOp       += cnt;

            if (userToken.messageBytes[userToken.messageBytes.Length - 1] == 0x23)
            {
                readyMessage = new byte[userToken.prefixBytes.Length + userToken.messageBytes.Length];
                Buffer.BlockCopy(userToken.prefixBytes, 0, readyMessage, 0, userToken.prefixBytes.Length);
                Buffer.BlockCopy(userToken.messageBytes, 0, readyMessage, userToken.prefixBytes.Length, userToken.messageBytes.Length);
            }

            return(0);
        }
Beispiel #8
0
        public int handlePrefix(SocketAsyncEventArgs rs, DataHoldingUserToken userToken, int bytesToProcess)
        {
            if (bytesToProcess == 0 || userToken.prefixBytesDoneCount >= RECEIVE_PREFIX_LENGTH)
            {
                return(bytesToProcess);
            }

            if (userToken.prefixBytesDoneCount == 0)
            {
                userToken.prefixBytes = new byte[RECEIVE_PREFIX_LENGTH];
            }

            int length = Math.Min(RECEIVE_PREFIX_LENGTH - userToken.prefixBytesDoneCount, bytesToProcess);

            Buffer.BlockCopy(rs.Buffer, rs.Offset + userToken.bytesDoneThisOp,
                             userToken.prefixBytes, userToken.prefixBytesDoneCount, length);

            userToken.prefixBytesDoneCount += length;
            userToken.bytesDoneThisOp      += length;


            return(bytesToProcess - length);
        }
Beispiel #9
0
        public int handle(SocketAsyncEventArgs saea, DataHoldingUserToken userToken, out byte[] readyMessage)
        {
            readyMessage = null;

            int cnt = 0;

            while (saea.BytesTransferred > (userToken.bytesDoneThisOp + cnt))
            {
                cnt++;
                if (saea.Buffer[userToken.bytesDoneThisOp + cnt - 1] == 0x23)
                {
                    break;
                }
            }

            log.DebugFormat("Photo packet cnt={0}", cnt);

            byte[] message = new byte[cnt + userToken.messageBytesDoneCount];
            if (userToken.messageBytesDoneCount != 0)
            {
                Buffer.BlockCopy(userToken.messageBytes, 0, message, 0, userToken.messageBytesDoneCount);
            }
            Buffer.BlockCopy(saea.Buffer, userToken.bytesDoneThisOp, message, userToken.messageBytesDoneCount, cnt);
            userToken.messageBytes           = message;
            userToken.messageBytesDoneCount += cnt;
            userToken.bytesDoneThisOp       += cnt;

            if (userToken.messageBytes[userToken.messageBytes.Length - 1] == 0x23)
            {
                message = new byte[2 + userToken.messageBytes.Length];
                Buffer.BlockCopy(userToken.prefixBytes, 2, message, 0, 2);
                Buffer.BlockCopy(userToken.messageBytes, 0, message, 2, userToken.messageBytes.Length);
                userToken.resetReadyMessage();

                string receivedData = Encoding.ASCII.GetString(message, 0, message.Length - 1);
                log.DebugFormat("Photo packet={0}", receivedData);

                int indexOfImageBytes   = 26;
                int indexOfStartPackage = 0;

                proccessCommonValues(userToken, receivedData, indexOfStartPackage - 2);
                if (userToken.uImageHolder.LastPackageSequence == 1)
                {
                    userToken.uImageHolder.ImageBytes = new byte[userToken.uImageHolder.TotalPackages][];
                }

                if (userToken.uImageHolder.ImageBytes == null)
                {
                    return(0);
                }

                userToken.uImageHolder.ImageBytes[userToken.uImageHolder.LastPackageSequence - 1] = StringToByteArray(receivedData.Substring(indexOfImageBytes));

                if (userToken.uImageHolder.LastPackageSequence == userToken.uImageHolder.TotalPackages)
                {
                    // 1) save image to file
                    saveImageToDisc(userToken);

                    // 2) reset imageHolder
                    userToken.uImageHolder = new UImageHolder();
                }
            }

            return(0);
        }