Mailbox and Mail

#ifndef __MAILBOX_H__
#define __MAILBOX_H__

#include <stdint.h>
#include <stdlib.h>
#include <string.h>

typedef struct
{
  // uint32_t Capacity;
  uint8_t * Memory;
  uint32_t MailSize;
  uint32_t MailCount;
  uint32_t ReadIndex;
  uint32_t ReadCount;
} MAILBOX;

// Creates a new mailbox and Init it.
MAILBOX * MBX_Create( uint32_t MailSize, uint32_t MailCount );

// Deletes a specified mailbox.
void MBX_Delete( MAILBOX * Mailbox );

// Clears all messages in a specified mailbox.
void MBX_Clear( MAILBOX * Mailbox );

// Init a new mailbox.
void MBX_Init( MAILBOX * Mailbox, uint32_t MailSize, uint32_t MailCount,
  void * Memory );

// Stores a new message of a predefined size in a mailbox.
uint32_t MBX_Write( MAILBOX * Mailbox, void * Mail );

/*
 * Stores a new message of a predefined size into a mailbox in front of all
 * other messages, if the mailbox is able to accept one more message.
 * The new message will be retrieved first.
 */
uint32_t MBX_WriteFront( MAILBOX * Mailbox, void * Mail );

/* Retrieves a new message of a predefined size from a mailbox.
 * Mail : Pointer to the memory area that the message should be stored at.
 * Make sure that it points to a valid memory area and that there is sufficient
 * space for an entiremessage. The message size (in bytes) was defined
 * when the mailbox was created.
 */
uint32_t MBX_Read( MAILBOX * Mailbox, void * Mail );

// Returns the number of mail currently available in a specified mailbox.
uint32_t MBX_GetCount( MAILBOX * Mailbox );

#endif /* __MAILBOX_H__ */
#include "mailbox.h"
#include "macro_misc.h"
#include "cmsis_os.h"

// Creates a new mailbox and Init it.
MAILBOX * MBX_Create( uint32_t MailSize, uint32_t MailCount )
{
  uint32_t Size = ALIGN_UP( sizeof(MAILBOX), 4 ) + MailSize * MailCount;
  MAILBOX * Mailbox = (MAILBOX *) osMalloc( Size, osWaitForever );
  if ( Mailbox == 0 )
    return Mailbox;

  uint8_t * Memory = //
    (uint8_t *) ( ( (uint32_t) ( Mailbox ) ) + ALIGN_UP( sizeof(MAILBOX), 4 ) );

  MBX_Init( Mailbox, MailSize, MailCount, Memory );

  return Mailbox;
}

// Deletes a specified mailbox.
void MBX_Delete( MAILBOX * Mailbox )
{
  osFree( Mailbox );
}

// Clears all messages in a specified mailbox.
void MBX_Clear( MAILBOX * Mailbox )
{
  Mailbox->ReadCount = 0;
}

// Returns the number of mail currently available in a specified mailbox.
uint32_t MBX_GetCount( MAILBOX * Mailbox )
{
  return Mailbox->ReadCount;
}

// Init a new mailbox.
void MBX_Init( MAILBOX * Mailbox, uint32_t MailSize, uint32_t MailCount,
  void * Memory )
{
  // Mailbox->Capacity = MailCount * MailSize;
  Mailbox->MailSize = MailSize;
  Mailbox->MailCount = MailCount;
  Mailbox->Memory = Memory;
  Mailbox->ReadIndex = 0;
  Mailbox->ReadCount = 0;
}

/* Stores a new message of a predefined size in a mailbox.
 *
 * 0: Message could not be stored (mailbox is full).
 * 1: Success; message stored.
 */
