// return listener if possible; set callback to null if no callback is needed public SockMgr BeginBuildTcp(SockFactoryOptions options, SocketRole socketRole, SockMgr.SockMgrConnectEventHandler connectCallback = null, SockMgr.SockMgrAcceptEventHandler acceptCallback = null, object callbackState = null) { SockMgr listenerMgr = null; _sockFactory.SetOptions(options); switch (socketRole) { case SocketRole.Client: if (options.TimesToTry > 0) { _sockFactory.BuildTcpClient(connectCallback, callbackState); } break; case SocketRole.Listener: listenerMgr = _sockFactory.GetTcpListener(); _sockFactory.ServerAccept(listenerMgr, acceptCallback, callbackState); RegisterNewSockMgr(listenerMgr); break; } return(listenerMgr); }
void BuildClientConsole() { SockFactoryOptions options = new SockFactoryOptions(); // collecting config Console.WriteLine("Enter server IP address (leave blank for 127.0.0.1:11000)"); Console.Write("> "); string ipAddr = Console.ReadLine(); int timesToTry = -1; if (ipAddr == "") { try { options.ListenerIpAddress = IPAddress.Parse("127.0.0.1"); } catch (FormatException) { Console.WriteLine("[Error] Invalid Input"); return; } options.ListenerPort = 11000; timesToTry = 1; } else { Console.WriteLine("Enter server port"); Console.Write("> "); int remotePort; try { remotePort = int.Parse(Console.ReadLine()); } catch (FormatException) { Console.WriteLine("[Error] Invalid Input"); return; } Console.WriteLine("Enter local port (leave blank for auto)"); Console.Write("> "); string localPortStr = Console.ReadLine(); Console.WriteLine("Enter how many times to try (leave blank for trying once)"); Console.Write("> "); string timesToTryStr = Console.ReadLine(); if (timesToTryStr == "") { timesToTry = 1; } else { try { timesToTry = int.Parse(timesToTryStr); } catch (FormatException) { Console.WriteLine("[Error] Invalid Input"); return; } } try { if (localPortStr == "") { try { options.ListenerIpAddress = IPAddress.Parse(ipAddr); } catch (FormatException) { Console.WriteLine("[Error] Invalid Input"); return; } options.ListenerPort = remotePort; } else { try { options.ListenerIpAddress = IPAddress.Parse(ipAddr); } catch (FormatException) { Console.WriteLine("[Error] Invalid Input"); return; } options.ListenerPort = remotePort; try { options.ClientPort = int.Parse(localPortStr); } catch (FormatException) { Console.WriteLine("[Error] Invalid Input"); return; } } } catch (SocketException ex) { switch ((SocketError)ex.ErrorCode) { case SocketError.InvalidArgument: Console.WriteLine("[Error] An invalid IP address was specified"); break; default: Console.WriteLine("[Error] SocketException"); break; } } } options.TimesToTry = timesToTry; options.ProtocolFactory = new Protocol.DefaultProtocolFactory(_protocolOptions); // begin to build _sockController.BeginBuildTcp(options, SocketRole.Client, CallbackTest, null, 1); }
void BuildListenerConsole() { SockFactoryOptions options = new SockFactoryOptions(); // collect config Console.WriteLine("[Build Listener]"); Console.WriteLine("1. TCP Server"); Console.WriteLine("2. UDP Server"); Console.Write("> "); string sel = Console.ReadLine(); Console.WriteLine("Enter listening IP address (leave blank for 0.0.0.0)"); Console.Write("> "); string localIpAddr = Console.ReadLine(); if (localIpAddr == "") { localIpAddr = "0.0.0.0"; } Console.WriteLine("Enter server port (leave blank for 11000)"); Console.Write("> "); string localPortStr = Console.ReadLine(); int localPort; if (localPortStr == "") { localPort = 11000; } else { try { localPort = int.Parse(localPortStr); } catch (FormatException) { Console.WriteLine("[Error] Invalid Input"); return; } } try { options.ListenerIpAddress = IPAddress.Parse(localIpAddr); } catch (FormatException) { Console.WriteLine("[Error] Invalid Input"); return; } options.ListenerPort = localPort; options.ProtocolFactory = new Protocol.DefaultProtocolFactory(_protocolOptions); // begin to build switch (sel) { case "1": _sockController.BeginBuildTcp(options, SocketRole.Listener); break; case "2": // TODO break; default: break; } }
public void SetOptions(SockFactoryOptions options) { _options = options; }