示例#1
0
        public int broadcast(string response)
        {
            int count = 0;

            ConvsationSockets.RemoveWhere(socket => (socket == null || !socket.Connected));
            foreach (Socket item in ConvsationSockets)
            {
                if (item != null)
                {
                    try
                    {
                        item.Send(HeaderCode.BYTES_JSON);
                        SocketHelper.sendTextFrame(item, response);
                        count++;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        Console.WriteLine(e.StackTrace);
                    }
                }
            }
            saveConvsationCache(response);
            return(count);
        }
示例#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);
        }