Exemple #1
0
        public WiFiDeviceGateway(string ovrDeviceHostname, bool scentActivationLogging = false, int localhostDebugPort = 0)
        {
            _socketSender      = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            _ovrDeviceHostname = ovrDeviceHostname;
            _socketRecieveState.ovrHostname = _ovrDeviceHostname;
            _socketRecieveState.udpClient   = new UdpClient(0, AddressFamily.InterNetwork);

            var localIp = IPAddressExtensions.GetLocalIP();

            UnityEngine.Debug.LogFormat("<b>[OVR]</b> Local IP: {0}", localIp);

            if (scentActivationLogging)
            {
                _scentActivationLog = new NetworkLogging(localhostDebugPort);
            }

            _socketRecievePort = ((IPEndPoint)_socketRecieveState.udpClient.Client.LocalEndPoint).Port;

            // Setup for broadcasting hostname request from device
            MarshalHeader.Set(ref _checkConnectionPacket, MessageTypes.DEVICE_HOSTNAME_REQUEST, sizeof(ushort));
            _checkConnectionPacket[MarshalHeader.Size]     = (byte)_socketRecievePort;
            _checkConnectionPacket[MarshalHeader.Size + 1] = (byte)(_socketRecievePort >> 8);
            _onConnect    += OnConnect;
            _onDisconnect += OnDisconnect;
        }
Exemple #2
0
        public static void ReceiveCallback(IAsyncResult ar)
        {
            UdpState udpState = (UdpState)(ar.AsyncState);

            var data = udpState.udpClient.EndReceive(ar, ref udpState.endPoint);

            _header.Set(data);
            if (!_header.Valid)
            {
                return;
            }

            if (_header.MessageType == (byte)MessageTypes.DEVICE_HOSTNAME)
            {
                var result = Encoding.Default.GetString(data, MarshalHeader.Size, _header.MessageSize);
                if (result == udpState.ovrHostname)
                {
                    _connectionTimer.Restart();
                    if (_currentDeviceEndPoint == null)
                    {
                        UnityEngine.Debug.LogFormat("<b>[OVR]</b> Device Found: {0}", result);
                        _onConnect.Invoke(udpState.endPoint);
                    }
                }
                else
                {
                    // UnityEngine.Debug.LogFormat("<b>[OVR]</b> Rejected Device Found: {0}. Does not match target hostname.", result);
                }
            }
            else if (_header.MessageType == (byte)MessageTypes.DEVICE_DEBUGMODE)
            {
                DeviceDebugMode = (data[MarshalHeader.Size] > 0);
            }
        }
Exemple #3
0
 public void OnConnect(IPEndPoint ip)
 {
     lock (_asyncLock)
     {
         _currentDeviceEndPoint = ip;
         MarshalHeader.Set(ref _packet, MessageTypes.DEVICE_DEBUGMODE_REQUEST);
         _socketSender.SendTo(_packet, MarshalHeader.Size, SocketFlags.None, _currentDeviceEndPoint);
     }
 }
Exemple #4
0
        public byte[] CommandsToPacket()
        {
            int index = MarshalHeader.Size;

            MarshalHeader.Set(ref _packet, MessageTypes.ODORANT_COMMANDS, (ushort)(_commands.Count * OdorantCommand.Size));

            foreach (var command in _commands)
            {
                Array.Copy(command.GetPacket(), 0, _packet, index, OdorantCommand.Size);
                index += OdorantCommand.Size;
            }

            return(_packet);
        }