public Boolean UpdateBackgroundImage(bool ignoreXorFirstReceive, CallbackHandler requestImageCallbackHandler)
        {
            Boolean ret = false;

            isRefreshAll = ignoreXorFirstReceive;

            if (ConnectionInfo.CurrentServer != null && ConnectionInfo.CurrentServer.IsConnected)
            {
                //변수 초기화
                this.currImgBytes   = null;
                this.totalSize      = 0;
                this.lastReceiveSeq = 0;

                imagePacket.Clear();
                imagePacket.PacketType       = PacketTypes.RequestImage;
                imagePacket.DeviceType       = DeviceType;
                imagePacket.ButtonType       = ButtonTypes.None;
                imagePacket.AccessCode       = ConnectionInfo.CurrentServer.AccessCode;
                imagePacket.Seq              = lastReceiveSeq;
                imagePacket.Flag             = ignoreXorFirstReceive ? 1 : 0;
                imagePacket.ImageQualityType = ImageQualityType;
                byte[] packet = imagePacket.CachedPacket;

                SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                args.UserToken = new UserToken(imagePacket, requestImageCallbackHandler);
                args.SetBuffer(packet, 0, packet.Length);
                args.Completed     += args_Completed;
                args.RemoteEndPoint = ConnectionInfo.CurrentServer.UdpIPEndPoint;

                imageUdpSocket = new UdpSocket();
                imageUdpSocket.ReceiveBufferSize = Constant.UDP_BUFFER_SIZE;
                imageUdpSocket.SendToAsync(args);

                ret = true;
            }
            return(ret);
        }
        public Boolean MoveTouch(TouchInfo[] touchInfos)
        {
            Boolean isSuccess = false;

            if (ConnectionInfo.CurrentServer.IsConnected)
            {
                byte[] packet = null;

                if (pointingUdpSocket == null)
                {
                    pointingUdpSocket = new UdpSocket();
                }

                SocketAsyncEventArgs udpSocketArg = new SocketAsyncEventArgs();
                movePacket.Clear();
                movePacket.DeviceType = DeviceType;
                movePacket.ButtonType = SettingInfo.UseExtendButton ? ButtonTypes.Navigation : ButtonTypes.None;
                movePacket.AccessCode = ConnectionInfo.CurrentServer.AccessCode;
                movePacket.PacketType = PacketTypes.Coordinates;
                movePacket.TouchInfos = touchInfos;

                packet = PacketUtils.MakeClientPacket(movePacket);
                //패킷 전송
                udpSocketArg.RemoteEndPoint = ConnectionInfo.CurrentServer.UdpIPEndPoint;
                udpSocketArg.SetBuffer(packet, 0, packet.Length);
                pointingUdpSocket.SendToAsync(udpSocketArg);

                //데이터 사용량 누적
                if (udpSocketArg.BytesTransferred > 0)
                {
                    AppLoader.CellularDataUtil.SumUsageCellularData(udpSocketArg.BytesTransferred);
                }
            }

            return(isSuccess);
        }
Example #3
0
        public Object ConnectServer(DeviceTypes deviceType, CallbackHandler connectResult)
        {
            if (!NetworkUtils.IsNetworkAvailable)
            {
                NetworkUtils.ShowWiFiSettingPage(I18n.GetString("AppMessageRequiredDataNetwork"), I18n.GetString("AppMessageNotification"));
                return(SocketError.NotConnected);
            }

            Object result = SocketError.TimedOut;

            ConnectionInfo.TcpSocket         = new TcpSocket();
            ConnectionInfo.TcpSocket.NoDelay = true;

            packetInfo.Clear();
            packetInfo.PacketType = PacketTypes.Authentication;
            packetInfo.DeviceType = deviceType;
            packetInfo.AccessCode = ConnectionInfo.CurrentServer.AccessCode;

            SocketAsyncEventArgs tcpSocketArgs = new SocketAsyncEventArgs();

            tcpSocketArgs.RemoteEndPoint = ConnectionInfo.CurrentServer.TcpIPEndPoint;
            tcpSocketArgs.UserToken      = new UserToken(packetInfo, connectResult);
            tcpSocketArgs.Completed     += new EventHandler <SocketAsyncEventArgs>((object s, SocketAsyncEventArgs e) =>
            {
                result = e.SocketError;
                // Signal that the request is complete, unblocking the UI thread
                _clientDone.Set();

                if (e.SocketError == SocketError.Success)
                {
                    //데이터 사용량 누적
                    if (e.BytesTransferred > 0)
                    {
                        AppLoader.CellularDataUtil.SumUsageCellularData(tcpSocketArgs.BytesTransferred);
                    }

                    UserToken userToken = null;
                    switch (e.LastOperation)
                    {
                    case SocketAsyncOperation.Connect:
                        if (e.ConnectSocket != null)
                        {
                            if (e.ConnectSocket.Connected)
                            {
                                byte[] packet = PacketUtils.MakeClientPacket(e.UserToken);
                                e.SetBuffer(packet, 0, packet.Length);
                                e.ConnectSocket.SendAsync(e);
                            }
                        }
                        else
                        {
                            userToken = e.UserToken as UserToken;
                            if (userToken.callback != null)
                            {
                                ConnectionInfo.CurrentServer.IsConnected = false;
                                ((CallbackHandler)userToken.callback)(null);
                            }
                        }
                        break;

                    case SocketAsyncOperation.Send:
                        userToken = e.UserToken as UserToken;
                        if (userToken.PacketInfo.PacketType == PacketTypes.Authentication)
                        {
                            byte[] packet = new byte[1024];
                            e.SetBuffer(packet, 0, packet.Length);
                            e.ConnectSocket.ReceiveAsync(e);
                        }
                        break;

                    case SocketAsyncOperation.Receive:
                        userToken = e.UserToken as UserToken;
                        ServerExtraInfo serverExtraInfo = (ServerExtraInfo)PacketUtils.ResolveServerPacket(PacketTypes.Authentication, e);

                        ConnectionInfo.CurrentServer.IsConnected = serverExtraInfo != null;
                        //결과 UI반영
                        if (userToken.callback != null)
                        {
                            ((CallbackHandler)userToken.callback)(serverExtraInfo);
                        }
                        //System.Diagnostics.Debug.WriteLine("접속 소켓 실행 결과 : " + result.ToString());
                        break;
                    }
                }
            });
            _clientDone.Reset();
            ConnectionInfo.TcpSocket.ConnectAsync(tcpSocketArgs);
            _clientDone.WaitOne(5000);

            //에러가 타임 아웃인 경우 강제적으로 콜백을 호출한다.
            if ((SocketError)result == SocketError.TimedOut)
            {
                connectResult(SocketError.TimedOut);
            }

            return(result);
        }