Ejemplo n.º 1
0
        /*
         * while (client.Connected && running)
         *  {
         *      int toRead = client.Available;
         *      if (toRead > 0)
         *      {
         *          sesion.LastSeen = DateTime.Now.Ticks;
         *          sesion.Notified = false;
         *          buffer[bufPos++] = (byte)stream.ReadByte();
         *          if (buffer[bufPos - 1] == '\n')
         *          {
         *              string recString = Encoding.ASCII.GetString(buffer, 0, bufPos - 1);
         *              bufPos = 0;
         *              recString = recString.Trim();
         *
         *              Log.WriteLine(sesion.Address + ">" + recString);
         *
         *              if (recString.StartsWith(CoreUtil.MUVC_STRING))
         *              {
         *                  if (recString == CoreUtil.DISCONNECT_STRING)
         *                  {
         *                      client.Close();
         *                      lock (OUTqueue)
         *                      {
         *                          OUTqueue.Remove(sesion);
         *                      }
         *                      lock (sesions)
         *                      {
         *                          sesions.Remove(sesion);
         *                      }
         *
         *                      Log.WriteLine("Client DSC:" + sesion.Address);
         *
         *                      return;
         *                  }
         *                  else
         *                  {
         *                      //handle MUVC commands
         *                  }
         *              }
         *              else
         *              {
         *                  if (MessageRecieved == null || eventpush)
         *                  {
         *                      INqueue.Enqueue(new Message(recString, sesion));
         *                  }
         *                  MessageRecieved?.Invoke(recString, sesion);
         *
         *              }
         *          }
         *      }
         *      if (!OUTQ.IsEmpty())
         *      {
         *          string trString = OUTQ.Dequeue().Contents;
         *          byte[] sendbuf = Encoding.ASCII.GetBytes(trString + "\n");
         *          stream.Write(sendbuf, 0, sendbuf.Length);
         *          if (trString == CoreUtil.DISCONNECT_STRING)
         *          {
         *              client.Close();
         *              lock (OUTqueue)
         *              {
         *                  OUTqueue.Remove(sesion);
         *              }
         *              lock (sesions)
         *              {
         *                  sesions.Remove(sesion);
         *              }
         *
         *              Log.WriteLine("Client Terminated:" + sesion.Address);
         *
         *              return;
         *          }
         *
         *          Log.WriteLine(sesion.Address + "<" + trString);
         *      }
         *      if (TimeToLiveSeconds >= 0 && (DateTime.Now.Ticks - sesion.LastSeen) / CoreUtil.TICKS_PER_SECOND > TimeToLiveSeconds)
         *      {
         *          if (sesion.Notified)
         *          {
         *              client.Close();
         *              lock (OUTqueue)
         *              {
         *                  OUTqueue.Remove(sesion);
         *              }
         *              lock (sesions)
         *              {
         *                  sesions.Remove(sesion);
         *              }
         *
         *              Log.WriteLine("Client Timed Out:" + sesion.Address);
         *
         *              return;
         *          }
         *          else
         *          {
         *              byte[] Ksendbuf = Encoding.ASCII.GetBytes(CoreUtil.KEEPALIVE_STRING + "\n");
         *              stream.Write(Ksendbuf, 0, Ksendbuf.Length);
         *              sesion.LastSeen = DateTime.Now.Ticks;
         *              sesion.Notified = true;
         *
         *              Log.WriteLine("Client Notified:" + sesion.Address);
         *          }
         *      }
         *
         *  }
         *  byte[] Dsendbuf = Encoding.ASCII.GetBytes(CoreUtil.DISCONNECT_STRING + "\n");
         *  stream.Write(Dsendbuf, 0, Dsendbuf.Length);
         *  client.Close();
         *  lock (OUTqueue)
         *  {
         *      OUTqueue.Remove(sesion);
         *  }
         *  lock (sesions)
         *  {
         *      sesions.Remove(sesion);
         *  }
         */

        private void ClientThread(object data)
        {
            TcpClient client            = (TcpClient)data;
            Sesion    sesion            = new Sesion((IPEndPoint)client.Client.RemoteEndPoint);
            ConcurrentMessageQueue OUTQ = new ConcurrentMessageQueue();
            Random random = new Random();

            lock (sesion)
            {
                sesions.Add(sesion);
            }
            lock (OUTqueue)
            {
                OUTqueue[sesion] = OUTQ;
            }
            NetworkStream stream = client.GetStream();

            byte[] buffer = new byte[_BufferSize];
            byte[] sendbuf;
            string msg;
            int    bufPos = 0;

            sesion.LastSeen = DateTime.Now.Ticks;
            double cTTL = _TimeToLiveSeconds * (random.NextDouble() + 1);

            //sesion.Notified = false;

            Log.WriteLine("Client Opened:" + sesion.Address);

            while (client.Connected && running)
            {
                if (client.Available > 0)
                {
                    buffer[bufPos++] = (byte)stream.ReadByte();
                    if (buffer[bufPos - 1] == CoreUtil.MESSAGE_TERMINATOR)
                    {
                        sesion.LastSeen = DateTime.Now.Ticks;
                        cTTL            = _TimeToLiveSeconds * (random.NextDouble() + 1);
                        //sesion.Notified = false;
                        msg    = Encoding.ASCII.GetString(buffer, 0, bufPos - 1);
                        bufPos = 0;

                        Log.WriteLine(sesion.Address + ">" + msg);

                        if (msg.StartsWith(CoreUtil.MUVC_STRING))
                        {
                            if (msg == CoreUtil.DISCONNECT_STRING)
                            {
                                client.Close();
                                lock (OUTqueue)
                                {
                                    OUTqueue.Remove(sesion);
                                }
                                lock (sesions)
                                {
                                    sesions.Remove(sesion);
                                }

                                Log.WriteLine("Client DSC:" + sesion.Address);

                                return;
                            }
                            //else if (msg == CoreUtil.KEEPALIVE_STRING)
                            //{
                            //    sendbuf = Encoding.ASCII.GetBytes(CoreUtil.ACKNOWLEDGE_STRING + CoreUtil.MESSAGE_TERMINATOR);
                            //    safeWrite(stream, sendbuf, 0, sendbuf.Length);

                            //    Log.WriteLine("Responded to client keepalive:" + sesion.Address);
                            //}
                            else
                            {
                                //TODO handle other MUVC commands
                            }
                        }
                        else
                        {
                            recMessage handler = MessageRecieved;
                            if (handler == null || _EventPush)
                            {
                                INqueue.Enqueue(new Message(msg, sesion));
                            }
                            handler?.Invoke(msg, sesion);
                        }
                    }
                }
                if (!OUTQ.IsEmpty())
                {
                    msg     = OUTQ.Dequeue().Contents;
                    sendbuf = Encoding.ASCII.GetBytes(msg + CoreUtil.MESSAGE_TERMINATOR);
                    SafeWrite(stream, sendbuf, 0, sendbuf.Length);
                    sesion.LastSeen = DateTime.Now.Ticks;
                    cTTL            = _TimeToLiveSeconds * (random.NextDouble() + 1);
                    if (msg == CoreUtil.DISCONNECT_STRING)
                    {
                        client.Close();
                        lock (OUTqueue)
                        {
                            OUTqueue.Remove(sesion);
                        }
                        lock (sesions)
                        {
                            sesions.Remove(sesion);
                        }

                        Log.WriteLine("Client Terminated:" + sesion.Address);

                        return;
                    }

                    Log.WriteLine(sesion.Address + "<" + msg);
                }
                if (_TimeToLiveSeconds >= 0 && (DateTime.Now.Ticks - sesion.LastSeen) / CoreUtil.TICKS_PER_SECOND > cTTL)
                {
                    //if (sesion.Notified)
                    //{
                    //    client.Close();
                    //    lock (OUTqueue)
                    //    {
                    //        OUTqueue.Remove(sesion);
                    //    }
                    //    lock (sesions)
                    //    {
                    //        sesions.Remove(sesion);
                    //    }

                    //    Log.WriteLine("Client Timed Out:" + sesion.Address);

                    //    return;
                    //}
                    //else
                    //{
                    sendbuf = Encoding.ASCII.GetBytes(CoreUtil.KEEPALIVE_STRING + CoreUtil.MESSAGE_TERMINATOR);
                    SafeWrite(stream, sendbuf, 0, sendbuf.Length);
                    sesion.LastSeen = DateTime.Now.Ticks;
                    cTTL            = _TimeToLiveSeconds * (random.NextDouble() + 1);
                    //sesion.Notified = true;

                    Log.WriteLine("Client Notified:" + sesion.Address);
                    //}
                }
            }
            sendbuf = Encoding.ASCII.GetBytes(CoreUtil.DISCONNECT_STRING + CoreUtil.MESSAGE_TERMINATOR);
            SafeWrite(stream, sendbuf, 0, sendbuf.Length);
            client.Close();
            lock (OUTqueue)
            {
                OUTqueue.Remove(sesion);
            }
            lock (sesions)
            {
                sesions.Remove(sesion);
            }

            Log.WriteLine("Client Closed:" + sesion.Address);
        }
Ejemplo n.º 2
0
 public bool AvailableRead()
 {
     return(!INqueue.IsEmpty());
 }