Example #1
0
        protected virtual void SendComplete(object sender, IOTaskCompleteEventArgs <SocketSendTask> e)
        {
            e.Task.Completed -= SendComplete;

            var msg = (IPacket)e.Task.UserToken;

            if (msg.Id == (byte)AresId.MSG_CHAT_SERVER_HTML)
            {
            }

            if (e.Task.Exception == null)
            {
                Monitor.AddOutput(e.Task.Transferred);
                try {
                    PacketSent?.Invoke(this, new PacketEventArgs(msg, MessageType, e.Task.Transferred));

                    if (IsConnected)
                    {
                        SendQueue();
                    }
                }
                catch (Exception ex) {
                    OnException(ex);
                }
            }
            else
            {
                OnException(e.Task.Exception);
            }
        }
Example #2
0
        /// <summary>
        /// Add a osc message to the send queue
        /// </summary>
        /// <param name="message">message to send</param>
        public void Send(OscPacket message)
        {
            if (State != OscSocketState.Connected)
            {
                return;
            }

            queueEmpty.Reset();

            if (sendQueue.Count >= messageBufferSize)
            {
                return;
            }

            sendQueue.Enqueue(message);

            if (sendQueue.Count != 1)
            {
                return;
            }

            int size = message.Write(buffer);

            if (Statistics != null)
            {
                message.IncrementSendStatistics(Statistics);

                Statistics.BytesSent.Increment(size);
            }

            PacketSent?.Invoke(message);

            Socket.BeginSend(buffer, 0, size, SocketFlags, Send_Callback, message);
        }
Example #3
0
        static public void ReportSend(HidReport report)
        {
            HidReport hr = report;

            hidDevice.WriteReport(hr, 1000000);

            // raise event
            PacketSent?.Invoke(hr);
        }
Example #4
0
        public void SendPacket(IPacket packet)
        {
            using (var ms = new MemoryStream())
            {
                serializer.Serialize(ms, packet);

                byte[] packetBytes = ms.ToArray();
                SendData(packetBytes);
            }

            PacketSent?.Invoke(this, EventArgs.Empty);
        }
Example #5
0
        private void Send_Callback(IAsyncResult ar)
        {
            bool shouldClose = false;

            try
            {
                SocketError error;

                Socket.EndSend(ar, out error);

                shouldClose = Socket.Connected == false;

                OscPacket packet;

                if (sendQueue.TryDequeue(out packet) == false)
                {
                    Debug.WriteLine("Could not dequeue packet");
                    return;
                }

                if (packet.IsSameInstance(ar.AsyncState as OscPacket) == false)
                {
                    Debug.WriteLine("Queue packet and async objects do not match");
                }

                if (sendQueue.TryPeek(out packet) && State == OscSocketState.Connected)
                {
                    int size = packet.Write(buffer);

                    PacketSent?.Invoke(packet);

                    Socket.BeginSend(buffer, 0, size, SocketFlags, Send_Callback, packet);
                }
                else
                {
                    queueEmpty.Set();
                }
            }
            catch
            {
                queueEmpty.Set();
            }
            finally
            {
                if (shouldClose == true)
                {
                    Dispose();
                }
            }
        }
Example #6
0
        static public void ReportSend(byte reportId, byte[] data)
        {
            HidReport hr;

            byte[] buffer = new byte[data.Length + 1];
            Array.ConstrainedCopy(data, 0, buffer, 1, data.Length);


            hr          = new HidReport(buffer.Length, new HidDeviceData(buffer, HidDeviceData.ReadStatus.Success));
            hr.ReportId = (byte)reportId;

            hidDevice.WriteReport(hr, 1000000);

            // raise event
            PacketSent?.Invoke(hr);
        }
Example #7
0
        private void SendLoop()
        {
            Try(() =>
            {
                while (!IsDisposed)
                {
                    var packet = Packets.Take();

                    Writer.Write((byte)0x02);
                    Writer.Write((byte)Registry.Register(packet.GetType()));
                    packet.WriteTo(Writer);
                    Writer.Write((byte)0x03);
                    Writer.Flush();

                    PacketSent?.Invoke(this, new PacketSentEventArgs(Connection, packet));
                }
            });
        }
Example #8
0
        private void SendComplete(object sender, IOTaskCompleteEventArgs <SocketSendTask> e)
        {
            e.Task.Completed -= sendHandler;

            if (e.Task.Exception == null)
            {
                Monitor.AddOutput(e.Task.Transferred);

                try {
                    var msg = (IPacket)e.Task.UserToken;
                    PacketSent?.Invoke(this, new PacketEventArgs(msg, WebSocketMessageType.Binary, e.Task.Transferred, e.Task.RemoteEndPoint));
                }
                catch (Exception ex) {
                    OnException(ex, e.Task.RemoteEndPoint);
                }
            }
            else
            {
                OnException(e.Task.Exception, e.Task.RemoteEndPoint);
            }
        }
Example #9
0
        public async Task <EnOceanPacket> SendTelegram(EnOceanTelegram telegram)
        {
            _stream.Pause();
            telegram.SetIdBase(_idBase.Span);

            var packet = telegram.ToPacket();

            PacketSent?.Invoke(this, new PacketSentEventArgs(packet, telegram));

            _stream.WriteFrame(packet);

            var p = await _stream.ReadFrame();

            if (p != null)
            {
                AnswerReceived?.Invoke(this, new AnswerReceviedEventArgs(p));
            }

            _stream.Continue();

            return(p);
        }
Example #10
0
        public virtual void SendPacket(string packet, bool session)
        {
            if (!_disconnectHandled && Connected)
            {
                try
                {
                    byte[] encrypted = Cryptography.Encrypt(packet, session);
                    Tcp.GetStream().Write(encrypted, 0, encrypted.Length);
                    PacketSent?.Invoke(packet);
                }
                catch (SocketException e)
                {
                    Error?.Invoke(e);

                    if (Tcp != null)
                    {
                        _logger.Error($"TCP state - {Tcp.GetState()}");
                    }

                    _logger.Error(e);
                    Disconnnect();
                }
                catch (IOException e)
                {
                    Error?.Invoke(e);

                    if (Tcp != null)
                    {
                        _logger.Error($"TCP state - {Tcp.GetState()}");
                    }

                    _logger.Error(e);
                    Disconnnect();
                }
            }
        }
Example #11
0
 internal override void WritePacket(byte[] packet)
 {
     _stream.Write(packet, 0, packet.Length);
     PacketSent?.Invoke(this, new McpPacket(packet));
 }
Example #12
0
 public virtual void OnPacketSent(object sender, EventArgs e)
 {
     PacketSent?.Invoke(sender, e);
 }