/// <summary>
            /// 构造函数 -- 封装数据
            /// </summary>
            /// <param name="eCategory">任务类别</param>
            /// <param name="eService">任务键名</param>
            public PacketHead(TaskCategory eCategory, TaskService eService)
            {
                this._eCategory = eCategory;
                this._eService  = eService;

                this.iPacketID = PacketID.Instance().CurrentID;
            }
Esempio n. 2
0
        private bool TryGetPacketHandler(PacketID id, out VerifiedPacketHandler handler)
        {
            switch (NetworkManager.instance.packetIDType)
            {
            case PacketIDType.STRING:
                if (defaultPacketHandlers_string.TryGetValue(id.string_ID, out handler))
                {
                    return(true);
                }
                else if (packetHandlers_string.TryGetValue(id.string_ID, out handler))
                {
                    return(true);
                }
                return(false);

            case PacketIDType.SHORT:
                if (defaultPacketHandlers_short.TryGetValue(id.short_ID, out handler))
                {
                    return(true);
                }
                else if (packetHandlers_short.TryGetValue(id.short_ID, out handler))
                {
                    return(true);
                }
                return(false);
            }

            handler = new VerifiedPacketHandler();
            return(false);
        }
Esempio n. 3
0
        public Packet ReceivePacket(PacketID pid)
        {
            byte[] buff = new byte[0x10000];
            while (true)
            {
                if (cliSkt.ReceiveBufferSize > 0)
                {
                    try
                    {
                        cliSkt.Receive(buff);
                        using (var rdr = new NReader(new MemoryStream(buff)))
                        {
                            int    len     = rdr.ReadInt32() - 5;
                            byte   id      = rdr.ReadByte();
                            byte[] content = rdr.ReadBytes(len);
                            ReceiveKey.Crypt(content, content.Length);

                            Console.WriteLine((PacketID)id);
                            if ((PacketID)id == pid)
                            {
                                return(new Packet((PacketID)id, content));
                            }
                        }
                    }
                    catch { }
                }
            }
        }
Esempio n. 4
0
        /* Starts listening for data */
        public bool Start(params object[] args)
        {
            /* Returns false if Start() has previously been called */
            flagMtx.WaitOne();
            if (isRunning)
            {
                flagMtx.ReleaseMutex(); return(false);
            }
            else
            {
                isRunning = true; flagMtx.ReleaseMutex();
            }

            /* Accepts an optional SubscriptionID parameter (defaults to UPDATE) */
            if (args.Length > 0)
            {
                if (args[0] is SubscriptionType)
                {
                    this.expectedID = (PacketID)args[0];
                }
            }

            /* Starts the receiving thread and returns true */
            receiverThread = new Thread(ThreadRoutine);
            receiverThread.Start();

            return(true);
        }
Esempio n. 5
0
        public void HandlePacket(byte[] packet)
        {
            //ushort id = BitConverter.ToUInt16(packet.Take(2).ToArray(), 0);

            //Console.WriteLine("[LoginServer] Time since last packet: {0}ms", timer.ElapsedMilliseconds);

            timer.Reset();

            using (Stream stream = new MemoryStream(packet))
            {
                while (stream.Length - stream.Position > 7)
                {
                    PacketID id = (PacketID)Packet.ReadUInt16(stream);

                    switch (id)
                    {
                    case PacketID.CLVersionCheck:
                        VersionCheck(new CLVersionCheck(stream));
                        break;

                    case PacketID.CLCreatePC:
                        CreatePC(new CLCreatePC(stream));
                        break;

                    case PacketID.CLDeletePC:
                        DeletePC(new CLDeletePC(stream));
                        break;

                    case PacketID.CLLogin:
                        Login(new CLLogin(stream));
                        break;

                    case PacketID.CLGetWorldList:
                        GetWorldList(new CLGetWorldList(stream));
                        break;

                    case PacketID.CLGetServerList:
                        GetServerList(new CLGetServerList(stream));
                        break;

                    case PacketID.CLGetPCList:
                    case PacketID.CLGetPCList2:     // ??
                        GetPCList(new CLGetPCList(stream));
                        break;

                    case PacketID.CLQueryCharacterName:
                        QueryCharacterName(new CLQueryCharacterName(stream));
                        break;

                    case PacketID.CLSelectPC:
                        SelectPC(new CLSelectPC(stream));
                        break;

                    case PacketID.CLReconnectLogin:
                        ReconnectLogin(new CLReconnectLogin(stream));
                        break;
                    }
                }
            }
        }
