public SdcpResponse(SdcpResult result)
 {
     this.result = result;
 }
 public SdcpResponse(SdcpResult result, SdcpError error)
     : this(result)
 {
     this.error = error;
 }
Exemple #3
0
        public SdcpResponse Send(SdcpRequest request)
        {
            if (request == null)
            {
                throw new ArgumentException(Properties.Resources.MSG_OBJECT_NULL, nameof(request));
            }
            if (!this.client.Connected)
            {
                throw new InvalidOperationException(Properties.Resources.MSG_NOT_CONNECTED);
            }
            if (this.Closed)
            {
                throw new InvalidOperationException(Properties.Resources.MSG_CONNECTION_CLOSED);
            }

            if ((this.stream == null) || !this.stream.CanWrite || !this.stream.CanRead)
            {
                this.Closed = true;
                throw new InvalidOperationException(Properties.Resources.MSG_STREAM_ERROR);
            }

            byte[] buffer     = request.ToByteArray();
            byte[] readBuffer = new byte[READ_BUFFER_SIZE];

            // only one command is allowed to be sent at a time,
            // prevent any other threads from making simultaneous calls to send
            lock (this.lockObject) {
                this.stream.Write(buffer, 0, buffer.Length);

                int bytesRead = 0;

                // We need to wait until the projector has processed the command
                if (!this.waitHandle.WaitOne(this.commandDelay))
                {
                    // wait timed out without beig set, so read the data from the port
                    do
                    {
                        // note: technically this can overflow, but projector should never send more than ~150 bytes
                        bytesRead = this.stream.Read(readBuffer, bytesRead, readBuffer.Length - bytesRead);
                    } while (this.stream.DataAvailable);
                }
                else
                {
                    // wait handle was signaled, we're closing
                    return(new SdcpResponse(SdcpResult.ERROR, SdcpError.OtherCommError));
                }
            }

            SdcpResponse response;
            SdcpResult   result = (SdcpResult)readBuffer[6];

            if (result == SdcpResult.ERROR)
            {
                int len = readBuffer[9];
                if (len > 0)
                {
                    int error = (256 * readBuffer[10]) + readBuffer[11];
                    response = new SdcpResponse(result, (SdcpError)error);
                }
                else
                {
                    response = new SdcpResponse(result, SdcpError.UnknownResponse);
                }
            }
            else
            {
                response = new SdcpResponse(result);

                if (request.Request == RequestType.Get)
                {
                    int    len  = readBuffer[9];
                    byte[] data = new byte[len];

                    for (int i = 0; i < len; i++)
                    {
                        data[i] = readBuffer[10 + i];
                    }

                    response.Data = data;
                }
            }

            return(response);
        }