public void ConnectCompleted(object sender, SocketAsyncEventArgs e)
        {
            Socket client = sender as Socket;

            if (e.SocketError == SocketError.Success)
            {
                Log($"서버와 연결됨: [DEST: {ip}:{port}]");
                OnConnected(this, new EventArgs());
                Log($"수신 환경 구축 중...");
                SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                Telegram telegram = new Telegram();
                telegram.initDataSize();
                args.UserToken = telegram;
                args.SetBuffer(telegram.Data, 0, telegram.Data.Length);
                args.Completed += new EventHandler<SocketAsyncEventArgs>(ReceiveCompleted);
                client.ReceiveAsync(args);
                Log($"수신 대기...");
            }
        }
        private void ReceiveCompleted(object sender, SocketAsyncEventArgs e)
        {
            Socket client = sender as Socket;

            Telegram telegram = (Telegram)e.UserToken;
            telegram.initDataSize();

            if (e.SocketError == SocketError.Success)
            {
                if (client.Connected && e.BytesTransferred > 0)
                {
                    telegram.Data = e.Buffer;
                    byte[] data = telegram.Data;
                    switch (telegram.Code)
                    {
                        case NET_INFO_TARGETS:
                            {
                                Log("타겟 정보 수신 중...");
                                int id = BitConverter.ToInt32(data, 8);
                                double diffx = BitConverter.ToDouble(data, 12);
                                double diffy = BitConverter.ToDouble(data, 20);
                                double ang = BitConverter.ToDouble(data, 28);
                                double scale = BitConverter.ToDouble(data, 36);

                                Log($"타겟 {id}:, 수평변위: {diffx}, 수직변위: {diffy}, 각도변화: {ang}, 스케일: {scale}");

                                Log("타겟 정보 수신 종료");
                                break;
                            }

                        case NET_INFO_CAMS:
                            {
                                Log("카메라 정보 수신 중...");
                                int id = BitConverter.ToInt32(data, 8);
                                double maxx = BitConverter.ToDouble(data, 12);
                                double maxy = BitConverter.ToDouble(data, 20);
                                double maxang = BitConverter.ToDouble(data, 28);
                                double maxscale = BitConverter.ToDouble(data, 36);
                                double evenx = BitConverter.ToDouble(data, 44);
                                double eveny = BitConverter.ToDouble(data, 52);
                                double evenang = BitConverter.ToDouble(data, 60);
                                double evenscale = BitConverter.ToDouble(data, 68);

                                Log($"카메라 {id}, 최대수평변위: {maxx}, 최대수직변위: {maxy}, 최대각도변화: {maxang}, 최대스케일: {maxscale}, 평균수평변위: {evenx}, 평균수직변위: {eveny}, 평균각도변화: {evenang}, 평균스케일: {evenscale}");

                                Log("카메라 정보 수신 종료");
                                
                                break;
                            }
                    }
                }

                if (!client.Connected)
                {
                    Close();
                }

                Telegram _telegram = new Telegram();
                _telegram.initDataSize();
                e.UserToken = _telegram;
                e.SetBuffer(_telegram.Data, 0, _telegram.Data.Length);
                client.ReceiveAsync(e);
            }
            else
            {
                Log($"서버와 연결 끊김 {e.SocketError.ToString()}");
                Close();
            }
        }
Beispiel #3
0
        private void ReceiveCompleted(object sender, SocketAsyncEventArgs e)
        {
            Socket client = (Socket)sender;
            Telegram telegram = (Telegram)e.UserToken;
            telegram.initDataSize(e.Buffer.Length);

            if (client.Connected && e.BytesTransferred > 0)
            {
                telegram.Data = e.Buffer;
            }

            if (!client.Connected)
            {
                client.Disconnect(false);
                clients.Remove(client);
            }

            Telegram _telegram = new Telegram();
            _telegram.initDataSize();
            e.UserToken = _telegram;
            e.SetBuffer(_telegram.Data, 0, _telegram.Data.Length);
            client.ReceiveAsync(e);
        }
Beispiel #4
0
        private void AcceptCompleted(object sender, SocketAsyncEventArgs e)
        {
            Socket client = e.AcceptSocket;
            Telegram telegram = new Telegram();
            telegram.initDataSize();

            SocketAsyncEventArgs args = new SocketAsyncEventArgs();
            args.UserToken = telegram;
            args.SetBuffer(telegram.Data, 0, telegram.Data.Length);
            args.Completed += new EventHandler<SocketAsyncEventArgs>(ReceiveCompleted);

            client.ReceiveAsync(args);
            clients.Add(client);

            Socket curServer = (Socket)sender;
            e.AcceptSocket = null;
            curServer.AcceptAsync(e);
        }