Esempio n. 6
0
        private int Parse(byte[] data, out Handler handler, out ushort len)
        {
            handler = null;
            len     = 0;
            int i = 0;

            while (i < data.Length - 2 && !ValidPacketID(data[i]))
            {
                i++;
            }

            if (data.Length - i < 3 || !ValidPacketID(data[i]))
            {
                return(i);
            }

            PacketID packetid = (PacketID)data[i];

            len = (ushort)((data[i + 1] << 8) | data[i + 2]);
            if (data.Length < len + 3)
            {
                return(i);
            }
            IEnumerable <byte> payload = data.Skip(3).Take(len);

            handler = Handlers[packetid];
            return(len + 3);
        }
Esempio n. 7
0
 public void MergeFrom(C2S other)
 {
     if (other == null)
     {
         return;
     }
     if (other.packetID_ != null)
     {
         if (packetID_ == null)
         {
             PacketID = new global::Common.PacketID();
         }
         PacketID.MergeFrom(other.PacketID);
     }
     if (other.header_ != null)
     {
         if (header_ == null)
         {
             Header = new global::TrdCommon.TrdHeader();
         }
         Header.MergeFrom(other.Header);
     }
     if (other.OrderID != 0UL)
     {
         OrderID = other.OrderID;
     }
     if (other.ReconfirmReason != 0)
     {
         ReconfirmReason = other.ReconfirmReason;
     }
     _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields);
 }
Esempio n. 8
0
 internal void ZapisHlavicku(PacketID id)
 {
     m_Packeta[0] = 0xFF;
     m_Packeta[1] = (byte)id;
     m_Packeta[2] = (byte)((m_Delka << 8) >> 8);
     m_Packeta[3] = (byte)(m_Delka >> 8);
 }
Esempio n. 9
0
        public override int GetHashCode()
        {
            int hash = 1;

            if (packetID_ != null)
            {
                hash ^= PacketID.GetHashCode();
            }
            if (header_ != null)
            {
                hash ^= Header.GetHashCode();
            }
            if (OrderID != 0UL)
            {
                hash ^= OrderID.GetHashCode();
            }
            if (ReconfirmReason != 0)
            {
                hash ^= ReconfirmReason.GetHashCode();
            }
            if (_unknownFields != null)
            {
                hash ^= _unknownFields.GetHashCode();
            }
            return(hash);
        }
Esempio n. 10
0
        public Packet ReceivePacket(PacketID pid)
        {
            var buff = new byte[0x10000];
            while (true)
            {
                if (cliSkt.ReceiveBufferSize <= 0)
                    continue;
                try
                {
                    cliSkt.Receive(buff);
                    using (var rdr = new NReader(new MemoryStream(buff)))
                    {
                        var len = rdr.ReadInt32() - 5;
                        var id = rdr.ReadByte();
                        var content = rdr.ReadBytes(len);
                        ReceiveKey.Crypt(content, content.Length);

                        Console.WriteLine((PacketID)id);
                        if ((PacketID)id == pid)
                            return new Packet((PacketID)id, content);
                    }
                }
                catch { }
            }
        }
