Esempio n. 1
0
        private void socketReceiveWork(object accept)
        {
            Socket  s       = accept as Socket;
            string  json    = "";
            Request request = new Request();

            request.DeviceId = "";
            byte[] buffer = bufferPool.borrow();
            mLogHelper.InfoFormat("addr:{0}, buffer:{1}", ((IPEndPoint)s.RemoteEndPoint).Address.ToString(), buffer.GetHashCode());
            try
            {
                while (true)
                {
                    //通过clientSocket接收数据
                    s.ReceiveTimeout = -1;
                    int receiveNumber = s.Receive(buffer, 4, SocketFlags.None);
                    if (receiveNumber <= 0)
                    {
                        mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, Receive: null, ByteCount:{2}", ((IPEndPoint)s.RemoteEndPoint).Address.ToString(), request.DeviceId, receiveNumber);
                        break;
                    }

                    if (buffer[0] == HeaderCode.BYTES_ASK[0] && buffer[1] == HeaderCode.BYTES_ASK[1] && buffer[2] == HeaderCode.BYTES_ASK[2] && buffer[3] == HeaderCode.BYTES_ASK[3])
                    {
                        mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, Receive: HeaderCode.ASK, ByteCount:{2}", ((IPEndPoint)s.RemoteEndPoint).Address.ToString(), request.DeviceId, receiveNumber);
                        s.Send(HeaderCode.BYTES_ANS);
                        mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, response: HeaderCode.ANS", ((IPEndPoint)s.RemoteEndPoint).Address.ToString(), request.DeviceId);
                        continue;
                    }
                    else if (buffer[0] == HeaderCode.BYTES_RAW[0] && buffer[1] == HeaderCode.BYTES_RAW[1] && buffer[2] == HeaderCode.BYTES_RAW[2] && buffer[3] == HeaderCode.BYTES_RAW[3])
                    {
                        try
                        {
                            string rawName  = SocketHelper.receiveTextFrame(s, buffer, mLogHelper);
                            int    size     = SocketHelper.receiveRawFrame(s, buffer, mRawFolder, rawName, mLogHelper);
                            string response = SocketHelper.responseJson(s, ((int)ResponseCode.Success).ToString(), "", rawName);
                            mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, Receive: HeaderCode.RAW, ByteCount:{2}, name:{3}", ((IPEndPoint)s.RemoteEndPoint).Address.ToString(), request.DeviceId, size, rawName);
                            mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, response{2}", ((IPEndPoint)s.RemoteEndPoint).Address.ToString(), request.DeviceId, response);
                        }
                        catch (Exception e)
                        {
                            string response = SocketHelper.responseJson(s, ((int)ResponseCode.ErrorSocketRecive).ToString(), "", format(e));
                        }
                    }
                    else if (buffer[0] == HeaderCode.BYTES_CKRAW[0] && buffer[1] == HeaderCode.BYTES_CKRAW[1] && buffer[2] == HeaderCode.BYTES_CKRAW[2] && buffer[3] == HeaderCode.BYTES_CKRAW[3])
                    {
                        try
                        {
                            string rawName = SocketHelper.receiveTextFrame(s, buffer, mLogHelper);
                            string md5     = SocketHelper.receiveTextFrame(s, buffer, mLogHelper);
                            mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, Receive: HeaderCode.CKRAW, MD5 ByteCount:{2}, name:{3}", ((IPEndPoint)s.RemoteEndPoint).Address.ToString(), request.DeviceId, md5, rawName);

                            if (SocketHelper.rawMd5ExistCheck(s, md5, mRawFolder, rawName))
                            {
                                s.Send(HeaderCode.BYTES_CK_SUC_RAW);
                                mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, Send: HeaderCode.BYTES_CK_SUC_RAW, name:{2}", ((IPEndPoint)s.RemoteEndPoint).Address.ToString(), request.DeviceId, rawName);
                            }
                            else
                            {
                                s.Send(HeaderCode.BYTES_CK_FAIL_RAW);
                                mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, Send: HeaderCode.BYTES_CK_FAIL_RAW,name:{2}", ((IPEndPoint)s.RemoteEndPoint).Address.ToString(), request.DeviceId, rawName);
                            }
                        }
                        catch (Exception e)
                        {
                            mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, HeaderCode.CKRAW Exception:{2} {3}", ((IPEndPoint)s.RemoteEndPoint).Address.ToString(), request.DeviceId, e.Message, e.StackTrace);
                            //string response = SocketHelper.responseJson(s, ((int)ResponseCode.ErrorSocketRecive).ToString(), "", format(e));
                        }
                    }
                    else if (buffer[0] == HeaderCode.BYTES_JSON[0] && buffer[1] == HeaderCode.BYTES_JSON[1] && buffer[2] == HeaderCode.BYTES_JSON[2] && buffer[3] == HeaderCode.BYTES_JSON[3])
                    {
                        string end;
                        int    code = (int)ResponseCode.Success;
                        try
                        {
                            json = SocketHelper.receiveTextFrame(s, buffer, mLogHelper);
                        }
                        catch (Exception e)
                        {
                            code = (int)ResponseCode.ErrorSocketRecive;
                            end  = SocketHelper.responseJson(s, ((int)ResponseCode.ErrorSocketRecive).ToString(), request.RequestId, format(e));
                        }
                        mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, Receive: HeaderCode.JSON, json:{2}", ((IPEndPoint)s.RemoteEndPoint).Address.ToString(), request.DeviceId, json);
                        request.Code = -1;
                        try
                        {
                            JSONNode node = JSON.Parse(json);
                            request.Code      = int.Parse(node[RequestKey.Code]);
                            request.Args      = (node[RequestKey.Args] as JSONArray).Childs.Select((m) => ((string)m)).ToArray();
                            request.RequestId = node[RequestKey.RequestId];
                            if (request.RequestId == null)
                            {
                                request.RequestId = "";
                            }
                            string deviceId = node[RequestKey.DeviceId];
                            if (!string.IsNullOrEmpty(deviceId))
                            {
                                request.DeviceId = deviceId;
                            }
                        }
                        catch
                        {
                            request.Args = new string[0];
                        }
                        if (request.Code == (int)RequestCode.DisConnect)
                        {
                            break;
                        }
                        try
                        {
                            end = handleJson(s, buffer, json, request, request.Args.Length, ref code);
                        }
                        catch (Exception ex)
                        {
                            end = ex.StackTrace;
                            Console.WriteLine(ex.Message);
                            Console.WriteLine(ex.StackTrace);
                        }
                        if (end != null)
                        {
                            string response = SocketHelper.responseJson(s, code.ToString(), request.RequestId, end);
                            mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, response:{2}", ((IPEndPoint)s.RemoteEndPoint).Address.ToString(), request.DeviceId, response);
                        }
                        else
                        {
                            mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, no response", ((IPEndPoint)s.RemoteEndPoint).Address.ToString(), request.DeviceId);
                        }
                    }
                    else
                    {
                        mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, unkown code {2}", ((IPEndPoint)s.RemoteEndPoint).Address.ToString(), request.DeviceId, buffer[0]);
                        s.ReceiveTimeout = 1000;
                        try
                        {
                            int len;
                            while ((len = s.Receive(buffer)) >= 0)
                            {
                                mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, Receive Trash len{2}", ((IPEndPoint)s.RemoteEndPoint).Address.ToString(), request.DeviceId, len);
                            }
                        }catch (Exception e)
                        {
                            mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, Receive Trash over e:{2} {3}", ((IPEndPoint)s.RemoteEndPoint).Address.ToString(), request.DeviceId, e.Message, e.StackTrace);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, while Receivere Exception:{2}", ((IPEndPoint)s.RemoteEndPoint).Address.ToString(), request.DeviceId, ex.Message);
                mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, while Receivere Exception:{2}", ((IPEndPoint)s.RemoteEndPoint).Address.ToString(), request.DeviceId, ex.StackTrace);
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            finally
            {
                mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, socket close", ((IPEndPoint)s.RemoteEndPoint).Address.ToString(), request.DeviceId);
                s.Shutdown(SocketShutdown.Both);
                s.Disconnect(true);
                s.Close();
                bufferPool.giveBack(buffer);
            }
        }
Esempio n. 2
0
        private string handleJson(Socket socket, byte[] buffer, string requestStr, Request request, int argCount, ref int code)
        {
            string back = "success";

            switch (request.Code)
            {
            case (int)RequestCode.RunCmd:
                if (argCount > 2)
                {
                    back = runCmd(request.Args[0], request.Args[1]);
                }
                break;

            case (int)RequestCode.FindWindow:
                back = findWindowHwnd(request.Args[0]);
                break;

            case (int)RequestCode.SendWindowInfo:
                back = PasteToWindow((IntPtr)int.Parse(request.Args[0]), request.Args[1]);
                break;

            case (int)RequestCode.RemoteFindWindow:
                var socketClient = getConnectedSocketClient(request.Args[0], request.Args[1]);
                back = socketClient.remoteFindWindow(request.Args[2]).ToString();
                break;

            case (int)RequestCode.MobileTerminalJson:
                if (MobileTerminalManager.getInstance().addSocket(socket))
                {
                    foreach (var item in MobileTerminalManager.getInstance().getConvastationCash())
                    {
                        SocketHelper.responseJson(socket, item);
                        mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, send new:{2}", ((IPEndPoint)socket.RemoteEndPoint).Address.ToString(), request.DeviceId, item);
                    }
                }
                if (request.Args != null && request.Args.Length >= 1)
                {
                    int count = MobileTerminalManager.getInstance().broadcast(SocketHelper.makeResponseJson(((int)ResponseCode.Success).ToString(), request.RequestId, request.Args));
                    mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, Conversation broadcast:{2}", ((IPEndPoint)socket.RemoteEndPoint).Address.ToString(), request.DeviceId, count);
                }

                // just for log out
                //string response1 = SocketHelper.responseJson(socket, code.ToString(), request.Args[0], request.RequestId, request.Args[1]);
                //mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, response:{2}", ((IPEndPoint)socket.RemoteEndPoint).Address.ToString(), request.DeviceId, response1);
                back = null;
                break;

            case (int)RequestCode.MobileTerminalRaw:
                socket.Send(HeaderCode.BYTES_RAW);
                if (request.Args.Length >= 2)
                {
                    if (SocketHelper.rawMd5Check(null, request.Args[1], mRawFolder, request.Args[0]))
                    {
                        mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, name:{2}, md5:{3} check suc", ((IPEndPoint)socket.RemoteEndPoint).Address.ToString(), request.DeviceId, request.Args[0], request.Args[1]);
                        socket.Send(HeaderCode.BYTES_CK_SUC_RAW);
                        break;
                    }
                    else
                    {
                        mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, name:{2}, md5:{3} check fail", ((IPEndPoint)socket.RemoteEndPoint).Address.ToString(), request.DeviceId, request.Args[0], request.Args[1]);
                        socket.Send(HeaderCode.BYTES_CK_FAIL_RAW);
                    }
                }

                if (File.Exists(mRawFolder + "\\" + request.Args[0]))
                {
                    try
                    {
                        int size = SocketHelper.responseRaw(socket, buffer, mRawFolder, request.Args[0]);
                        mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, download image over size:{2}", ((IPEndPoint)socket.RemoteEndPoint).Address.ToString(), request.DeviceId, size);
                        back = null;
                    }
                    catch (Exception e)
                    {
                        back = format(e);
                        code = (int)ResponseCode.ErrorRawSend;
                        mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, download image exception:{2}", ((IPEndPoint)socket.RemoteEndPoint).Address.ToString(), request.DeviceId, back);
                    }
                    mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, download image suc", ((IPEndPoint)socket.RemoteEndPoint).Address.ToString(), request.DeviceId);
                }
                else
                {
                    code = (int)ResponseCode.ErrorRawNotExist;
                    back = "server don not find file:" + request.Args[0];
                    mLogHelper.InfoFormat("addr:{0}, deviceId:{1}, download image request server don not find file", ((IPEndPoint)socket.RemoteEndPoint).Address.ToString(), request.DeviceId);
                }
                if (back != null)
                {
                    SocketHelper.sendTextFrame(socket, request.Args[0]);
                    SocketHelper.sendTextFrame(socket, back);
                    back = null;
                }
                break;

            default:
                back = null;
                break;
            }
            return(back);
        }