Beispiel #1
0
 // 接收服务端发来信息的方法
 static void Recv()
 {
     while (!IsShutdownStarted)
     {
         while (Connected && !IsShutdownStarted)
         {
             try
             {
                 string requestString = PublicTools.DecodeDatabasePacket(stream);
                 _messages.TryAdd(requestString.Substring(0, 5), requestString.Substring(5));
             }
             catch (Exception ex) { Thread.Sleep(1000); ex.LogException(); }
         }
         while (!Connected && !IsShutdownStarted)
         {
             L.E("Message Recieve waiting for connection......");
             Thread.Sleep(500);
         }
     }
 }
Beispiel #2
0
        private static void Recv(object socketclientpara)
        {
            string        val;
            Socket        baseSocket        = socketclientpara as Socket;
            NetworkStream stream            = new NetworkStream(baseSocket);
            string        remoteEP          = baseSocket.RemoteEndPoint.ToString();
            bool          _connectionOpened = false;

            while (true)
            {
                try
                {
                    string requestString = PublicTools.DecodeDatabasePacket(stream);
                    if (requestString.Length <= 5)
                    {
                        baseSocket.CloseAndDispose();
                        stream.CloseAndDispose();
                        L.W("Recived Data: " + requestString);
                        break;
                    }

                    string _MessageId = requestString.Substring(0, 5);
                    requestString = requestString.Substring(5);

                    if (requestString == "openConnection")
                    {
                        L.W("OC: Recieve an OpenConnection Request, from " + remoteEP);
                        byte[] arrSendMsg = PublicTools.MakeDatabasePacket(_MessageId, remoteEP);
                        stream.Write(arrSendMsg, 0, arrSendMsg.Length);
                        stream.Flush();
                        _connectionOpened = true;
                        L.W("OC: Replied an OpenConnection Request, to " + remoteEP);
                    }
                    else if (_connectionOpened)
                    {
                        if (requestString == "HeartBeat")
                        {
                            L.D("HB: Recieve a HearBeat, from " + remoteEP);
                            DateTime rtime      = DateTime.Now;
                            byte[]   arrSendMsg = PublicTools.MakeDatabasePacket(_MessageId, rtime.ToNormalString());
                            stream.Write(arrSendMsg, 0, arrSendMsg.Length);
                            stream.Flush();
                            L.D("HB: Replied a HearBeat, to " + remoteEP);
                        }
                        else if (requestString.ToParsedObject(out DataBaseSocketIO request))
                        {
                            QueryStrings[remoteEP] = requestString;
                            L.I("Q: " + remoteEP + " :: " + requestString);
                            //It takes Time.....
                            string returnStr  = DatabaseCore.ProcessRequest(request);
                            byte[] arrSendMsg = PublicTools.MakeDatabasePacket(_MessageId, returnStr);
                            stream.Write(arrSendMsg, 0, arrSendMsg.Length);
                            stream.Flush();
                            L.I("Q: " + remoteEP + " :: " + returnStr);
                        }
                        else
                        {
                            //Invalid Connection......
                            baseSocket.CloseAndDispose();
                            QueryStrings.TryRemove(remoteEP, out val);
                            L.W("Recived Data: " + requestString);
                            L.E("E: " + remoteEP + " :: JSON Parse Exception!");
                            break;
                        }
                    }
                    else
                    {
                        baseSocket.CloseAndDispose();
                        stream.CloseAndDispose();
                        QueryStrings.TryRemove(remoteEP, out val);
                        L.W("Recived Data: " + requestString);
                        L.E("Connection to " + remoteEP + " is not marked as 'Opened'");
                        break;
                    }
                }
                catch (Exception ex)
                {
                    stream.CloseAndDispose();
                    baseSocket.CloseAndDispose();
                    L.E("Client " + remoteEP + " drops the connection. ");
                    ex.LogException();
                    QueryStrings.TryRemove(remoteEP, out val);
                    break;
                }
            }
            QueryStrings.TryRemove(remoteEP, out val);
            L.E("Client Connection Socket to " + remoteEP + " gonna Stop!");
            return;
        }