Esempio n. 11
0
        public void Status()
        {
            IPAddress ip = Dns.GetHostAddresses(this.server) [0];

            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            client.Connect(ip, this.port);
            isConnected = true;
            this.SendPacket(new object[] { (byte)PacketID.ServerListPing });
            PacketID packetID = (PacketID)readByte();

            client.Close();
            isConnected = false;
            if (packetID == PacketID.DisconnectKick)
            {
                string   status = readString();
                string[] parts  = status.Split('§');
                Console.WriteLine(
                    "{0}:{1} {2} {3}/{4}",
                    this.server,
                    this.port,
                    parts [0],
                    parts [1],
                    parts [2] == "0" ? "???" : parts [2]
                    );
            }
        }
Esempio n. 12
0
 public BAMCPacket(BAMCPacket Packet)
 {
     Length   = Packet.Length;
     PacketID = Packet.PacketID;
     Payload  = Packet.Payload;
     Client   = Packet.Client;
 }
Esempio n. 13
0
 internal Packeta(PacketReader pr, PacketWriter pw, PacketID id, Client client)
 {
     m_Reader = pr;
     m_Writer = pw;
     m_Id     = id;
     m_Client = client;
 }
Esempio n. 14
0
 private void SendPipeMessage(PacketID packetId, bool isIdle = false, string data = "", string data2 = "", PacketID requestId = PacketID.None, NamedPipeConnection <IdleMessage, IdleMessage> connection = null)
 {
     try
     {
         if (connection == null)
         {
             server.PushMessage(new IdleMessage
             {
                 packetId  = (int)packetId,
                 isIdle    = isIdle,
                 requestId = (int)requestId,
                 data      = data,
                 data2     = data2
             });
         }
         else
         {
             connection.PushMessage(new IdleMessage
             {
                 packetId  = (int)packetId,
                 isIdle    = isIdle,
                 requestId = (int)requestId,
                 data      = data,
                 data2     = data2
             });
         }
     }
     catch (Exception ex)
     {
         Utilities.Log("SendPipeMessage: " + ex.Message);
     }
 }
Esempio n. 15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PacketEventArgs"/> class.
 /// </summary>
 /// <param name="PacketID">The packet ID.</param>
 /// <param name="RemoteClient">The remote client.</param>
 /// <param name="PacketMode">The packet mode.</param>
 /// <param name="Packet">The packet.</param>
 /// <remarks></remarks>
 public PacketEventArgs(PacketID PacketID, RemoteClient RemoteClient, PacketMode PacketMode, Packet Packet)
 {
     this.PacketID = PacketID;
     this.RemoteClient = RemoteClient;
     this.Handled = false;
     this.Packet = Packet;
 }
 public bool IsWaitProtocol(PacketID reqPacketID, out List <PacketID> recvpacketIDs)
 {
     if (WaitProtocol.TryGetValue(reqPacketID, out recvpacketIDs))
     {
         return(true);
     }
     return(false);
 }
Esempio n. 17
0
        public static List <T> TransferItems <T>(GarminUnit garmin, ushort command, PacketID pid)
        {
            byte[] buffer = BitConverter.GetBytes(command);
            Packet p      = MakeCommandPacket(garmin, (uint)buffer.Length);

            garmin.Write(p, buffer);
            return(ReadRecords <T>(garmin, pid));
        }
Esempio n. 18
0
        public static Packet FromBinaryReader(BinaryReader binaryReader)
        {
            PacketID id     = (PacketID)binaryReader.ReadByte();
            Packet   packet = GetNewPacketById(id);

            packet.Read(binaryReader);
            return(packet);
        }
Esempio n. 19
0
 private bool OnPacketReceived(PacketID id, byte[] pkt)
 {
     if (!_parent.IsReady())
     {
         return(false);
     }
     _parent.Manager.Network.AddPendingPacket(_parent, id, pkt);
     return(true);
 }
Esempio n. 20
0
        /// <summary>
        /// Gets a Packet for the specified PacketID.  If IsServer
        /// is set to false, it assumes that the packet is being
        /// handled by a client.
        /// </summary>
        public static Packet GetPacket(PacketID PacketID, bool IsServer = true)
        {
            byte   id = (byte)PacketID;
            Type   t  = Packets[id];
            Packet p  = (Packet)Activator.CreateInstance(Packets[(byte)PacketID]);

            p.IsServerContext = IsServer;
            return(p);
        }
                /// <summary>
                /// 静态调用
                /// </summary>
                /// <returns>数据包ID</returns>
                public static PacketID Instance()
                {
                    if (pSingleton == null)
                    {
                        pSingleton = new PacketID();
                    }

                    return(pSingleton);
                }
