public void CloseConnection() { if (GingerSocket.SocketConnected(mClientSocket)) { // send graceful disconnect PayLoad PL = new PayLoad("GingerSocket", "Disconnect"); SendPayLoad(PL); } if (mGingerSocketMonitorWindow != null) { mGingerSocketMonitorWindow.Dispatcher.Invoke(() => { mGingerSocketMonitorWindow.DelayedClose(); }); } mClientSocket.Close(); }
public void HandleClientComm(object client) { TcpClient tcpClient = (TcpClient)client; // TODO: need to decide when to go out of the loop !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! while (true) { try { PayLoad PL = GingerSocket.ReadPayLoad(tcpClient); if (PL.Name == "GingerSocket") { // This is Client/Server Protocol talks string txt = PL.GetValueString(); if (txt == "Disconnect") { tcpClient.Close(); break; } } else { PayLoad PLRC = ProcessMessage(PL); if (PLRC != null) // if async then it will be null? or we must return something, for async we should return start processing - or key id of req to pair later { SendMessage(tcpClient, PLRC); } } } catch (Exception ex) { //TODO: handle // Client cut the connection or some err //a socket error has occurred // or any other server err PayLoad PLErr = PayLoad.Error("Error - " + ex.Message); SendMessage(tcpClient, PLErr); } } tcpClient.Close(); }
public PayLoad SendPayLoad(PayLoad pl) { if (IsGingerSocketLogging) { GingerSocketLog GSL = new GingerSocketLog(); GSL.LogType = "Send"; GSL.SetPayLoad(pl); GingerSocketLogs.Add(GSL); } try { Stopwatch st = new Stopwatch(); st.Start(); GingerSocket.Send(mNetworkStream, pl); PayLoad plrc = GingerSocket.ReadPayLoad(mClientSocket); st.Stop(); if (IsGingerSocketLogging) { GingerSocketLog GSL = new GingerSocketLog(); GSL.SetPayLoad(plrc); GSL.LogType = "Recv"; GSL.Elapsed = st.ElapsedMilliseconds; GingerSocketLogs.Add(GSL); } return(plrc); } catch (Exception ex) { // Raise event so consumer can show a message or close nicely or retry connect OnMessage(GingerSocket.eProtocolMessageType.CommunicationError, ex.Message); return(PayLoad.Error("Error in SendPayLoad:" + ex.Message)); } }
private void SendMessage(TcpClient tcpClient, PayLoad PL) { NetworkStream clientStream = tcpClient.GetStream(); GingerSocket.Send(clientStream, PL); }