uint32_t MBX_Write( MAILBOX * Mailbox, void * Mail )
{
  if ( Mailbox->ReadCount == Mailbox->MailCount )
    return 0;

  uint32_t Value = osDisableInterrupt( );

  uint32_t IndexToWrite = ( Mailbox->ReadIndex + Mailbox->ReadCount );
  if ( IndexToWrite == Mailbox->MailCount )
    IndexToWrite = 0;

  uint32_t MemoryIndexToWrite = IndexToWrite * Mailbox->MailSize;
  memcpy( &Mailbox->Memory[ MemoryIndexToWrite ], Mail, Mailbox->MailSize );
  Mailbox->ReadCount++;

  osRestoreInterrupt( Value );

  return 1;
}

/*
 * Stores a new message of a predefined size into a mailbox in front of all
 * other messages, if the mailbox is able to accept one more message.
 * The new message will be retrieved first.
 *
 * 0: Message could not be stored (mailbox is full).
 * 1: Success; message stored.
 */
uint32_t MBX_WriteFront( MAILBOX * Mailbox, void * Mail )
{
  if ( Mailbox->ReadCount == Mailbox->MailCount )
    return 0;

  if ( Mailbox->ReadCount == 0 )
    return MBX_Write( Mailbox, Mail );

  uint32_t Value = osDisableInterrupt( );

  uint32_t IndexToWrite;

  if ( Mailbox->ReadIndex )
    IndexToWrite = Mailbox->ReadIndex - 1;
  else
    IndexToWrite = Mailbox->MailCount - 1;

  uint32_t MemoryIndexToWrite = IndexToWrite * Mailbox->MailSize;
  memcpy( &Mailbox->Memory[ MemoryIndexToWrite ], Mail, Mailbox->MailSize );

  Mailbox->ReadIndex = IndexToWrite;
  Mailbox->ReadCount++;

  osRestoreInterrupt( Value );

  return 1;
}

/* Retrieves a new message of a predefined size from a mailbox.
 * Mail : Pointer to the memory area that the message should be stored at.
 * Make sure that it points to a valid memory area and that there is sufficient
 * space for an entiremessage. The message size (in bytes) was defined
 * when the mailbox was created.
 *
 * 0: Message could not be retrieved (mailbox is empty)
 * 1: Success; message retrieved.
 */
uint32_t MBX_Read( MAILBOX * Mailbox, void * Mail )
{
  if ( Mailbox->ReadCount == 0 )
    return 0;

  uint32_t Value = osDisableInterrupt( );

  uint32_t MemoryIndexToRead = Mailbox->ReadIndex * Mailbox->MailSize;
  memcpy( Mail, &Mailbox->Memory[ MemoryIndexToRead ], Mailbox->MailSize );

  Mailbox->ReadIndex++;
  if ( Mailbox->ReadIndex == Mailbox->MailCount )
    Mailbox->ReadIndex = 0;
  Mailbox->ReadCount--;

  osRestoreInterrupt( Value );

  return 1;
}

Mailbox and Mail

时间: 2024-08-29 03:13:05

Mailbox and Mail的相关文章

centos7.2下搭建postfix++dovecot+courier-authlib+extmail邮件收发系统

专业的事由专业的人去做,现在DNS,mail邮箱系统基本都是专业的公司去做了,越来越少公司自己搭建DNS,mail等系统服务 这次由于服务器要迁移,公司的邮箱系统一直都是用开源的postfix的,只能自己折腾 在此记录一下,搭建全过程使用root账号,中间有一些错误调试的,都给忽略了,这里只给出最的配置 在文章最后面会有一些错误调试的记录 不管遇到什么错误,首先打印日志来看! 不管遇到什么错误,首先打印日志来看! 不管遇到什么错误,首先打印日志来看! 在网上大概了解了一下整个邮箱系统的组成: #

DNS 中的协议字段详细定义

DNS中的协议字段定义 Table of Contents 1 概述 2 DNS Classes 3 DNS OpCodes 4 DNS RCODEs 5 DNS Label Types 6 DNS资源记录 7 EDNS Version 8 DNS EDNS0 Option Codes (OPT) 1 概述 总结DNS协议中各字段的取值 2 DNS Classes Decimal Name Reference 0 Reserved RFC6895 1 Internet(IN) RFC1035 2

线程邮箱

在多线程开发中,消息队列是一种有效的线程间通讯方式.我在开发KendyNet的过程中一直在寻找一种高效而易用的消息队列实现. 期间使用过的一种实现可参考message queue的设计.这个实现的消息队列是相当高效的,但其存在的一个问题是,如果发送方相 对较慢,则需要一个定时机制以固定间隔将本线程中缓存的待发送消息同步到共享队列中,这也导致了消息有一定的延时. 然后我还考虑过无锁实现的队列,但无锁队列有一个问题,就是当队列为空的时候,不能给消息消费者提供一种可被唤醒的休眠手段. 下面的示例代码是

【转载】Powershell获取世纪互联Office365所有用户最后一次登录时间

1 #$Mails=get-mailbox -ResultSize 10 2 $Mails=get-mailbox -ResultSize Unlimited 3 $Mails | Measure-Object 4 5 $i=0 6 7 foreach ($Mail in $Mails) 8 { 9 $i=$i+1 10 $mailbox=Get-MailboxStatistics $Mail.UserPrincipalName 11 $content=-join($i,"`t",$M

error writing messa ge: File too large

1 postfix 作为邮件服务器时,当然这里应用的不是虚拟用户,出现用户只能发送Email 而不能接受Email 的问题,在其日志中出现如下 cannot update mailbox /var/mail/root for user root. error writing message: File too large 2  该问题主要原因是因为在postfix配置文件main.cf.default mailbox_size_limit = 51200000(50M) 也就是说当用户的对应的/

Mail搭建

一.本章结构 1.postfix概念与原理 2.postfix配置文件解析 3.邮件服务器端配置与客户端使用 4.垃圾邮件过滤 5.邮件自动回复 二.邮件服务器概述 1.邮件服务器概念 电子邮件服务器是处理邮件交换的软硬件设施设施的总称,包括电子邮件程序.电子邮件箱等.它是为用户提供基于E-mail服务的电子邮件系统,人们通过访问服务器实现邮件的交换. 2.常见的邮件服务器 Sendmail.Qmail.Postfix.Zmailer (linux) Exchange.Notes/Domino

cmsis-rtos v1.02不支持Mail Queue?

STM32CubeMX默认支持Freertos. /* USER CODE BEGIN Variables */ osMailQDef(mail, 6, uint8_t); /* USER CODE END Variables */ cmsis-rtos文档中有对邮件队列管理的描述,按照定义后,编译总是报错: ..\Src\freertos.c(52): error:  #151: a typedef name may not be redeclared as a parameter osMai

&lt;邮件服务postfix+mysql&gt;MAIL第二篇

环境:本服务是建立在第一篇的基础之上的,最好搭建好第一篇 玩此服务的前提是你的系统装好了msql和postfix服务. Postfix+mysql主要是把邮件服务的发与mysql结合使用.当然mysql要是一直是在命令行下使用也不是很方便对吧,下面我们来看linux图形化下mysql的使用: 下面的软件包. phpMyAdmin-2.11.3-all-languages.tar.gz是以php的格式结合Apache通过网页的形式管理mysql. 既然是使用网页管理mysql,当然很定得将这个软件

企业级mail服务器 Extmail 搭建

邮件服务器 MUA 邮件用户代理 (Mail User Agent)  如foxmail outlookMDA邮件投递代理(MAIL DELIVERY AGENT)如dovecotMTA 邮件传输代理 (Mail Transfer Agent)如postfix  sendmailSMTP(Simple Mail Transfer Protocol)即简单邮件传输协议, 它帮助每台计算机在发送或中转信件时找到下一个目的地.通过SMTP协议所指定的服务器,就可以把E-mail寄到收信人的服务器上了,