Esempio n. 22
0
 public void MergeFrom(C2S other)
 {
     if (other == null)
     {
         return;
     }
     if (other.packetID_ != null)
     {
         if (packetID_ == null)
         {
             PacketID = new global::Common.PacketID();
         }
         PacketID.MergeFrom(other.PacketID);
     }
     if (other.header_ != null)
     {
         if (header_ == null)
         {
             Header = new global::TrdCommon.TrdHeader();
         }
         Header.MergeFrom(other.Header);
     }
     if (other.TrdSide != 0)
     {
         TrdSide = other.TrdSide;
     }
     if (other.OrderType != 0)
     {
         OrderType = other.OrderType;
     }
     if (other.Code.Length != 0)
     {
         Code = other.Code;
     }
     if (other.Qty != 0D)
     {
         Qty = other.Qty;
     }
     if (other.Price != 0D)
     {
         Price = other.Price;
     }
     if (other.AdjustPrice != false)
     {
         AdjustPrice = other.AdjustPrice;
     }
     if (other.AdjustSideAndLimit != 0D)
     {
         AdjustSideAndLimit = other.AdjustSideAndLimit;
     }
     if (other.SecMarket != 0)
     {
         SecMarket = other.SecMarket;
     }
     _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields);
 }
Esempio n. 23
0
 public static bool TryParse(ushort value, out PacketID parsed)
 {
     parsed = PacketID.Null;
     if (!Enum.IsDefined(typeof(PacketID), value))
     {
         return(false);
     }
     parsed = (PacketID)value;
     return(true);
 }
Esempio n. 24
0
        private static void Listener_NetworkReceiveEvent(NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod)
        {
            PacketID id = (PacketID)reader.GetByte();

            switch (id)
            {
            case PacketID.PlayerUpdatePacket:

                break;
            }
        }
Esempio n. 25
0
 public void MergeFrom(C2S other)
 {
     if (other == null)
     {
         return;
     }
     if (other.packetID_ != null)
     {
         if (packetID_ == null)
         {
             PacketID = new global::Common.PacketID();
         }
         PacketID.MergeFrom(other.PacketID);
     }
     if (other.header_ != null)
     {
         if (header_ == null)
         {
             Header = new global::TrdCommon.TrdHeader();
         }
         Header.MergeFrom(other.Header);
     }
     if (other.OrderID != 0UL)
     {
         OrderID = other.OrderID;
     }
     if (other.ModifyOrderOp != 0)
     {
         ModifyOrderOp = other.ModifyOrderOp;
     }
     if (other.ForAll != false)
     {
         ForAll = other.ForAll;
     }
     if (other.Qty != 0D)
     {
         Qty = other.Qty;
     }
     if (other.Price != 0D)
     {
         Price = other.Price;
     }
     if (other.AdjustPrice != false)
     {
         AdjustPrice = other.AdjustPrice;
     }
     if (other.AdjustSideAndLimit != 0D)
     {
         AdjustSideAndLimit = other.AdjustSideAndLimit;
     }
     _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields);
 }
Esempio n. 26
0
        public void RegisterPacketCallback<PacketClass>(PacketID packetID, Action<PacketClass> packetCallback)
            where PacketClass : IPacket, new()
        {
            if (_packetIDToCallback.ContainsKey(packetID)) throw new Exception("Packet ID '" + packetID.ToString() + "' already has a callback");

            if (packetCallback != null) {
                _packetIDToCallback[packetID] = (IPacket packet) => {
                    packetCallback.Invoke((PacketClass)packet);
                };
            } else {
                throw new ArgumentNullException("packetCallback");
            }
        }
