Ejemplo n.º 1
0
        private void tcpServerReceiveCallback(TCPServer myTCPServer, uint clientIndex, int numberOfBytesReceived)
        {
            if (numberOfBytesReceived > 0)
            {
                this.myBuffer.Append(myTCPServer.GetIncomingDataBufferForSpecificClient(clientIndex), numberOfBytesReceived);
                if (!this.isOnline)
                {
                    if (this.myBuffer.IndexOf("\r\n\r\n") > 0)
                    {
                        string boundString = StringUtil.getBoundString(this.myBuffer.ToString(), "Sec-WebSocket-Key: ", "\r\n");
                        if (boundString != null && boundString.Length > 0)
                        {
                            byte[] numArray = WebsocketUtil.buildHandshakeMessage(boundString);
                            myTCPServer.SendData(clientIndex, numArray, numArray.Length);
                            this.myBuffer.Clear();
                            this.isOnline = true;
                        }
                        else
                        {
                            this.resetConnection(clientIndex);
                        }
                    }
                }
                else
                {
                    while (true)
                    {
                        uint num1 = WebsocketUtil.DecodeLength(this.myBuffer.ToArray());
                        if ((long)this.myBuffer.Length() >= (long)num1 && num1 != 0U)
                        {
                            byte[] array = this.myBuffer.ToArray(0, (int)num1);
                            this.myBuffer.Delete(0, (int)num1);
                            byte   num2      = array[0];
                            byte[] numArray1 = WebsocketUtil.DecodeMsg(array);
                            switch (num2)
                            {
                            case 129:
                                string msg = StringUtil.toString(numArray1);
                                if (RecieveMessage != null)
                                {
                                    RecieveMessage(msg);
                                }
                                CrestronConsole.PrintLine("Connection {0}, recieved [{1}]", ClientIndex, msg);
                                break;

                            case 136:
                                this.resetConnection(clientIndex);
                                break;

                            case 137:
                                byte[] numArray2 = WebsocketUtil.EncodeMsg((byte)138, numArray1);
                                myTCPServer.SendData(clientIndex, numArray2, numArray2.Length);
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                this._server.ReceiveDataAsync(clientIndex, new TCPServerReceiveCallback(tcpServerReceiveCallback));
            }
        }
Ejemplo n.º 2
0
        private void tcpServerReceiveCallback(
            TCPServer myTCPServer,
            uint clientIndex,
            int numberOfBytesReceived)
        {
            if (numberOfBytesReceived == 0)
            {
                this.resetConnection(clientIndex);
            }
            else
            {
                this.myBuffer.Append(myTCPServer.GetIncomingDataBufferForSpecificClient(clientIndex), numberOfBytesReceived);
                if (!this.isOnline)
                {
                    if (this.myBuffer.IndexOf("\r\n\r\n") > 0)
                    {
                        string boundString = StringUtil.getBoundString(this.myBuffer.ToString(), "Sec-WebSocket-Key: ", "\r\n");
                        if (boundString != null && boundString.Length > 0)
                        {
                            byte[] numArray = WebsocketUtil.buildHandshakeMessage(boundString);
                            myTCPServer.SendData(clientIndex, numArray, numArray.Length);
                            this.myBuffer.Clear();
                            this.isOnline = true;
                            if (this.OnClientConnectedChange != null)
                            {
                                this.OnClientConnectedChange((ushort)1);
                            }
                        }
                        else
                        {
                            this.resetConnection(clientIndex);
                        }
                    }
                }
                else
                {
                    while (true)
                    {
                        uint num1 = WebsocketUtil.DecodeLength(this.myBuffer.ToArray());
                        if ((long)this.myBuffer.Length() >= (long)num1 && num1 != 0U)
                        {
                            byte[] array = this.myBuffer.ToArray(0, (int)num1);
                            this.myBuffer.Delete(0, (int)num1);
                            byte   num2      = array[0];
                            byte[] numArray1 = WebsocketUtil.DecodeMsg(array);
                            switch (num2)
                            {
                            case 129:
                                string msg = StringUtil.toString(numArray1);
                                if (msg.StartsWith("PUSH"))
                                {
                                    ushort signal = ushort.Parse(StringUtil.getBoundString(msg, "[", "]"));
                                    if (this.OnDigitalSignalChange != null)
                                    {
                                        this.OnDigitalSignalChange(signal, (ushort)1);
                                        break;
                                    }
                                    break;
                                }
                                if (msg.StartsWith("RELEASE"))
                                {
                                    ushort signal = ushort.Parse(StringUtil.getBoundString(msg, "[", "]"));
                                    if (this.OnDigitalSignalChange != null)
                                    {
                                        this.OnDigitalSignalChange(signal, (ushort)0);
                                        break;
                                    }
                                    break;
                                }
                                if (msg.StartsWith("LEVEL"))
                                {
                                    ushort signal = ushort.Parse(StringUtil.getBoundString(msg, "[", ","));
                                    ushort num3   = ushort.Parse(StringUtil.getBoundString(msg, ",", "]"));
                                    if (this.OnAnalogSignalChange != null)
                                    {
                                        this.OnAnalogSignalChange(signal, num3);
                                        break;
                                    }
                                    break;
                                }
                                if (msg.StartsWith("STRING"))
                                {
                                    ushort signal      = ushort.Parse(StringUtil.getBoundString(msg, "[", ","));
                                    string boundString = StringUtil.getBoundString(msg, ",", "]");
                                    if (this.OnStringSignalChange != null)
                                    {
                                        this.OnStringSignalChange(signal, boundString);
                                    }
                                    break;
                                }
                                break;

                            case 136:
                                this.resetConnection(clientIndex);
                                break;

                            case 137:
                                byte[] numArray2 = WebsocketUtil.EncodeMsg((byte)138, numArray1);
                                myTCPServer.SendData(clientIndex, numArray2, numArray2.Length);
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                // ISSUE: method pointer
                this.server.ReceiveDataAsync(clientIndex, new TCPServerReceiveCallback(tcpServerReceiveCallback));
            }
        }