Ejemplo n.º 1
0
        public override void Send(Chunk packet, uint token      = TokenHelper.TokenNone,
                                  SendCallbackData callbackData = null)
        {
            if (packet.Flags.HasFlag(SendFlags.Connless))
            {
                if (packet.DataSize > NetworkHelper.MaxPayload)
                {
                    Debug.Warning("network", $"packet payload too big, length={packet.DataSize}");
                    return;
                }

                if (packet.ClientId == -1 && Connection.EndPoint != null &&
                    packet.EndPoint.Compare(Connection.EndPoint, true))
                {
                    packet.ClientId = 0;
                }

                if (token != TokenHelper.TokenNone)
                {
                    NetworkHelper.SendPacketConnless(UdpClient, packet.EndPoint,
                                                     token, TokenManager.GenerateToken(packet.EndPoint),
                                                     packet.Data, packet.DataSize);
                }
                else
                {
                    if (packet.ClientId == -1)
                    {
                        TokenCache.SendPacketConnless(packet.EndPoint,
                                                      packet.Data, packet.DataSize, callbackData);
                    }
                    else
                    {
                        Debug.Assert(packet.ClientId == 0, "errornous client id");
                        Connection.SendPacketConnless(packet.Data, packet.DataSize);
                    }
                }
            }
            else
            {
                if (packet.DataSize + NetworkHelper.MaxChunkHeaderSize >= NetworkHelper.MaxPayload)
                {
                    Debug.Warning("network", $"chunk payload too big, length={packet.DataSize} dropping chunk");
                    return;
                }

                Debug.Assert(packet.ClientId == 0, "errornous client id");

                var flags = ChunkFlags.None;
                if (packet.Flags.HasFlag(SendFlags.Vital))
                {
                    flags = ChunkFlags.Vital;
                }

                Connection.QueueChunk(flags, packet.Data, packet.DataSize);

                if (packet.Flags.HasFlag(SendFlags.Flush))
                {
                    Connection.Flush();
                }
            }
        }
Ejemplo n.º 2
0
        public override bool Send(Chunk packet, uint token = TokenHelper.TokenNone)
        {
            if (packet.Flags.HasFlag(SendFlags.Connless))
            {
                if (packet.DataSize >= NetworkHelper.MaxPayload)
                {
                    Debug.Warning("netserver", $"packet payload too big ({packet.DataSize}). dropping packet");
                    return(false);
                }

                if (packet.ClientId == -1)
                {
                    for (var i = 0; i < Connections.Count; i++)
                    {
                        if (Connections[i].State != ConnectionState.Offline &&
                            Connections[i].EndPoint != null &&
                            Connections[i].EndPoint.Compare(packet.EndPoint, comparePorts: true))
                        {
                            packet.ClientId = i;
                            break;
                        }
                    }
                }

                if (token != TokenHelper.TokenNone)
                {
                    NetworkHelper.SendPacketConnless(UdpClient, packet.EndPoint, token,
                                                     TokenManager.GenerateToken(packet.EndPoint), packet.Data, packet.DataSize);
                }
                else
                {
                    if (packet.ClientId == -1)
                    {
                        TokenCache.SendPacketConnless(packet.EndPoint, packet.Data, packet.DataSize);
                    }
                    else
                    {
                        Debug.Assert(packet.ClientId >= 0 &&
                                     packet.ClientId < Connections.Count, "errornous client id");
                        Connections[packet.ClientId].SendPacketConnless(packet.Data, packet.DataSize);
                    }
                }
            }
            else
            {
                if (packet.DataSize + NetworkHelper.MaxChunkHeaderSize >= NetworkHelper.MaxPayload)
                {
                    Debug.Warning("netserver", $"packet payload too big ({packet.DataSize}). dropping packet");
                    return(false);
                }

                Debug.Assert(packet.ClientId >= 0 &&
                             packet.ClientId < Connections.Count, "errornous client id");

                var flags = packet.Flags.HasFlag(SendFlags.Vital)
                    ? ChunkFlags.Vital
                    : ChunkFlags.None;

                if (Connections[packet.ClientId].QueueChunk(flags, packet.Data, packet.DataSize))
                {
                    if (packet.Flags.HasFlag(SendFlags.Flush))
                    {
                        Connections[packet.ClientId].Flush();
                    }
                }
                else
                {
                    Drop(packet.ClientId, "Error sending data");
                }
            }

            return(true);
        }