private void GrabTransmissionInCache(object derp, ElapsedEventArgs e) { string msg; msg = ClientMsgCache.Read(CID, cType); try { if (msg != null) { bytes = Encoding.ASCII.GetBytes(msg); connection.Send(bytes); } timSendChk.Start(); } catch (Exception Ex) { GlobSyn.Log("FAILURE! Error while xmitting to client " + connection.RemoteEndPoint.ToString() + Environment.NewLine + "~~See Exception: " + Ex.Message); } SessionTime += 100; //add 100ms if (cType == ClientType.ConsumerClient && SessionTime / (5 * 60 * 1000) > 0) //over 5 minutes of connection? kill 'im { GlobSyn.Log("RemConClient reached max connection time (5 minutes) " + connection.RemoteEndPoint.ToString() + Environment.NewLine + "~~~Killing the connection"); bytes[0] = disposing ? (byte)0 : ThisConnectionSucks(); } }
private void BidiChat() { var syn = new System.Windows.Forms.Form(); //wow...I actually need a freaking form object instantiated here to get these timers to run on this thread -__- ISynchronizeInvoke synob = (ISynchronizeInvoke)syn; ClientMsgCache.ClientAdded(cType); GlobSyn.MsgFromClient(cType, CID, connection.RemoteEndPoint, "ClientSetup <EOF>"); timKeepAlive = new System.Timers.Timer(); timKeepAlive.SynchronizingObject = synob; timKeepAlive.Elapsed += new ElapsedEventHandler(KeepAliveTO); timKeepAlive.Interval = 1000 * 20; //at 20 seconds, 18 bytes per exchange, should be 2.3MB/month timKeepAlive.Start(); timSendChk = new System.Timers.Timer(); timSendChk.SynchronizingObject = synob; timSendChk.Elapsed += new ElapsedEventHandler(GrabTransmissionInCache); //check for message to send every 100ms timSendChk.Interval = 100; timSendChk.Start(); timRecMsgEscape = new System.Timers.Timer(); timRecMsgEscape.SynchronizingObject = synob; timRecMsgEscape.Elapsed += new ElapsedEventHandler(recMsgTO); timRecMsgEscape.Interval = 1000; connection.Blocking = false; try { while (true) { string retstr = null; do { Thread.Sleep(10); retstr = ReceiveWholeMsg(); } while (retstr == null); GlobSyn.MsgFromClient(cType, CID, connection.RemoteEndPoint, retstr); timKeepAlive.Stop(); timKeepAlive.Interval = 20000; timKeepAlive.Start(); //Thread.Sleep(100); //take a lil break before blocking on another receive } } catch (Exception Ex) { if (connection != null) { GlobSyn.Log("FAILED TCP Client Connection reading from " + connection.RemoteEndPoint.ToString() + Environment.NewLine + "~~See Exception: " + Ex.Message); } } }
public void SendToClients() { string msg = "ACK_Status <EOF>"; //CarState already in here msg = msg.Insert(msg.IndexOf(" <EOF>"), " RPIClientEndPoint:" + Ipep); msg = msg.Insert(msg.IndexOf(" <EOF>"), " CarState:" + CarState); msg = msg.Insert(msg.IndexOf(" <EOF>"), " RPIState:" + RPIState); msg = msg.Insert(msg.IndexOf(" <EOF>"), " InfoTime:" + InfoTime + '~'); ClientMsgCache.AddMessage(msg, ClientType.ConsumerClient); }
public TCPClientConn(Socket handle, ClientType ct) { connection = handle; connection.ReceiveTimeout = 500; //not presently used? maybe? I'm in "non-blocking" sychronous mode..whatever that means?!!???? cType = ct; SessionTime = 0; CID = ClientMsgCache.GenerateClientID(cType); connThread = new Thread(new ThreadStart(BidiChat)); //connThread.Priority = ThreadPriority.BelowNormal; connThread.IsBackground = true; connThread.Start(); }
private byte ThisConnectionSucks() { disposing = true;// don't let this thread call this thang more than once! ClientMsgCache.ClientRemoved(cType); GlobSyn.MsgFromClient(cType, CID, connection.RemoteEndPoint, "ClientKilled <EOF>"); timKeepAlive.Stop(); timRecMsgEscape.Stop(); timSendChk.Stop(); timKeepAlive.Dispose(); timRecMsgEscape.Dispose(); timSendChk.Dispose(); SelfDestruct(this); //if I abort the thread I am running on...wat..wait BRAIN MELT??!?!? will this be reached? put abort afterwards...lesse connThread.Abort(); return(1); }
public void OnMsgFromClient(ClientType ctSource, uint CID, EndPoint ipep, string msg) { if (ctSource == ClientType.ConsumerClient) { if (msg.Contains("ClientSetup")) { //ClientMsgCache.AddMessage(msg, ClientType.RPIProducerClient); parentForm.lsvRemConClients.Items.Add("CID: " + CID.ToString() + " - " + ipep.ToString()); //Package an ACK_Status of the RPI off to this client right away??! LatestRPI.SendToClients(); } else if (msg.Contains("ClientKilled")) { string removelistitem = "CID: " + CID.ToString() + " - " + ipep.ToString(); foreach (ListViewItem item in parentForm.lsvRemConClients.Items) { if (item.Text == removelistitem) { parentForm.lsvRemConClients.Items.Remove(item); break; } } //?? For now, turn off car when client DC's //LatestRPI.CarState = "OFF"; } else if (msg.Contains("ACK_Status")) //meh, just for knowing the remconclient is alive...but respond with a full ack from rpi { BackgroundBlipper bb = new BackgroundBlipper("CID: " + CID.ToString() + " - " + ipep.ToString(), ImaBlipAClient); string removelistitem = "CID: " + CID.ToString() + " - " + ipep.ToString(); foreach (ListViewItem item in parentForm.lsvRemConClients.Items) { if (item.Text == removelistitem) { item.BackColor = Color.LightGreen; break; } } //LatestRPI.SendToClients(); naw...he just responded to a req } else if (msg.Contains("StartCar")) { ClientMsgCache.AddMessage(msg, ClientType.RPIProducerClient); parentForm.txtTraffic.Text = ">> " + String.Format("{0:G}", DateTime.Now) + " - " + " (RemCon -> Rpi) " + msg + Environment.NewLine + parentForm.txtTraffic.Text; //?? For now pass back to client that he started the car, SHHHHH. He won't know //LatestRPI.CarState = "ON"; //LatestRPI.SendToClients(); } else if (msg.Contains("Debug <EOF>")) { ClientMsgCache.AddMessage(msg, ClientType.RPIProducerClient); parentForm.txtTraffic.Text = ">> " + String.Format("{0:G}", DateTime.Now) + " - " + " (RemCon -> Rpi) " + msg + Environment.NewLine + parentForm.txtTraffic.Text; } else { return; } } else if (ctSource == ClientType.RPIProducerClient) { if (msg.Contains("ClientSetup")) // { //update RPI info LatestRPI.Ipep = ipep.ToString(); LatestRPI.InfoTime = String.Format("{0:G}", DateTime.Now); LatestRPI.RPIState = "UP"; //prep/send message on to remcon clients LatestRPI.SendToClients(); //update form parentForm.lblRPIStatus.Text = LatestRPI.RPIState; parentForm.lblRPIStatus.BackColor = Color.Green; parentForm.lblRPIEndPoint.Text = LatestRPI.Ipep; parentForm.lblRPILastCom.Text = LatestRPI.InfoTime; } else if (msg.Contains("ClientKilled")) { // parentForm.lblRPIEndPoint.Text = "N/A"; leave the IP alone? Will just update with next ACK_Status I s'pose LatestRPI.InfoTime = String.Format("{0:G}", DateTime.Now); LatestRPI.RPIState = "DOWN"; //prep/send message on to remcon clients LatestRPI.SendToClients(); //update form parentForm.lblRPIStatus.Text = "DOWN"; parentForm.lblRPIStatus.BackColor = Color.Red; } else if (msg.Contains("ACK_Status")) //Arrives With: "CarState:""ON" or "OFF" ----- Also leaves with: "RPIClientEndPoint:"IP:PORT , "InfoTime:"LatestRPI.InfoTime , "RPIState:""UP" or "DOWN" { //update RPI info LatestRPI.Ipep = ipep.ToString(); LatestRPI.InfoTime = String.Format("{0:G}", DateTime.Now); LatestRPI.RPIState = "UP"; LatestRPI.CarState = msg.Substring(msg.IndexOf("CarState:")).Split(' ')[0].Replace("CarState:", ""); //prep/send message on to remcon clients LatestRPI.SendToClients(); //update form parentForm.lblRPIEndPoint.Text = LatestRPI.Ipep; parentForm.lblRPILastCom.Text = LatestRPI.InfoTime; parentForm.pcbCarState.Image = LatestRPI.CarState == "ON" ? SocketsExchangeService.Properties.Resources.Car_On : SocketsExchangeService.Properties.Resources.Car_Off; //Log the transaction, MEH probably shouldn't log every ACK_Status from the RPI. updating the com time is fine //parentForm.txtTraffic.Invoke((MethodInvoker)(() => parentForm.txtTraffic.Text = ">> " + now + " - " + "(RPI -> RemConClients)" + " ~~Contents: " + msg + Environment.NewLine + parentForm.txtTraffic.Text)); } else { } } }