public virtual bool SendCustom(ICustomMessage message, DeliveryType deliveryType, int channel) { if (message == null) { throw new ArgumentNullException(nameof(message)); } ChannelDescriptor.CheckChannelValue(channel); using (var rawMessage = this.Parent.CreateMessage()) { rawMessage.DeliveryType = deliveryType; rawMessage.Channel = channel; using (WardenStreamWriter sw = new WardenStreamWriter(rawMessage.BaseStream, true)) { message.WriteTo(new WriteFormatterInfo(sw, this.configuration.Serializer)); } UdpSendStatus result = SendRawMessage(new Warden.Networking.Udp.Messages.MessageInfo(rawMessage, deliveryType, channel)); return(result != UdpSendStatus.Failed); } }
public async Task <HttpStatusCode> UploadFileAsync(ChannelDescriptor channel, MessageDescriptor message, string filePath, CancellationToken ct) { if (channel is ChannelCategoryDescriptor || channel is VoiceChannelDescriptor) { throw new ArgumentException("Invalid channel type", nameof(channel)); } MultipartFormDataContent content = new MultipartFormDataContent("--dcore--"); if (message?.Content != null || message?.embeds != null) { object msg = new { content = message.Content ?? "", embed = message.embeds[0] ?? null }; content.Add( new StringContent( JsonConvert.SerializeObject(msg, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }) ), "payload_json" ); } content.Add(new StreamContent(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)), "file", Path.GetFileName(filePath)); HttpResponseMessage response = await _rest.PostAsync($"{BaseUri}/{channel.Id}/{MessageUri}", content, ct); return(response.StatusCode); }
public void AddChannelDescriptorAndOffset(ChannelDescriptor descriptor, float frameData) { ChannelDescriptorAndOffset datum = new ChannelDescriptorAndOffset(); datum.ChannelDescriptor = descriptor; datum.Offset = frameData; }
private void MapGuildMessage(MessageDescriptor message, GuildMapper mapper) { if (!mapper.MappedGuilds.TryGetValue(message.guild_id.Value, out GuildDescriptor guild)) { throw new MappingException("Guild ID contained in message not previously mapped.", message.guild_id.Value, message); } message.Guild = guild; ChannelDescriptor channel = guild.Channels.FirstOrDefault(c => c.Id == message.channel_id); message.Channel = channel ?? throw new MappingException("Channel ID contained in message not previously mapped", message.channel_id, message, guild); MemberDescriptor member = guild.Members.FirstOrDefault(m => m.User.Id == message.author.id); message.Sender = member ?? throw new MappingException("Author ID contained in message not previously mapped", message.author.id, message, guild); message.MentionedRoles = guild.Roles.Join(inner: message.mention_roles, outerKeySelector: rD => rD.Id, innerKeySelector: id => id, resultSelector: (rD, id) => rD).ToList(); message.MentionedUsers = guild.Members.Join(inner: message.mentions, outerKeySelector: mD => mD.User.Id, innerKeySelector: uO => uO.id, resultSelector: (mD, uO) => mD).ToList(); }
public async Task <HttpStatusCode> PostMessageAsync(ChannelDescriptor channel, MessageDescriptor message, CancellationToken ct) { if (channel is ChannelCategoryDescriptor || channel is VoiceChannelDescriptor) { throw new ArgumentException("Invalid channel type", nameof(channel)); } if (message == null || (message.Content == null && message.embeds == null)) { throw new ArgumentException("Message may not be null", nameof(message)); } string msgJson = JsonConvert.SerializeObject( new { content = message.Content, embed = message.embeds?[0] ?? null }, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore } ); HttpContent content = new StringContent(msgJson, Encoding.UTF8, "application/json"); HttpResponseMessage response = await _rest.PostAsync($"{BaseUri}/{channel.Id}/{MessageUri}", content, ct); return(response.StatusCode); }
public ChannelBase(MemoryStreamPool memoryStreamPool, ILogManager logManager, ChannelDescriptor descriptor, IChannelConnection connection) { this.memoryStreamPool = memoryStreamPool; this.Descriptor = descriptor; this.connection = connection; this.logger = logManager.GetLogger(this.GetType().Name); }
public void addChannel(ChannelDescriptor channel) { if (this.channels == null) { this.channels = new List <ChannelDescriptor>(0); } channels.Add(channel); }
public ReliableChannel(MemoryStreamPool memoryStreamPool, ILogManager logManager, ChannelDescriptor descriptor, IChannelConnection connection, bool ordered) : base(memoryStreamPool, logManager, descriptor, connection) { this.ordered = ordered; this.recvWindowStart = 0; this.ackWindowStart = 0; if (ordered) { this.recvWithheld = new Datagram[WINDOW_SIZE]; } else { this.recvEarlyReceived = new bool[WINDOW_SIZE]; } this.sendPendingPackets = new PendingPacket[WINDOW_SIZE]; this.sendDelayedPackets = new ConcurrentQueue <Datagram>(); }
public UdpSendStatus SendMessage(MessageInfo messageInfo) { if (!CheckStatus(UdpConnectionStatus.Connected)) { messageInfo.Message.Dispose(); return(UdpSendStatus.Failed); } var descriptor = new ChannelDescriptor(messageInfo.Channel, messageInfo.DeliveryType); IChannel channel_ = GetOrAddChannel(descriptor); if (!CheckCanBeSendUnfragmented(messageInfo.Message)) { //need split if (messageInfo.DeliveryType == DeliveryType.Unreliable || messageInfo.DeliveryType == DeliveryType.UnreliableSequenced) { messageInfo.Message.Dispose(); if (peer.Configuration.TooLargeUnreliableMessageBehaviour == UdpConfigurationPeer.TooLargeMessageBehaviour.RaiseException) { throw new ArgumentException("You couldn't send fragmented message throught unreliable channel. You message below MTU limit or change delivery type"); } else { return(UdpSendStatus.Failed); } } return(SendFragmentedMessage(messageInfo.Message, channel_)); } Datagram datagram = messageInfo.Message.ConvertToDatagram(); datagram.Type = MessageType.UserData; datagram.Channel = messageInfo.Channel; datagram.ConnectionKey = this.EndPoint.ConnectionKey; datagram.DeliveryType = messageInfo.DeliveryType; var status = channel_.SendDatagram(datagram); messageInfo.Message.Dispose(); return(status); }
IChannel GetOrAddChannel(ChannelDescriptor descriptor) { IChannel result = channels.GetOrAdd(descriptor, (desc) => { switch (desc.DeliveryType) { case DeliveryType.ReliableOrdered: return(new ReliableChannel(peer.Configuration.MemoryStreamPool, peer.Configuration.LogManager, desc, this, true)); case DeliveryType.ReliableUnordered: return(new ReliableChannel(peer.Configuration.MemoryStreamPool, peer.Configuration.LogManager, desc, this, false)); case DeliveryType.Unreliable: return(new UnreliableChannel(peer.Configuration.MemoryStreamPool, peer.Configuration.LogManager, desc, this)); case DeliveryType.UnreliableSequenced: return(new UnreliableSequencedChannel(peer.Configuration.MemoryStreamPool, peer.Configuration.LogManager, desc, this)); default: throw new ArgumentException("Got datagram with unknown delivery type"); } }); return(result); }
public UnreliableChannel(MemoryStreamPool memoryStreamPool, ILogManager logManager, ChannelDescriptor descriptor, IChannelConnection connection) : base(memoryStreamPool, logManager, descriptor, connection) { }