Exemple #1
0
        private void OnRecievedData(IAsyncResult ar)
        {
            // Socket was the passed in object
            Socket sock = (Socket)ar.AsyncState;

            // Check if we got any data

            bytesRecv += sock.EndReceive(ar);
            if (currentCommand == null && bytesRecv < 5)
            {
                sock.BeginReceive(readBuffer, bytesRecv, readBuffer.Length - bytesRecv, SocketFlags.None, OnRecievedData, sock);
                return;
            }
            else
            {
                if (currentCommand == null)
                {
                    var tagId = BitConverter.ToInt32(readBuffer, 1);
                    if (!pendingCommands.TryGetValue(tagId, out currentCommand))
                    {
                        throw new InvalidOperationException("Unexpected command response.");
                    }
                }
                if (bytesRecv - 1 >= currentCommand.Response.Length)
                {
                    var response  = currentCommand.Response;
                    var userState = currentCommand.UserState;
                    for (int count = 0; count < currentCommand.Response.Length; count++)
                    {
                        currentCommand.Response[count] = readBuffer[count + 1];
                    }
                    var tagId = BitConverter.ToInt32(readBuffer, 1);
                    lock (pendingCommands)
                    {
                        pendingCommands.Remove(tagId);
                    }
                    currentCommand = null;
                    OnCommandCompleted(new UbisenseCommandCompletedEventArgs(tagId, response, null, false, userState));
                    bytesRecv = 0;
                }
            }
            sock.BeginReceive(readBuffer, bytesRecv, readBuffer.Length - bytesRecv, SocketFlags.None, OnRecievedData, sock);
        }
Exemple #2
0
        public void CommandAsync(byte[] command, int responseSize, object userState)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            if (command.Length < 1)
            {
                throw new ArgumentException("Command bytes must not be empty.");
            }

            var code           = command[0];
            var tagId          = BitConverter.ToInt32(command, 1);
            var response       = new byte[responseSize];
            var pendingCommand = new UbisensePendingCommand(tagId, response, userState);

            lock (pendingCommands)
            {
                pendingCommands.Add(tagId, pendingCommand);
            }

            communicationSocket.Send(command, 0, command.Length, SocketFlags.None);
        }