Esempio n. 1
0
        public BrainConnector(TCPComm.TCPConnector _conn)
        {
            connector           = _conn;
            connector.newError += (o1, err) => {
                g_NewNetworkError(this, err);
            };
            connector.newRawMessage += (o, str) =>
            {
                if (str == "")
                {
                    g_DeviceClosed?.Invoke(this, null);
                    connector.Dispose();
                    return;
                }

                var msgs = TCPComm.Utils.SplitIntoMessages(str);
                foreach (var raw in msgs)
                {
                    var msg = ZeusMessage.fromString(raw);
                    if (msg.Token != NetUtils.Token.Token)
                    {
                        g_WrongToken.Invoke(this, msg);
                    }
                    else
                    {
                        switch (msg.Order)
                        {
                        case messageKinds.heartbeat:
                            break;

                        case messageKinds.present:
                            g_Present?.Invoke(this, msg);
                            break;

                        case messageKinds.showYourWindow:
                            g_ShowSoftwareWindow?.Invoke(this, EventArgs.Empty);
                            break;

                        case messageKinds.config_ok:
                            g_ConfigOk?.Invoke(this, msg);
                            break;

                        case messageKinds.deviceClosed:
                            g_DeviceClosed?.Invoke(this, msg);
                            connector.Dispose();
                            break;

                        case messageKinds.majorErrorInDevice:
                            g_NewErrorFromDevice?.Invoke(this, msg);
                            break;

                        default:
                            throw new Exception($"Unnexpected Order: [{msg.Order}] received from device [{this.remoteName}]");
                        }
                    }
                }
            };
        }
Esempio n. 2
0
        public BrainConnector(TCPComm.TCPConnector _conn)
        {
            connector           = _conn;
            connector.newError += (o1, err) => {
                g_NewNetworkError(this, err);
            };
            connector.newRawMessage += (o, str) =>
            {
                if (str == "")
                {
                    g_DeviceClosed?.Invoke(this, null);

                    connector.Dispose();
                    return;
                }

                var msgs = TCPComm.Utils.SplitIntoMessages(str);
                foreach (var raw in msgs)
                {
                    try
                    {
                        var msg = BrainMessage.fromString(raw);
                        if (msg.Token != NetUtils.Token.Token)
                        {
                            g_WrongToken.Invoke(this, msg);
                        }
                        else
                        {
                            switch (msg.Order)
                            {
                            case messageKinds.heartbeat:
                                break;

                            case messageKinds.present:
                                g_Present?.Invoke(this, msg);
                                break;

                            case messageKinds.update:
                                g_Update(this, msg.Params);
                                break;

                            case messageKinds.deviceClosed:
                                g_DeviceClosed?.Invoke(this, msg);
                                connector.Dispose();
                                break;

                            case messageKinds.majorErrorInDevice:
                                g_NewErrorFromDevice?.Invoke(this, msg);
                                break;

                            default:
                                throw new Exception($"Unnexpected Order: [{msg.Order}] received from device [{this.remoteName}]");
                            }
                        }
                    }catch (Exception e)
                    {
                        throw e;
                    }
                }
            };
        }