Exemple #1
0
 public void SetIndirectTextSignal(ushort signal, string text)
 {
     byte[] numArray = WebsocketUtil.EncodeMsg((byte)129, StringUtil.toByteArray("STRING[" + (object)signal + "," + text + "]"));
     if (this.server.ServerSocketStatus != SocketStatus.SOCKET_STATUS_CONNECTED)
     {
         return;
     }
     this.server.SendData(numArray, numArray.Length);
 }
Exemple #2
0
 public void SetAnalogSignal(ushort signal, ushort value)
 {
     byte[] numArray = WebsocketUtil.EncodeMsg((byte)129, StringUtil.toByteArray("LEVEL[" + (object)signal + "," + (object)value + "]"));
     if (this.server.ServerSocketStatus != SocketStatus.SOCKET_STATUS_CONNECTED)
     {
         return;
     }
     this.server.SendData(numArray, numArray.Length);
 }
Exemple #3
0
 public void SetDigitalSignal(ushort signal, ushort state)
 {
     byte[] numArray = state <= (ushort)0 ? WebsocketUtil.EncodeMsg((byte)129, StringUtil.toByteArray("OFF[" + (object)signal + "]")) : WebsocketUtil.EncodeMsg((byte)129, StringUtil.toByteArray("ON[" + (object)signal + "]"));
     if (this.server.ServerSocketStatus != SocketStatus.SOCKET_STATUS_CONNECTED)
     {
         return;
     }
     this.server.SendData(numArray, numArray.Length);
 }
Exemple #4
0
 public void ServerSendDataToEveryone(string dataToSend)
 {
     foreach (DictionaryEntry client in _clientList)
     {
         if (server.ClientConnected((client.Value as Connection).ClientIndex) && dataToSend != null && dataToSend.Length > 0)
         {
             byte[] numArray = WebsocketUtil.EncodeMsg((byte)129, StringUtil.toByteArray(dataToSend));
             server.SendDataAsync((client.Value as Connection).ClientIndex, numArray, numArray.Length, Server_SendDataCallback);
         }
     }
 }
Exemple #5
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));
            }
        }
Exemple #6
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));
            }
        }