Esempio n. 27
0
        public static Packet GetPacket(PacketID pId)
        {
            byte id = ( byte )pId;
            Type t  = Packets[id];

            if (t == typeof(PacketInvalid))
            {
                return(null);
            }
            Packet p = ( Packet )Activator.CreateInstance(Packets[( byte )pId]);

            return(p);
        }
Esempio n. 28
0
        /* Runs when triggered by an ACReader object */
        private void OnPacketReceived(object sender, ACReceivedEventArgs args)
        {
            PacketID id = args.id;

            if (id == PacketID.RESPONSE)
            {
                responseMtx.WaitOne();
                responsePacket = (HandshakeResponse)args.packet;
                responseMtx.ReleaseMutex();

                dataMtx.WaitOne();
                maxRpm = 0;
                dataMtx.ReleaseMutex();
            }
            else if (id == PacketID.UPDATE)
            {
                RTCarInfo packet = (RTCarInfo)args.packet;

                dataMtx.WaitOne();
                if (packet.engineRPM > maxRpm)
                {
                    maxRpm = (int)packet.engineRPM;
                }
                dataMtx.ReleaseMutex();

                PositionEventArgs newPos = new PositionEventArgs();
                newPos.x = packet.carCoordinates[0];
                newPos.y = packet.carCoordinates[1];
                newPos.z = packet.carCoordinates[2];

                GForceEventArgs newGs = new GForceEventArgs();
                newGs.lat = packet.accG_horizontal;
                newGs.lon = packet.accG_frontal;

                PowertrainEventArgs newPow = new PowertrainEventArgs();
                newPow.rpm = (int)packet.engineRPM;
                newPow.kmh = packet.speed_Kmh;
                newPow.mph = packet.speed_Mph;

                AxesEventArgs newAxes = new AxesEventArgs();
                newAxes.throttle = packet.gas;
                newAxes.brake    = packet.brake;
                newAxes.clutch   = 1.0f - packet.clutch;
                newAxes.steering = packet.steer;

                RaisePositionReceived(newPos);
                RaiseGForceReceived(newGs);
                RaisePowertrainReceived(newPow);
                RaiseAxesReceived(newAxes);
            }
        }
Esempio n. 29
0
        public override int GetHashCode()
        {
            int hash = 1;

            if (packetID_ != null)
            {
                hash ^= PacketID.GetHashCode();
            }
            if (header_ != null)
            {
                hash ^= Header.GetHashCode();
            }
            if (TrdSide != 0)
            {
                hash ^= TrdSide.GetHashCode();
            }
            if (OrderType != 0)
            {
                hash ^= OrderType.GetHashCode();
            }
            if (Code.Length != 0)
            {
                hash ^= Code.GetHashCode();
            }
            if (Qty != 0D)
            {
                hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(Qty);
            }
            if (Price != 0D)
            {
                hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(Price);
            }
            if (AdjustPrice != false)
            {
                hash ^= AdjustPrice.GetHashCode();
            }
            if (AdjustSideAndLimit != 0D)
            {
                hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(AdjustSideAndLimit);
            }
            if (SecMarket != 0)
            {
                hash ^= SecMarket.GetHashCode();
            }
            if (_unknownFields != null)
            {
                hash ^= _unknownFields.GetHashCode();
            }
            return(hash);
        }
Esempio n. 30
0
        public void Unregister(PacketID id1, PacketID2 id2, HandleMsgCallback onHandleMsg)
        {
            int cmdID = (int)id1 << 16 | (int)id2;

            if (m_HandleMap.ContainsKey(cmdID))
            {
                m_HandleMap[cmdID] -= onHandleMsg;
                if (m_HandleMap[cmdID] == null)
                {
                    m_HandleMap.Remove(cmdID);
                }
                m_dispatchDelHandle.Add(onHandleMsg);
            }
        }
