public TCPClientPacketService()
        {
            SendPackets.Add(typeof(S0001Authenticate), 0x0001);
            SendPackets.Add(typeof(S0003Chat), 0x0003);
            SendPackets.Add(typeof(S0005TimeRequest), 0x0005);

            RecvPackets.Add(0x0006, typeof(R0006TimeResponse));
        }
Exemplo n.º 2
0
        public TCPServerPacketService()
        {
            SendPackets.Add(typeof(S0006TimeResponse), 0x0006);

            RecvPackets.Add(0x0001, typeof(R0001Authenticate));
            RecvPackets.Add(0x0003, typeof(R0003Chat));
            RecvPackets.Add(0x0005, typeof(R0005TimeRequest));
        }
Exemplo n.º 3
0
        private void ProcessRecvQueue()
        {
            List <RUDPPacket> RecvPackets;

            lock (_recvMutex)
                RecvPackets = _recvQueue.OrderBy(x => x.Seq).ToList();
            foreach (RUDPPacket p in RecvPackets)
            {
                if (p.Processed || _processedSequences.Contains(p.Seq))
                {
                    continue;
                }

                if (p.Seq != Remote)
                {
                    RUDPConnection.Trace("{0} != {1} | {2}", p.Seq, Remote, Name);

                    lock (_recvMutex)
                        _recvQueue.Add(p);
                    if (p.Seq > Remote)
                    {
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }

                if (p.Qty == 0)
                {
                    Remote++;
                    p.Processed = true;
                    _processedSequences.Add(p.Seq);
                    RUDPConnection.Debug("RECV <- {0}: {1}", p.Src, p);

                    if (IsServer)
                    {
                        if (p.Type == RUDPPacketType.SYN)
                        {
                            SendPacket(RUDPPacketType.ACK);
                            LastKeepAliveReceived = DateTime.Now;
                            Connection.InvokeIncomingConnection(this);
                            continue;
                        }
                    }
                    else if (p.Type == RUDPPacketType.ACK)
                    {
                        State = State.CONNECTED;
                        LastKeepAliveReceived = DateTime.Now;
                        Connection.InvokeConnected(this);
                        continue;
                    }

                    if (p.Type == RUDPPacketType.DAT)
                    {
                        Connection.InvokePacketReceived(this, p);
                    }
                }
                else if (p.Qty > 0 && p.Type == RUDPPacketType.DAT)
                {
                    List <RUDPPacket> multiPackets = RecvPackets.Where(x => x.Id == p.Id).ToList();
                    multiPackets = multiPackets.GroupBy(x => x.Seq).Select(g => g.First()).OrderBy(x => x.Seq).ToList();
                    if (multiPackets.Count == p.Qty)
                    {
                        RUDPConnection.Debug("MULTIPACKET {0}", p.Id);

                        byte[]       buf;
                        MemoryStream ms = new MemoryStream();
                        using (BinaryWriter bw = new BinaryWriter(ms))
                            foreach (RUDPPacket mp in multiPackets)
                            {
                                mp.Processed = true;
                                bw.Write(mp.Data);
                                RUDPConnection.Debug("RECV MP <- {0}: {1}", mp.Src, mp);
                            }
                        _processedSequences.AddRange(multiPackets.Select(x => x.Seq));
                        buf = ms.ToArray();
                        // Debug("MULTIPACKET ID {0} DATA: {1}", p.PacketId, Encoding.ASCII.GetString(buf));
                        Connection.InvokePacketReceived(this, new RUDPPacket()
                        {
                            Serializer = Connection.Serializer,
                            Data       = buf,
                            Id         = p.Id,
                            Qty        = p.Qty,
                            Type       = p.Type,
                            Seq        = p.Seq
                        });

                        Remote += p.Qty;
                    }
                    else if (multiPackets.Count < p.Qty)
                    {
                        lock (_recvMutex)
                            _recvQueue.Add(p);
                        break;
                    }
                    else
                    {
                        RUDPConnection.Debug("P.QTY > MULTIPACKETS.COUNT ({0} > {1})", p.Qty, multiPackets.Count);
                        throw new Exception();
                    }
                }
            }
            lock (_recvMutex)
                _recvQueue.RemoveAll(x => x.Processed);
        }