// New incoming Ginger Client public void AcceptCallback(IAsyncResult ar) { try { // Signal the main thread to continue. allDone.Set(); // Get the socket that handles the client request. Socket listener = (Socket)ar.AsyncState; Socket socket = listener.EndAccept(ar); // Create the state object - one per Ginger client GingerSocketInfo gingerSocketInfo = new GingerSocketInfo(); gingerSocketInfo.Socket = socket; gingerSocketInfo.SessionID = Guid.NewGuid(); // Create new session id gingerSocketInfo.MessageHandler = MessageHandler; gingerSocketInfo.Receive(); Clients.Add(gingerSocketInfo); } catch (Exception ex) { if (isClosing) { return; } Console.WriteLine("AcceptCallback Error: " + ex.Message); } }
public void Connect(string IP, int port) { try { //Local host IPAddress ipAddress = IPAddress.Parse(IP); IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); Socket socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // Connect to Ginger Server async socket.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), socket); mConnectDone.WaitOne(); //start waiting for incoming data async - non blocking // Create the state object. mGingerSocketInfo = new GingerSocketInfo(); mGingerSocketInfo.Socket = socket; mGingerSocketInfo.MessageHandler = MessageHandler; mGingerSocketInfo.Receive(); // Now Ginger client is ready for incoming data mConnected = true; // if there is code here it will run - no wait //TODO: handshake: version, security, encryption NewPayLoad HandShake1 = new NewPayLoad("GetSession", "v1"); HandShake1.PaylodType = NewPayLoad.ePaylodType.SocketRequest; NewPayLoad RC = SendSocketPayLoad(HandShake1); if (RC.Name == "SessionID") { mGingerSocketInfo.SessionID = RC.GetGuid(); } else { throw new Exception("Error in connecting to GingerSocketServer invalid Session"); } } catch (Exception e) { Console.WriteLine("Error: " + e.Message); throw e; } }
// New incoming Ginger Client public void AcceptCallback(IAsyncResult ar) { // Signal the main thread to continue. allDone.Set(); // Get the socket that handles the client request. Socket listener = (Socket)ar.AsyncState; Socket socket = listener.EndAccept(ar); // Create the state object - one per Ginger client GingerSocketInfo gingerSocketInfo = new GingerSocketInfo(); gingerSocketInfo.Socket = socket; gingerSocketInfo.SessionID = Guid.NewGuid(); // Create new session id gingerSocketInfo.MessageHandler = MessageHandler; gingerSocketInfo.Receive(); Clients.Add(gingerSocketInfo); }
/// <summary> /// Connect to Services Grid /// Retry mechanism will retry every 5 seconds up to total 30 seconds /// </summary> /// <param name="IP"></param> /// <param name="port"></param> public void Connect(string IP, int port) { try { //Local host IPAddress ipAddress = IPAddress.Parse(IP); IPEndPoint remoteIP = new IPEndPoint(ipAddress, port); Socket socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); Console.WriteLine("Connecting to: " + remoteIP + ":" + port); // Connect to Ginger Server async, retry max 30 seconds Stopwatch stopwatch = Stopwatch.StartNew(); int retrycount = 0; while (!socket.Connected && stopwatch.ElapsedMilliseconds < 30000) { socket.BeginConnect(remoteIP, new AsyncCallback(ConnectCallback), socket); mConnectDone.WaitOne(); if (!socket.Connected) { retrycount++; Console.WriteLine("Connect retry #" + retrycount); Thread.Sleep(5000); } } if (socket.Connected) { // Now Ginger client is ready for incoming data mConnected = true; } else { Console.WriteLine("Failed to connect, exiting"); mConnected = false; return; } //start waiting for incoming data async - non blocking // Create the state object. mGingerSocketInfo = new GingerSocketInfo(); mGingerSocketInfo.Socket = socket; mGingerSocketInfo.MessageHandler = MessageHandler; mGingerSocketInfo.Receive(); // not blocking // if there is code here it will run - no wait //TODO: handshake: version, security, encryption NewPayLoad HandShake1 = new NewPayLoad("GetSession", "v1"); HandShake1.PaylodType = NewPayLoad.ePaylodType.SocketRequest; NewPayLoad RC = SendSocketPayLoad(HandShake1); if (RC.Name == "SessionID") { mGingerSocketInfo.SessionID = RC.GetGuid(); } else { throw new Exception("Error in connecting to GingerSocketServer invalid Session"); } } catch (Exception e) { Console.WriteLine("Error: " + e.Message); throw e; } }