public string Transact(string dpInputString) { DataPacket dpInputPacket = new DataPacket(); try { dpInputPacket = JsonConvert.DeserializeObject <DataPacket>(dpInputString); //dpInputPacket.Message = "ACK. " + dpInputPacket.Message; //dpInputPacket.TimeStamp = DateTime.Now; //string strSenderName = dpInputPacket.RecepientID; //string strReceiverName = dpInputPacket.SenderID; //dpInputPacket.SenderID = strSenderName; //dpInputPacket.RecepientID = strReceiverName; Console.WriteLine("Enqueue web St. " + DateTime.Now.ToString("yyyyMMdd HH:mm:ss:fff") + " Msg: " + ReceivedMessages.Count + " TID: " + Thread.CurrentThread.ManagedThreadId); ReceivedMessages.Enqueue(dpInputPacket); Console.WriteLine("Enqueue web St. " + DateTime.Now.ToString("yyyyMMdd HH:mm:ss:fff") + " Msg: " + ReceivedMessages.Count + " TID: " + Thread.CurrentThread.ManagedThreadId); DataPacket Ack = new DataPacket(); Ack = JsonConvert.DeserializeObject <DataPacket>(dpInputString); Ack.SenderID = InstanceName; Ack.SenderType = AppType.Server; Ack.RecepientID = dpInputPacket.SenderID; Ack.RecepientType = dpInputPacket.SenderType; Ack.Message = "ACK." + Ack.Message; Ack.Type = PacketType.Acknowledge; Ack.TimeStamp = DateTime.Now; return(JsonConvert.SerializeObject(Ack)); } catch (Exception ex) { dpInputPacket.Message = "Error while processing input." + ex.Message; } return(JsonConvert.SerializeObject(dpInputPacket)); }
// Lives in a thread! public void ReadMessages() { //CheckersMessage nextMessage; NetworkStream netStream = Client.GetStream(); int bytesRead = 0; byte[] bufferFiller = new byte[2048]; // 2048 is just the read batch size, doesn't really matter how big it is while (Client.Connected) { // fill up the read buffer. okay to block here! while (netStream.DataAvailable) { // bufferFiller is just an intermediate data location, so overwriting it is fine. bytesRead = netStream.Read(bufferFiller, 0, bufferFiller.Length); ReadBuffer.Write(bufferFiller, 0, bytesRead); ReadBuffer.Seek(0, SeekOrigin.Begin); } // get a message if there is one. If there's an InvalidProtocolBufferException, trust/hope // that it happened because a delimited message was only partially transmitted upon // calling ParseDelimitedFrom try{ ReceivedMessages.Enqueue(parser.ParseDelimitedFrom(ReadBuffer)); //nextMessage = parser.ParseDelimitedFrom(ReadBuffer); //ReceivedMessages.Enqueue(nextMessage); ClearReadBufferBeforeCurrentPosition(); } catch (InvalidProtocolBufferException) { // Message wasn't ready; //Console.WriteLine (e.Message); } // don't check too frequently Thread.Sleep(100); } }
public string TransactMin(string Sender, string Receiver, string Message) { DataPacket dpInputPacket = new DataPacket(); DataPacket dpOutputPacket = new DataPacket(); try { dpInputPacket.IsManualCmd = true; dpInputPacket.Message = Message; dpInputPacket.MsgID = DateTime.Now.ToString("ss"); dpInputPacket.SenderID = Sender; dpInputPacket.SenderType = AppType.Moderator; dpInputPacket.RecepientID = Receiver; dpInputPacket.RecepientType = AppType.Client; dpInputPacket.Type = PacketType.Request; dpInputPacket.TimeStamp = DateTime.Now; Console.WriteLine("Enqueue web2 St. " + DateTime.Now.ToString("yyyyMMdd HH:mm:ss:fff") + " Msg: " + ReceivedMessages.Count + " TID: " + Thread.CurrentThread.ManagedThreadId); ReceivedMessages.Enqueue(dpInputPacket); Console.WriteLine("Enqueue web2 St. " + DateTime.Now.ToString("yyyyMMdd HH:mm:ss:fff") + " Msg: " + ReceivedMessages.Count + " TID: " + Thread.CurrentThread.ManagedThreadId); dpOutputPacket.IsManualCmd = true; dpOutputPacket.Message = "ACK." + Message; dpOutputPacket.MsgID = DateTime.Now.ToString("ss"); dpOutputPacket.SenderID = InstanceName; dpOutputPacket.SenderType = AppType.Server; dpOutputPacket.RecepientID = Sender; dpOutputPacket.RecepientType = AppType.Moderator; dpOutputPacket.TimeStamp = DateTime.Now; dpOutputPacket.Type = PacketType.Acknowledge; return(JsonConvert.SerializeObject(dpOutputPacket)); } catch (Exception ex) { dpOutputPacket.Message = "Error while processing input." + ex.Message; dpOutputPacket.SenderID = InstanceName; dpOutputPacket.SenderType = AppType.Server; dpOutputPacket.TimeStamp = DateTime.Now; dpOutputPacket.Type = PacketType.Acknowledge; } return(JsonConvert.SerializeObject(dpOutputPacket)); }