示例#1
0
文件: IoTServer.cs 项目: x893/LoRaWAN
        private void SendTo(ServerState state, GatewayPacket packet)
        {
            if (state.State == SERVER_STATE.Running)
            {
                byte[]      data = null;
                PACKET_TYPE type = PACKET_TYPE.PKT_UNKNOWN;

                switch (packet.PacketType)
                {
                case PACKET_TYPE.PKT_PUSH_DATA:
                    data = new byte[4];
                    type = PACKET_TYPE.PKT_PUSH_ACK;
                    break;

                case PACKET_TYPE.PKT_PULL_DATA:
                    data = new byte[12];
                    packet.EUI.CopyTo(data, 4);
                    type = PACKET_TYPE.PKT_PULL_ACK;
                    break;

                default:
                    break;
                }

                if (type != PACKET_TYPE.PKT_UNKNOWN)
                {
                    data[0] = packet.Version;
                    BitConverter.GetBytes(packet.Token).CopyTo(data, 1);
                    data[3] = (byte)type;

                    try
                    {
                        state.Socket.BeginSendTo(data, 0, data.Length, SocketFlags.None, packet.Sender, new AsyncCallback(SendToCallback), state);
                    }
                    catch (Exception ex)
                    {
                        state.ErrorSendTo = ex;
                    }
                }
            }
        }
示例#2
0
文件: IoTServer.cs 项目: x893/LoRaWAN
        private void ReceiveFromCallback(IAsyncResult ar)
        {
            ServerState state = (ServerState)ar.AsyncState;

            if (state.State == SERVER_STATE.Running)
            {
                Socket socket = state.Socket;
                try
                {
                    // Read packet from gateway
                    IPEndPoint ipep      = new IPEndPoint(IPAddress.Any, 0);
                    EndPoint   epSender  = (EndPoint)ipep;
                    int        bytesRead = socket.EndReceiveFrom(ar, ref epSender);
                    byte[]     buffer    = state.Buffer;

                    // Check packet from gateway
                    if (bytesRead >= 4)
                    {
                        byte version = buffer[0];
                        if (version == 1 || version == 2)
                        {
                            GatewayPacket packet = new GatewayPacket();
                            packet.Version = version;
                            packet.Token   = BitConverter.ToUInt16(buffer, 1);

                            byte type = buffer[3];
                            switch (type)
                            {
                            case (byte)PACKET_TYPE.PKT_PUSH_DATA:
                                if (bytesRead > 12)
                                {
                                    packet.Json = Encoding.ASCII.GetString(buffer, 12, bytesRead - 12);
                                    packet.SetEUI(buffer);
                                }
                                else
                                {
                                    packet = null;
                                }
                                break;

                            case (byte)PACKET_TYPE.PKT_PULL_DATA:
                                if (bytesRead == 12)
                                {
                                    packet.SetEUI(buffer);
                                }
                                else
                                {
                                    packet = null;
                                }
                                break;

                            case (byte)PACKET_TYPE.PKT_TX_ACK:
                                if (bytesRead > 4)
                                {
                                    packet.Json = Encoding.ASCII.GetString(buffer, 4, bytesRead - 4);
                                }
                                else
                                {
                                    packet = null;
                                }
                                break;

                            default:
                                packet = null;
                                break;
                            }

                            if (packet != null)
                            {
                                packet.PacketType = (PACKET_TYPE)type;
                                packet.Sender     = epSender;

                                lock (state.LockPackets)
                                {
                                    if (state.Packets.Count < 100)
                                    {
                                        state.Packets.Enqueue(packet);
                                    }
                                    else
                                    {
                                        state.LostPackets++;
                                    }

                                    state.ResetEvent.Set();
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    state.ErrorReceiveFromCB = ex;
                }

                try
                {
                    IPEndPoint ipep     = new IPEndPoint(IPAddress.Any, 0);
                    EndPoint   epSender = (EndPoint)ipep;
                    socket.BeginReceiveFrom(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, ref epSender, new AsyncCallback(ReceiveFromCallback), state);
                }
                catch (Exception ex)
                {
                    state.ErrorReceiveFrom = ex;
                }
            }
        }