示例#1
0
        /// <summary>
        /// Invoke the packet-associated method and send response packets contained in MethodResponse</summary>
        /// <param name="Packet">The incoming packet</param>
        /// <param name="conn">The parsed packet</param>
        /// </summary>
        private void HandlePacket(Packet Packet, Connection conn)
        {
            Core.GetLogging().WriteLine("Received Packet: " + Packet.GetPacketString(), LogLevel.Debug);
            //Get associated Packet method using packet header/name
            Method Method = Core.GetMethodByName(Packet.Header.ToLower());

            if (Method != null)
            {
                //Packet body values count must match with method parameters count
                if (Method.GetParametersCount() != Packet.bodyValues.Count)
                {
                    Core.GetLogging().WriteLine("Method: " + Method.GetName() + " has " + Method.GetParametersCount() + " params but client request has " + Packet.bodyValues.Count + " params", LogLevel.Error);
                }
                else
                {
                    MethodResponse result = null;
                    try
                    {
                        //Try invoke associated method given packet body values as parameters
                        result = (MethodResponse)Method.GetMethodInfo().Invoke(Core.dllInstance, Packet.bodyValues.ToArray());
                    }
                    catch (Exception e)
                    {
                        Core.GetLogging().WriteLine("Error handling Method: " + Method.GetName() + " Exception Message: " + e.Message, LogLevel.Error);
                    }
                    if (result != null)
                    {
                        Core.GetLogging().WriteLine("Handled Method: " + Method.GetName() + ". Sending response..", LogLevel.Information);

                        //Invoke succeed! now read Packets contained in MethodResponse and send them to the specified clients
                        foreach (Packet PacketToSend in result.Packets)
                        {
                            string PacketString = PacketToSend.GetPacketString();
                            if (PacketToSend.sendToAll) //Send to all clients
                            {
                                sendToAll(StrToByteArray(PacketString));
                                Core.GetLogging().WriteLine("Sent response: " + PacketString + " to all clients", LogLevel.Debug);
                            }
                            else if (PacketToSend.sendTo != null) //Only send to clients specified in a list
                            {
                                foreach (int connID in PacketToSend.sendTo)
                                {
                                    Send(StrToByteArray(PacketString), _sockets[connID]);
                                    Core.GetLogging().WriteLine("Sent response: " + PacketString + " to client id: " + connID, LogLevel.Debug);
                                }
                            }
                            else //Send to sender
                            {
                                Send(StrToByteArray(PacketString), conn);
                                Core.GetLogging().WriteLine("Sent response: " + PacketString + " to client id: " + conn.connID, LogLevel.Debug);
                            }
                        }
                    }
                }
            }
            else
            {
                Core.GetLogging().WriteLine("Invoked Method: " + Packet.Header + " does not exist", LogLevel.Error);
            }
        }
示例#2
0
        public void Step()
        {
            // Make sure the input is set to something.
            if (!state.input.HasValue)
            {
                if (Idle)
                {
                    state.input = -1;
                }
                else
                {
                    var packet = queue.Peek();
                    if (packet.SentX)
                    {
                        queue.Dequeue();
                        state.input = packet.Y;
                    }
                    else
                    {
                        packet.SentX = true;
                        state.input  = packet.X;
                    }
                }
            }

            if (!Intcode.Step(state))
            {
                throw new Exception("oops?");
            }

            var output = state.PopOutput();

            if (output.HasValue)
            {
                if (toSend == null)
                {
                    toSend             = new PacketToSend();
                    toSend.Destination = output.Value;
                }
                else if (!toSend.ToSend.SentX)
                {
                    toSend.ToSend.X     = output.Value;
                    toSend.ToSend.SentX = true;
                }
                else
                {
                    // Send it
                    toSend.ToSend.Y     = output.Value;
                    toSend.ToSend.SentX = false;
                    packetSender(toSend);
                    toSend = null;
                }
            }
        }
示例#3
0
        private IOperation SendSetup()
        {
            IOperation operation = new SendOperation();

            operation.PacketToSend += (s, e) => PacketToSend?.Invoke(this, e);
            if (_context.Data == null || _context.Data.Count == 0)
            {
                throw new XModemProtocolException(new AbortedEventArgs(XModemAbortReason.BufferEmpty));
            }
            return(operation);
        }
示例#4
0
        public async Task <ActionResult <PacketToSend> > GetTech(string przewod)
        {
            var wiazka = (await _db.Technical.FirstOrDefaultAsync(c => c.PrzewodCiety == przewod)).Wiazka;
            var result = await _db.Technical.Where(c => c.Wiazka == wiazka).Select(c => c.BIN).Distinct().ToListAsync();

            PacketToSend pts = new PacketToSend
            {
                StatusCode = 1,
            };

            if (result == null)
            {
                pts.StatusCode = 0;
                return(pts);
            }

            pts.BIN = result;

            return(pts);
        }
示例#5
0
 protected void FirePacketToSendEvent(object sender, PacketToSendEventArgs args)
 {
     PacketToSend?.Invoke(sender, args);
 }
示例#6
0
 protected virtual void FirePacketToSendEvent(int packetNumber, List <byte> packet)
 {
     PacketToSend?.Invoke(this, new PacketToSendEventArgs(packetNumber, packet));
 }