Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::processes::SharedMessageEndpoint Class Referencefinal

A endpoint that allows processes to queue messages on. More...

#include <ipc.h>

Inheritance diagram for MaxOS::processes::SharedMessageEndpoint:
MaxOS::processes::Resource

Public Member Functions

 SharedMessageEndpoint (const string &name, size_t size, resource_type_t type)
 Creates a new shared message endpoint.
 
 ~SharedMessageEndpoint () final
 Destroys the shared message endpoint and frees all messages.
 
int read (void *buffer, size_t size, size_t flags) final
 Reads the first message from the endpoint or will yield until a message has been written.
 
int write (const void *buffer, size_t size, size_t flags) final
 Handles writing to the message endpoint resource by writing the buffer as a message.
 
- Public Member Functions inherited from MaxOS::processes::Resource
 Resource (const string &name, size_t flags, resource_type_t type)
 Constructs a new Resource object.
 
string name ()
 Gets the name of this resource.
 
resource_type_t type ()
 Gets the type of this resource.
 
virtual void open (size_t flags)
 Opens the resource.
 
virtual void close (size_t flags)
 Closes the resource.
 

Detailed Description

A endpoint that allows processes to queue messages on.

Definition at line 54 of file ipc.h.

Constructor & Destructor Documentation

◆ SharedMessageEndpoint()

SharedMessageEndpoint::SharedMessageEndpoint ( const string name,
size_t  size,
resource_type_t  type 
)

Creates a new shared message endpoint.

Parameters
nameThe name of the endpoint
sizeThe size of messages that can be sent
typeThe type of resource

Definition at line 112 of file ipc.cpp.

113: Resource(name, size, type)
114{
115
116}
Represents a generic resource that can be opened, closed, read from and written to.
Definition resource.h:29
string name()
Gets the name of this resource.
Definition resource.cpp:79
resource_type_t type()
Gets the type of this resource.
Definition resource.cpp:88

◆ ~SharedMessageEndpoint()

SharedMessageEndpoint::~SharedMessageEndpoint ( )
final

Destroys the shared message endpoint and frees all messages.

Definition at line 121 of file ipc.cpp.

121 {
122
123 // Free the messages
124 for(auto& message : m_queue)
126}
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22

Member Function Documentation

◆ read()

int SharedMessageEndpoint::read ( void buffer,
size_t  size,
size_t  flags 
)
finalvirtual

Reads the first message from the endpoint or will yield until a message has been written.

Parameters
bufferWhere to write the message to
sizeMax size of the message to be read
flagsUnused
Returns
The amount of bytes read

Reimplemented from MaxOS::processes::Resource.

Definition at line 136 of file ipc.cpp.

136 {
137
138 // Wait for a message
139 if(m_queue.empty())
140 return -1 * (int)resource_error_base_t::SHOULD_BLOCK;
141
142 // Read the message into the buffer
143 buffer_t* message = m_queue.pop_front();
144 size_t readable = size > message->capacity() ? message->capacity() : size;
145 memcpy(buffer, message -> raw(), readable);
146
147 return readable;
148}
Wrapper class for a region of bytes in memor in an attempt to add some memory safety....
Definition buffer.h:25
void * memcpy(void *destination, const void *source, uint64_t num)
Copies a block of memory from one location to another.
Definition memoryIO.cpp:165

References memcpy().

◆ write()

int SharedMessageEndpoint::write ( const void buffer,
size_t  size,
size_t  flags 
)
finalvirtual

Handles writing to the message endpoint resource by writing the buffer as a message.

Parameters
bufferThe message to write to the endpoint
sizeThe size of the message
flagsUnused
Returns
The amount of bytes written

Reimplemented from MaxOS::processes::Resource.

Definition at line 158 of file ipc.cpp.

158 {
159
160 m_message_lock.lock();
161
162 // Create the message
163 auto* new_message = new buffer_t(size);
164 new_message->copy_from(buffer, size);
165 m_queue.push_back(new_message);
166
167 m_message_lock.unlock();
168 return size;
169}
void lock()
Lock the spinlock once it is available.
Definition spinlock.cpp:24
void unlock()
Unlock the spinlock.
Definition spinlock.cpp:32

References MaxOS::common::Spinlock::lock(), and MaxOS::common::Spinlock::unlock().


The documentation for this class was generated from the following files: