Created
December 23, 2013 22:28
-
-
Save DevJohnC/8105906 to your computer and use it in GitHub Desktop.
Basic connection testing for reliable udp
This file contains hidden or 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
| // | |
| // Author: John Carruthers ([email protected]) | |
| // | |
| // Copyright (C) 2013 John Carruthers | |
| // | |
| // Permission is hereby granted, free of charge, to any person obtaining | |
| // a copy of this software and associated documentation files (the | |
| // "Software"), to deal in the Software without restriction, including | |
| // without limitation the rights to use, copy, modify, merge, publish, | |
| // distribute, sublicense, and/or sell copies of the Software, and to | |
| // permit persons to whom the Software is furnished to do so, subject to | |
| // the following conditions: | |
| // | |
| // The above copyright notice and this permission notice shall be | |
| // included in all copies or substantial portions of the Software. | |
| // | |
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
| // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
| // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
| // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
| // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
| // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
| // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| // | |
| using System; | |
| using System.Net; | |
| using System.Net.Sockets; | |
| using FragLabs.Network.ReliableUdp; | |
| using FragLabs.Network.ReliableUdp.Synchronization; | |
| namespace FragLabs.Network.SocketTest | |
| { | |
| class Program | |
| { | |
| /// <summary> | |
| /// Client 1. | |
| /// </summary> | |
| private static Socket _client1; | |
| /// <summary> | |
| /// Synchronization client for client1. | |
| /// </summary> | |
| private static ISynchronizationClient _clientSync1; | |
| /// <summary> | |
| /// Client 2. | |
| /// </summary> | |
| private static Socket _client2; | |
| /// <summary> | |
| /// Synchronization client for client2. | |
| /// </summary> | |
| private static ISynchronizationClient _clientSync2; | |
| /// <summary> | |
| /// Connection count. | |
| /// </summary> | |
| private static int _connectCount; | |
| static void Main(string[] args) | |
| { | |
| /* | |
| * Step 1: Create endpoints for communcations. | |
| */ | |
| var clientEndpoint1 = new IPEndPoint(IPAddress.Any, 23124); | |
| var clientEndpoint2 = new IPEndPoint(IPAddress.Any, 23125); | |
| /* | |
| * Step 2: Create the syncronization clients. | |
| * These handle the initial connection state of the reliable udp streams and typically | |
| * use an out of band communcations server when used over the internet. | |
| * For local use a basic client that requires prior knowledge of endpoints can be used. | |
| */ | |
| _clientSync1 = new BasicPunchSynchronizationClient(clientEndpoint2); | |
| _clientSync2 = new BasicPunchSynchronizationClient(clientEndpoint1); | |
| /* | |
| * Step 3: Create the sockets. | |
| */ | |
| _client1 = new ReliableUdpSocket(clientEndpoint1, _clientSync1); | |
| _client2 = new ReliableUdpSocket(clientEndpoint2, _clientSync2); | |
| /* | |
| * Step 4: Connect the sockets. | |
| */ | |
| var saea1 = new SocketAsyncEventArgs(); | |
| saea1.RemoteEndPoint = _clientSync1.RemoteEndpoint; | |
| saea1.Completed += ConnectComplete; | |
| var saea2 = new SocketAsyncEventArgs(); | |
| saea2.RemoteEndPoint = _clientSync2.RemoteEndpoint; | |
| saea2.Completed += ConnectComplete; | |
| _client1.ConnectAsync(saea1); | |
| _client2.ConnectAsync(saea2); | |
| Console.ReadLine(); | |
| } | |
| private static void ConnectComplete(object sender, SocketAsyncEventArgs socketAsyncEventArgs) | |
| { | |
| if (socketAsyncEventArgs.SocketError == SocketError.Success) | |
| { | |
| Console.WriteLine("Connection established."); | |
| if (++_connectCount == 2) | |
| { | |
| Console.WriteLine("Sockets ready for data"); | |
| } | |
| } | |
| else | |
| { | |
| Console.WriteLine("Connection failed."); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment