Created
June 7, 2013 16:41
-
-
Save AngryAnt/5730611 to your computer and use it in GitHub Desktop.
Broadcaster / Seeker basics for sharing and joining games on your local network. Example code for "The implicit local network interface" blog post on AngryAnt.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (m_BroadcastTimer == null) | |
// Setup broadcast of hosting service | |
{ | |
m_BroadcastSocket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); | |
try | |
{ | |
IPAddress broadcastGroup = IPAddress.Parse (kBroadcastGroup); | |
m_BroadcastSocket.SetSocketOption ( | |
SocketOptionLevel.IP, | |
SocketOptionName.AddMembership, | |
new MulticastOption (broadcastGroup) | |
); | |
m_BroadcastSocket.SetSocketOption ( | |
SocketOptionLevel.IP, | |
SocketOptionName.MulticastTimeToLive, | |
1 | |
); | |
m_BroadcastSocket.Connect (new IPEndPoint (broadcastGroup, kBroadcastPort)); | |
} | |
catch (Exception e) | |
{ | |
Debug.LogError (string.Format ( | |
"Exception when attempting to set up service broadcast ({0}:{1}): {2}", | |
kBroadcastGroup, | |
kBroadcastPort, | |
e | |
)); | |
return false; | |
} | |
m_BroadcastTimer = new Timer (kBroadcastDelay); | |
m_BroadcastTimer.Elapsed += new ElapsedEventHandler ( | |
(object s, ElapsedEventArgs e) => | |
{ | |
m_BroadcastSocket.Send (GetBroadcastBytes ()); | |
} | |
); | |
m_BroadcastTimer.Enabled = true; | |
} | |
return true; | |
// ... | |
byte[] GetBroadcastBytes () | |
{ | |
/* | |
Return byte array containing IP and port of our host socket plus meta data | |
such as current level and perhaps location (for portal placement) | |
*/ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (m_SeekerSocket == null) | |
{ | |
m_SeekerSocket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); | |
try | |
{ | |
m_SeekerSocket.Bind (new IPEndPoint (IPAddress.Any, kBroadcastPort)); | |
IPAddress broadcastGroup = IPAddress.Parse (kBroadcastGroup); | |
m_SeekerSocket.SetSocketOption ( | |
SocketOptionLevel.IP, | |
SocketOptionName.AddMembership, | |
new MulticastOption (broadcastGroup, IPAddress.Any) | |
); | |
ReadBroadcasts (); | |
} | |
catch (Exception e) | |
{ | |
Debug.LogError (string.Format ( | |
"Exception when attempting to set up service listener ({0}:{1}): {2}", | |
kBroadcastGroup, | |
kBroadcastPort, | |
e | |
)); | |
m_SeekerSocket.Dispose (); | |
} | |
} | |
// ... | |
void ReadBroadcasts () | |
{ | |
/* | |
Now set up periodic or event-driven reading from m_SeekerSocket, parsing | |
broadcast packages containing IP, port and meta data of locally available games | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment