Merge pull request #1 from deavmi/hotfix/migrate_socket

Hotfix/migrate socket
This commit is contained in:
Tristan B. Velloza Kildaire 2023-09-22 17:38:20 +02:00 committed by GitHub
commit 6652cb1467
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 92 additions and 24 deletions

View File

@ -17,20 +17,13 @@ public class CoapClient
/**
* CoAP server endpoint
*/
private Address address;
package Address address;
/**
* Running status
*/
package bool running;
/**
* The datagram socket
*
* TODO: Move this into the messaging layer
*/
package Socket socket;
/**
* The messaging layer which provides
* request-response message match-ups
@ -94,18 +87,11 @@ public class CoapClient
*/
private void init()
{
// TODO: IF connect fails then don't start messaging
this.socket = new Socket(this.address.addressFamily(), SocketType.DGRAM, ProtocolType.UDP);
// this.socket.blocking(true);
this.socket.connect(address);
// Set status to running
this.running = true;
// Start the messaging layer
this.messaging.start();
this.messaging.begin();
}
/**
@ -121,11 +107,8 @@ public class CoapClient
// Set status to not running
this.running = false;
// Shutdown the socket (stopping the messaging layer)
this.socket.shutdown(SocketShutdown.BOTH);
// Unbind (disallow incoming datagrams to source port (from device))
this.socket.close();
// Shutdown the messaging layer
this.messaging.close();
// TODO: We must wake up other sleeprs with an error
// (somehow, pass it in, flag set)
@ -229,7 +212,7 @@ public class CoapClient
private void transmitRequest(CoapRequest request)
{
// Encode the request packet and send it
this.socket.send(request.getRequestPacket().getBytes());
this.messaging.send(request.getRequestPacket());
// Now start ticking the timer
request.startTime();

View File

@ -11,6 +11,9 @@ import doap.client.request : CoapRequest;
import std.stdio;
import std.socket : Socket, SocketSet;
import std.socket : Address;
import std.socket : Socket, Address, SocketType, ProtocolType, getAddress, parseAddress, InternetAddress, SocketShutdown;
// TODO: Generalize this and then make
// ... a UDP version of it
@ -29,6 +32,16 @@ class CoapMessagingLayer : Thread
*/
private CoapClient client;
/**
* Running status
*/
private bool running; // TODO: Check volatility
/**
* The datagram socket
*/
private Socket socket;
/**
* Constructs a new messaging layer instance
* associated with the provided client
@ -42,6 +55,78 @@ class CoapMessagingLayer : Thread
this.client = client;
}
/**
* Retrieves the CoAP endpoint the client is
* connected to
*
* Returns: the endpoint address
*/
protected final Address getEndpointAddress() // Final in Interface
{
return this.client.address;
}
/**
* Starts the messaging layer by starting
* the underlying transport and then the
* reader loop
*/
public void begin() // Candidate for Interface
{
// TODO: Handle socket errors nicely?
// Set status to running
this.running = true;
// TODO: IF connect fails then don't start messaging
this.socket = new Socket(getEndpointAddress().addressFamily(), SocketType.DGRAM, ProtocolType.UDP);
// this.socket.blocking(true);
this.socket.connect(getEndpointAddress());
// Start the thread (TODO: In future hide threading)
this.start();
}
/**
* Transmit the provided packet
*
* Params:
* packet = the `CoapPacket`
* to transmit
*/
public void send(CoapPacket packet) // Candidate for Interface
{
// Encode the packet and send the bytes
ubyte[] encodedPacket = packet.getBytes();
this.socket.send(encodedPacket);
}
/**
* Stops the messaging layer by
* stopping the underlying network
* transport and therefore the
* reading loop
*
* Blocks till the reading loop
* has terminated
*/
public void close() // Candidate for Interface
{
// Set status to not running
this.running = false;
// Shutdown the socket (stopping the messaging layer)
this.socket.shutdown(SocketShutdown.BOTH);
// Unbind (disallow incoming datagrams to source port (from device))
this.socket.close();
// Wait till the reading-loop thread exits
this.join();
}
/**
* Reading loop which reads datagrams
* from the socket
@ -89,13 +174,13 @@ class CoapMessagingLayer : Thread
SocketFlags flags = cast(SocketFlags)(SocketFlags.PEEK | MSG_TRUNC);
byte[] data;
data.length = 1; // At least one else never does underlying recv()
ptrdiff_t dgramSize = client.socket.receive(data, flags);
ptrdiff_t dgramSize = this.socket.receive(data, flags);
// If we have received something then dequeue it of the peeked length
if(dgramSize > 0)
{
data.length = dgramSize;
client.socket.receive(data);
this.socket.receive(data);
writeln("received size: ", dgramSize);
writeln("received bytes: ", data);