Beispiel #1
0
        public void Reply(byte[] data)
        {
            if (serverClosed)
            {
                throw new InvalidOperationException("The server is not open");
            }
            NetworkStream stream = clientSocket.GetStream();

            stream.Write(BitConverter.GetBytes(data.Length), 0, sizeof(int));
            replyBuffer = data;


            OnDataSent?.Invoke(this, new DataSentEventArgs(data, StringTerminator));
        }
Beispiel #2
0
        public override void Send(byte[] data)
        {
            try
            {
                client.SendTimeout = 100;
                if (!client.Connected)
                {
                    client.Connect(IP, m_port);
                }
                serverStream = client.GetStream();
            }
            catch (Exception ex)
            {
                throw new ConnectionErrorException("Could not connect to server, check if the device with ip " + IP + " is connected and the program is allowed through firewall", ex);
            }

            try
            {
                serverStream.WriteTimeout = 200;
                byte[] bytes = BitConverter.GetBytes(data.Length);
                System.Diagnostics.Debug.WriteLine(data.Length);
                serverStream.Write(new byte[] { 0xAA, 0xFF, bytes[0], bytes[1], bytes[2], bytes[3] }, 0, sizeof(int) + 2);
                byte[] response = new byte[2];
                serverStream.Read(response, 0, 2);
                if (response[0] == 'o' && response[1] == 'k')
                {
                    serverStream.Write(data, 0, data.Length);
                    Connected = true;
                    OnDataSent?.Invoke(this, new DataSentEventArgs(data, StringTerminator));
                    if (!replyWaitStarted) //TODO: Do something CPU usage of this
                    {
                        //ThreadPool.QueueUserWorkItem(WaitForReply, serverStream);
                        //replyWaitStarted = true;
                    }
                }
                else
                {
                    throw new CommunicationException("Invalid response recieved from server");
                }
            }
            catch (System.IO.IOException ex)
            {
                serverStream.Close();
                serverStream.Dispose();
                client.Close();
                Connected = false;
                throw new Exception("Server was closed", ex);
            }
        }
Beispiel #3
0
 public void DataSent(int size)
 {
     SendBandwidth.Measure(size);
     OnDataSent?.Invoke(size);
 }
Beispiel #4
0
        private void setup()
        {
            if (server.streamId == null)
            {
                server.createNewStream((success, data) =>
                {
                    if (!success)
                    {
                        OnError?.Invoke(this, new SpeckleEventArgs("Failed to create stream."));
                        return;
                    }
                    server.streamId = data.streamId;
                    setupWebsocket();
                });
            }
            else
            {
                server.getStream((success, data) =>
                {
                    if (!success)
                    {
                        OnError?.Invoke(this, new SpeckleEventArgs("Failed to retrieve stream."));
                        return;
                    }
                    setupWebsocket();
                });
            }

            // start the is ready checker timer.
            // "ready" is defined as:
            // a) we have a valid stream id (means we've contacted the server sucessfully)
            // && b) we have a live wsSessionId (means sockets were correctly initialised)

            isReadyCheck           = new System.Timers.Timer(150);
            isReadyCheck.AutoReset = false; isReadyCheck.Enabled = true;
            isReadyCheck.Elapsed  += (sender, e) =>
            {
                if (server.streamId == null)
                {
                    isReadyCheck.Start(); return;
                }
                if (server.wsSessionId == null)
                {
                    isReadyCheck.Start(); return;
                }

                dynamic data = new ExpandoObject();
                data.streamId    = server.streamId;
                data.wsSessionId = server.wsSessionId;

                OnReady?.Invoke(this, new SpeckleEventArgs("sender ready", data));
            };

            // data sender debouncer
            DataSender           = new System.Timers.Timer(dataDebounceInterval);
            DataSender.AutoReset = false; DataSender.Enabled = false;
            DataSender.Elapsed  +=
                (sender, e) =>
            {
                Debug.WriteLine("SPKSENDER: Sending data payload.");

                dynamic x = new ExpandoObject();
                x.objects          = converter.convert(this.objects);
                x.objectProperties = converter.getObjectProperties(this.objects);
                x.layers           = layers;
                x.streamName       = name;

                server.updateStream(x as ExpandoObject, (success, data) =>
                {
                    if (!success)
                    {
                        OnError?.Invoke(this, new SpeckleEventArgs("Failed to update stream."));
                        return;
                    }

                    converter.commitCache();
                    OnDataSent?.Invoke(this, new SpeckleEventArgs("Stream was updated."));
                });
                DataSender.Stop();
            };

            // metadata sender debouncer
            MetadataSender           = new System.Timers.Timer(metaDebounceInterval);
            MetadataSender.AutoReset = false; MetadataSender.Enabled = false;
            MetadataSender.Elapsed  += (sender, e) =>
            {
                Debug.WriteLine("SPKSENDER: Sending meta payload.");

                dynamic payload = new ExpandoObject();
                payload.layers     = layers;
                payload.streamName = name;

                server.updateStreamMetadata(payload as ExpandoObject, (success, response) =>
                {
                    if (!success)
                    {
                        OnError?.Invoke(this, new SpeckleEventArgs("Failed to update stream metadata."));
                        return;
                    }
                    OnDataSent?.Invoke(this, new SpeckleEventArgs("Stream metadata was updated."));
                });
            };
        }
Beispiel #5
0
 public void TriggerClientDataReceived(DataEventArgs e)
 {
     OnDataSent?.Invoke(e);
 }
Beispiel #6
0
 public void ClearSentBuffer()
 {
     dataSent.Clear();
     OnDataSent?.Invoke(this, new EventArgs());
 }
Beispiel #7
0
        private void Connection_OnJsonDataSend(object sender, JsonObject e)
        {
//            dataSent.Insert(0, e.Stringify() + Environment.NewLine);
            dataSent.Insert(0, e.PrettyPrint() + Environment.NewLine);
            OnDataSent?.Invoke(this, new EventArgs());
        }