public void ProcessData(StreamFrame frame) { // Do not accept data if the stream is reset. if (State == StreamState.ResetRecvd) { return; } byte[] data = frame.StreamData; if (frame.Offset != null) { _data.Add(frame.Offset.Value, frame.StreamData); } else { // TODO: Careful with duplicate 0 offset packets on the same stream. Probably PROTOCOL_VIOLATION? _data.Add(0, frame.StreamData); } // Either this frame marks the end of the stream, // or fin frame came before the data frames if (frame.EndOfStream) { State = StreamState.SizeKnown; } _currentTransferRate += (UInt64)data.Length; // Terminate connection if maximum stream data is reached if (_currentTransferRate >= _maximumStreamData) { ShortHeaderPacket errorPacket = _connection.PacketCreator.CreateConnectionClosePacket(Infrastructure.ErrorCode.FLOW_CONTROL_ERROR, frame.ActualType, ErrorConstants.MaxDataTransfer); _connection.SendData(errorPacket); _connection.TerminateConnection(); return; } if (State == StreamState.SizeKnown && IsStreamFull()) { State = StreamState.DataRecvd; OnStreamDataReceived?.Invoke(this, Data); } }
private void websocket_MessageReceived(object sender, MessageReceivedEventArgs e) { try { var obj = Newtonsoft.Json.JsonConvert.DeserializeObject(e.Message, typeof(Cortex.Response.General)) as Cortex.Response.General; if (obj.error != null) { string add = ""; if (Request2Method.ContainsKey(obj.id)) { add = Request2Method[obj.id] + ": "; } OnError?.Invoke(this, add + obj.error.message); return; } if (Request2Method.ContainsKey(obj.id)) { switch (Request2Method[obj.id]) { case Cortex.Request.Subscribe.Method: subscription = JsonConvert.DeserializeObject(e.Message, typeof(Cortex.Response.Subscribe)) as Cortex.Response.Subscribe; OnMessage?.Invoke(this, $"Subscription processed.. receiving data for {string.Join(",", subscription.result?.success?.Select(s => s.streamName))}"); foreach (var item in subscription.result?.success) { SubsriptionDetails[item.streamName] = item; } break; case Cortex.Request.CreateSession.Method: session = JsonConvert.DeserializeObject(e.Message, typeof(Cortex.Response.CreateSession)) as Cortex.Response.CreateSession; var subscribeRequest = new Cortex.Request.Subscribe(); subscribeRequest.__params__.cortexToken = auth.result.cortexToken; subscribeRequest.__params__.session = session.result.id; subscribeRequest.__params__.streams = new string[] { "eeg", "mot", "dev", "pow", "met", "com", "fac", "sys" }; SendReqest(subscribeRequest); break; case Cortex.Request.QueryHeadsets.Method: devices = JsonConvert.DeserializeObject(e.Message, typeof(Cortex.Response.QueryHeadsets)) as Cortex.Response.QueryHeadsets; if (devices.result.Length > 0) { OnDeviceChange?.Invoke(this, new Struct.Device() { Manufacturer = "Emotiv", Model = devices.result[0].id + "-" + devices.result[0].firmware, Uid = devices.result[0].id, }); } OnMessage?.Invoke(this, "Hadsets information received"); if (!string.IsNullOrEmpty(auth.result?.cortexToken)) { OnMessage?.Invoke(this, "Creating session..."); var request = new Cortex.Request.CreateSession(); request.__params__.cortexToken = auth.result.cortexToken; request.__params__.headset = devices.result.FirstOrDefault()?.id; SendReqest(request); } break; case Cortex.Request.Authorize.Method: auth = JsonConvert.DeserializeObject(e.Message, typeof(Cortex.Response.Authorize)) as Cortex.Response.Authorize; OnMessage?.Invoke(this, "Authorized"); if (!string.IsNullOrEmpty(auth.result?.cortexToken)) { OnMessage?.Invoke(this, "Getting headset information..."); SendReqest(new Cortex.Request.QueryHeadsets()); } break; case Cortex.Request.GetUserLogin.Method: OnMessage?.Invoke(this, "Received user information"); user = JsonConvert.DeserializeObject(e.Message, typeof(Cortex.Response.GetUserLogin)) as Cortex.Response.GetUserLogin; break; case Cortex.Request.RequestAccess.Method: access = JsonConvert.DeserializeObject(e.Message, typeof(Cortex.Response.RequestAccess)) as Cortex.Response.RequestAccess; OnMessage?.Invoke(this, "Request Access Message received"); if (access.result?.accessGranted == true) { OnMessage?.Invoke(this, "Access granted. Going to authorize..."); var request = new Cortex.Request.Authorize(); request.__params__.clientId = clientId; request.__params__.clientSecret = clientSecret; SendReqest(request); } else { if (!string.IsNullOrEmpty(access.result.message)) { OnError?.Invoke(this, "Access not granted: " + access.result.message); } else { OnError?.Invoke(this, "Access not granted. No access data received"); } } break; } } else { // received the subscription data var data = JsonConvert.DeserializeObject(e.Message, typeof(Cortex.Response.StreamData)) as Cortex.Response.StreamData; try { Dictionary <string, Dictionary <string, object> > ret = ProcessToStringValue(data); OnStreamDataReceived?.Invoke(null, ret); } catch (Exception exc) { OnError?.Invoke(this, exc.Message); } } //var response = JsonUtility.FromJson<Cortex.Response.General>(reply); lastMessageReceived = e.Message; messageReceiveEvent.Set(); } catch (Exception exc) { OnError?.Invoke(this, exc.Message); } }