Exemple #1
0
        public void ProcessData(NL_Conn conn)
        {
            if (conn.BufferCount < sizeof(Int32))
            {
                return;
            }
            Array.Copy(conn.Buffer, conn.LenBytes, sizeof(int));
            conn.MsgLength = BitConverter.ToInt32(conn.LenBytes, 0);
            if (conn.BufferCount < (conn.MsgLength + sizeof(int)))
            {
                return;
            }
            //处理消息
            //string message = System.Text.Encoding.UTF8.GetString(conn.Buffer, sizeof(Int32), conn.MsgLength);
            //Console.WriteLine("Recieve Client[{0}]'s Message: {1}", conn.GetAddress, message);
            //SendMessage(message);

            ProtocolBase tempBase = _protocolBase.Decode(conn.Buffer, sizeof(int), conn.MsgLength);

            HandleMessage(conn, tempBase);
            //清楚已处理的消息
            conn.BufferCount -= (sizeof(Int32) + conn.MsgLength);
            Array.Copy(conn.Buffer, sizeof(Int32) + conn.MsgLength, conn.Buffer, 0, conn.BufferCount);

            if (conn.BufferCount > 0)
            {
                ProcessData(conn);
            }
        }
Exemple #2
0
        private void RecieveCb(IAsyncResult ar)
        {
            NL_Conn tempConn = ar.AsyncState as NL_Conn;

            try
            {
                int count = tempConn.Socket.EndReceive(ar);
                //if (count <= 0)
                //{
                //    Console.WriteLine($"Disconnected with {tempConn.GetAddress}.");
                //    tempConn.Close();
                //}

                tempConn.BufferCount += count;
                ProcessData(tempConn);
                //string message = System.Text.Encoding.UTF8.GetString(tempConn.Buffer, tempConn.BufferCount, count);
                //Console.WriteLine("Recieve Client[{0}]'s Message: {1}", tempConn.GetAddress, message);
                //tempConn.BufferCount = count;

                // message = MessageHandle(message, tempConn);

                //SendMessage(message);
                tempConn.Socket.BeginReceive(tempConn.Buffer, tempConn.BufferCount, tempConn.RemainCount, SocketFlags.None, RecieveCb, tempConn);
            }
            catch (Exception ex)
            {
                Console.WriteLine("RecieveCb is error.{0}", ex.Message);
            }
        }
Exemple #3
0
        public void HandleMessage(NL_Conn conn, ProtocolBase protocol)
        {
            string name = protocol.GetProtocolName;

            Console.WriteLine("Recieved ProtocolName is : {0}", name);
            if (name == "HeartBeat")
            {
                Console.WriteLine("Refresh {0} 's heartbeat.", conn.GetAddress);
                conn.LastTickTime = Sys.GetTimeStamp();
            }
        }
Exemple #4
0
 public NL_Server()
 {
     Socket    = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     _maxCount = 50;
     Conns     = new NL_Conn[_maxCount];
     for (int current = 0; current < _maxCount; current++)
     {
         Conns[current] = new NL_Conn();
     }
     _timer           = new Timer(1000);
     _timer.AutoReset = true;
     _timer.Enabled   = true;
 }
Exemple #5
0
 private string MessageHandle(string message, NL_Conn conn)
 {
     //if (string.IsNullOrEmpty(message)) return;
     if (message == "_GET")
     {
         string str;
         str = OutputMessageBoard();
         return(str);
     }
     else
     {
         SaveMessage(message, conn.GetAddress);
         return("");
     }
 }
Exemple #6
0
        public void SendData(NL_Conn conn, string message)
        {
            byte[] msgBytes  = Encoding.UTF8.GetBytes(message);
            byte[] lenBytes  = BitConverter.GetBytes(msgBytes.Length);
            byte[] sendBytes = lenBytes.Concat(msgBytes).ToArray();

            try
            {
                conn.Socket.BeginSend(sendBytes, 0, sendBytes.Length, SocketFlags.None, null, null);
            }
            catch (Exception ex)
            {
                Console.WriteLine("SendData is error.{0}", ex.Message);
            }
        }
Exemple #7
0
 private int GetIndex()
 {
     for (int index = 0; index < _maxCount; index++)
     {
         if (Conns[index] == null)
         {
             Conns[index] = new NL_Conn();
             return(index);
         }
         if (Conns[index].IsUse)
         {
             continue;
         }
         return(index);
     }
     Console.WriteLine("[Server] count is full.");
     return(-1);
 }
Exemple #8
0
        private void AcceptCb(IAsyncResult ar)
        {
            int index = GetIndex();

            if (index < 0)
            {
                return;
            }
            try
            {
                Socket  tempSocket = Socket.EndAccept(ar);
                NL_Conn tempConn   = Conns[index];
                tempConn.Init(tempSocket);
                Console.WriteLine("[Server] accept a client({0})", tempConn.GetAddress);
                tempConn.Socket.BeginReceive(tempConn.Buffer, tempConn.BufferCount, tempConn.RemainCount, SocketFlags.None, RecieveCb, tempConn);
                Socket.BeginAccept(AcceptCb, null);
            }
            catch (Exception ex)
            {
                Console.WriteLine("AcceptCb is error.{0}", ex.Message);
            }
        }