Skip to content
This repository has been archived by the owner on Jan 14, 2021. It is now read-only.

Commit

Permalink
Fixed the SSDP tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
stbrowne authored and alexkay committed Jan 18, 2012
1 parent fa4f505 commit a61b0ac
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
16 changes: 13 additions & 3 deletions src/Mono.Ssdp/Mono.Ssdp/Mono.Ssdp/MulticastReader.cs
Expand Up @@ -47,14 +47,24 @@ internal void AsyncReadResult (SsdpSocket socket)


void AsyncReadResult (AsyncReceiveBuffer buffer) void AsyncReadResult (AsyncReceiveBuffer buffer)
{ {
buffer.Socket.BeginReceiveFrom (buffer, OnAsyncResultReceived); try {
buffer.Socket.BeginReceiveFrom (buffer, OnAsyncResultReceived);
} catch (ObjectDisposedException) {
// Socket disposed while we were receiving from it... just ignore this
}
} }


void OnAsyncResultReceived (IAsyncResult asyncResult) void OnAsyncResultReceived (IAsyncResult asyncResult)
{ {
var buffer = (AsyncReceiveBuffer)asyncResult.AsyncState; var buffer = (AsyncReceiveBuffer)asyncResult.AsyncState;
buffer.BytesReceived = buffer.Socket.EndReceiveFrom (asyncResult, ref buffer.SenderEndPoint);

try {
buffer.BytesReceived = buffer.Socket.EndReceiveFrom (asyncResult, ref buffer.SenderEndPoint);
} catch (ObjectDisposedException) {
// Socket already disposed... just ignore this and exit
return;
}

if (OnAsyncResultReceived (buffer)) { if (OnAsyncResultReceived (buffer)) {
AsyncReadResult (buffer); AsyncReadResult (buffer);
} else { } else {
Expand Down
44 changes: 23 additions & 21 deletions tests/Mono.Ssdp.Tests/ServerTests.cs
Expand Up @@ -44,14 +44,15 @@ public class ServerTests


readonly object mutex = new object (); readonly object mutex = new object ();


// static Socket CreateMulticastSocket () static Socket CreateMulticastSocket ()
// { {
// var socket = CreateSocket (); var socket = CreateSocket ();
// socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Broadcast, true); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
// socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 4); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
// socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption (ipaddress, 0)); socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 4);
// return socket; socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption (ipaddress, 0));
// } return socket;
}


static Socket CreateSocket () static Socket CreateSocket ()
{ {
Expand All @@ -63,21 +64,22 @@ static Socket CreateSocket ()
[Test] [Test]
public void AnnouncementTest () public void AnnouncementTest ()
{ {
using (var socket = CreateSocket ()) { using (var socket = CreateMulticastSocket ()) {
using (var server = new Server ()) { using (var server = new Server ()) {
socket.Bind (ip_endpoint);
var buffer = new byte[1024];
socket.BeginReceiveFrom (buffer, 0, buffer.Length, SocketFlags.None, ref endpoint,
result => {
lock (mutex) {
socket.EndReceiveFrom (result, ref endpoint);
var datagram = Encoding.ASCII.GetString (buffer, 0, buffer.Length);
Assert.IsTrue (datagram.StartsWith ("NOTIFY * HTTP/1.1\r\n"));
Monitor.Pulse (mutex);
}
}, null
);
lock (mutex) { lock (mutex) {
socket.Bind (endpoint);
var buffer = new byte[1024];
socket.BeginReceiveFrom (buffer, 0, buffer.Length, SocketFlags.None, ref endpoint,
result => {
lock (mutex) {
socket.EndReceiveFrom (result, ref endpoint);
var datagram = Encoding.ASCII.GetString (buffer, 0, buffer.Length);
Assert.IsTrue (datagram.StartsWith ("NOTIFY * HTTP/1.1\r\n"));
Monitor.Pulse (mutex);
}
}, null
);

server.Announce ("test-service", "test1", "http://localhost/"); server.Announce ("test-service", "test1", "http://localhost/");
if (!Monitor.Wait (mutex, TimeSpan.FromSeconds (5))) { if (!Monitor.Wait (mutex, TimeSpan.FromSeconds (5))) {
Assert.Fail ("The server notification timed out."); Assert.Fail ("The server notification timed out.");
Expand Down

0 comments on commit a61b0ac

Please sign in to comment.