Esempio n. 31
0
        public void Register(PacketID id1, PacketID2 id2, HandleMsgCallback onHandleMsg)
        {
            int cmdID = (int)id1 << 16 | (int)id2;

            //Debugger.LogError("Register : " + cmdID);
            if (!m_HandleMap.ContainsKey(cmdID))
            {
                m_HandleMap[cmdID] = onHandleMsg;
            }
            else
            {
                m_HandleMap[cmdID] += onHandleMsg;
            }
        }
Esempio n. 32
0
            public void HandleData(Packet _packet)
            {
                int _packetLength = _packet.ReadInt();

                byte[] _packetBytes = _packet.ReadBytes(_packetLength);

                ThreadManager.ExecuteOnMainThread(() =>
                {
                    using (Packet _newPacket = new Packet(_packetBytes))
                    {
                        _newPacket.Decrypt(NetworkManager.instance.encryptionType, NetworkManager.instance.encryptionKey);
                        PacketID _packetId = NetworkManager.instance.packetIDType == PacketIDType.STRING ? new PacketID(_newPacket.ReadString()) : new PacketID(_newPacket.ReadShort());
                        client.HandleData(_packetId, _newPacket);
                    }
                });
            }
Esempio n. 33
0
 public bool ValidPacketID(PacketID id)
 {
     return Handlers.ContainsKey(id);
 }
Esempio n. 34
0
 public Packet(PacketID id, byte[] body)
 {
     this.id = id;
     this.body = body;
 }
Esempio n. 35
0
 bool OnPacketReceived(PacketID id, byte[] pkt)
 {
     if (parent.IsReady())
     {
         parent.Manager.Network.AddPendingPacket(parent, id, pkt);
         return true;
     }
     else
         return false;
 }
Esempio n. 36
0
 public RobotPacket(PacketID id)
 {
     commandID = id;
     Parameters = new byte[] { };
     //if
 }
Esempio n. 37
0
 /// <summary>
 /// Gets a Packet for the specified PacketID.  If IsServer
 /// is set to false, it assumes that the packet is being
 /// handled by a client.
 /// </summary>
 public static Packet GetPacket(PacketID PacketID, bool IsServer = true)
 {
     byte id = (byte)PacketID;
     Type t = Packets[id];
     Packet p = (Packet)Activator.CreateInstance(Packets[(byte)PacketID]);
     p.IsServerContext = IsServer;
     return p;
 }
Esempio n. 38
0
 public static Packet GetPacket( PacketID pId  )
 {
     byte id = ( byte ) pId;
     Type t = Packets[ id ];
     if ( t == typeof( PacketInvalid ) ) {
         return null;
     }
     Packet p = ( Packet ) Activator.CreateInstance( Packets[ ( byte ) pId ] );
     return p;
 }
Esempio n. 39
0
 public Packet(PacketID packetID, int byteDataLength, byte[] byteData)
 {
     this.packetID = packetID;
     this.byteDataLength = byteDataLength;
     this.byteData = byteData;
 }
Esempio n. 40
0
 public void setPacketArray(byte[] realArray)
 {
     packetID = (PacketID)realArray[0];
     byteDataLength = BitConverter.ToInt32(realArray, 1);
     byteData = new byte[byteDataLength];
     Array.Copy(realArray, 5, byteData, 0, byteDataLength);
 }
Esempio n. 41
0
        public void RegisterHandler(PacketID packetID, PacketHandler handler)
        {
            if (!_handlers.ContainsKey(packetID))
                _handlers.Add(packetID, new List<PacketHandler>());

            _handlers[packetID].Add(handler);
        }
 private void SendPacket(byte[] packet, PacketID ID)
 {
     packet = new byte[] { (byte)ID }
         .Concat(MakeUInt((uint)packet.Length))
         .Concat(packet).ToArray();
     TcpClient.GetStream().Write(packet, 0, packet.Length);
 }
Esempio n. 43
0
 public void AddPendingPacket(Client client, PacketID id, byte[] packet)
 {
     pendings.Enqueue(new Work(client, id, packet));
 }