Beispiel #1
0
        public static void AcceptCallback(IAsyncResult ar)
        {
            try
            {
                // Signal the main thread to continue.
                allDone.Set();

                if (Form.runServer != true)
                {
                    return;
                }

                // Get the socket that handles the client request.
                Socket listener = (Socket)ar.AsyncState;
                Socket handler  = listener.EndAccept(ar);

                // Create the state object.
                StateObject state = new StateObject();
                state.workSocket = handler;
                EtherHandler eh = (EtherHandler)Form.commHandler;
                eh.CSocketHandler = handler;
                handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                     new AsyncCallback(ReadCallback), state);
                if (handler.Connected)
                {
                    Form.Dispatcher.Invoke(new UpdateTextDelegate(Form.DisplayText), recvTab.CommRxList, "클라이언트 연결!!");
                }
            }
            catch (SocketException ex)
            {
                throw ex;
                //Form.Dispatcher.Invoke(new UpdateTextDelegate(Form.DisplayText), Form.SocketRxList, ex.Message);
            }
        }
Beispiel #2
0
        public static void ReadCallback(IAsyncResult ar)
        {
            String content = String.Empty;

            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            StateObject  state   = (StateObject)ar.AsyncState;
            Socket       handler = state.workSocket;
            EtherHandler eh      = (EtherHandler)Form.commHandler;

            eh.CSocketHandler = handler;

            try
            {
                if (handler != null)
                {
                    if (handler.Connected)
                    {
                        // Read data from the client socket.
                        int bytesRead = handler.EndReceive(ar);

                        if (bytesRead > 0)
                        {
                            // There  might be more data, so store the data received so far.
                            //state.sb.Append(Encoding.ASCII.GetString(
                            //  state.buffer, 0, bytesRead));

                            // Check for end-of-file tag. If it is not there, read
                            // more data.

                            //string str = string.Empty;
                            int           cnt = 0;
                            StringBuilder sb  = new StringBuilder();

                            foreach (byte b in state.buffer)
                            {
                                sb.Append(string.Format("[" + "{0:x2}" + "]", b));
                                cnt++;
                                if (cnt == bytesRead)
                                {
                                    break;
                                }
                            }

                            Form.Dispatcher.Invoke(() =>
                            {
                                //code 로 메시지 확인
                                switch (state.buffer[Frame.Code])
                                {
                                case Code.ACK:
                                    recvTab.SeqNum = (int)state.buffer[Frame.Seq];      ///전송연번 업데이트
                                    break;

                                case Code.NACK:
                                    recvTab.SeqNum = (int)state.buffer[Frame.Seq];      ///전송연번 업데이트
                                    //추후 재전송 로직 추가
                                    break;

                                case Code.STATUS_RES:                                  ///상태정보 수신
                                    {
                                        recvTab.SeqNum = (int)state.buffer[Frame.Seq]; ///전송연번 업데이트
                                        //ACK를 보내줌.
                                        ProcItem item = null;
                                        sndTab.MakeFrame(Code.ACK, out byte[] data, Form.comm, ref item);
                                        Send(data);
                                    }
                                    break;

                                case Code.VIO_CONFIRM_REQ:                             ///위반확인요구 수신
                                    {
                                        recvTab.SeqNum = (int)state.buffer[Frame.Seq]; ///전송연번 업데이트
                                        //ACK를 보내줌.
                                        //sndTab.MakeEtherFrame(Code.ACK, out byte[] data);
                                        //Send(handler, data);

                                        int nCopy        = Marshal.SizeOf(typeof(PACKET_VIO_REQUEST));
                                        byte[] _cpyArray = new byte[nCopy];

                                        Array.Copy(state.buffer, Frame.Data, _cpyArray, 0, nCopy);

                                        //위반확인응답을 보내줌.
                                        PACKET_VIO_REQUEST pVioReq = (PACKET_VIO_REQUEST)PacketMethods.ByteToStructure(_cpyArray, typeof(PACKET_VIO_REQUEST));
                                        if (pVioReq.imgStatus == 0x00)
                                        {
                                            recvTab.triggerStatus.Text = "정상";
                                        }
                                        else
                                        {
                                            recvTab.triggerStatus.Text = "비정상";
                                        }

                                        ProcItem pItem = new ProcItem((uint)sndTab.pcComboBox.SelectedIndex + 1);
                                        pItem.seq      = state.buffer[Frame.Seq];
                                        pItem.vioNum   = pVioReq.imagNum;
                                        sndTab.procList.Add(pItem);
                                        /// 영상번호 업데이트
                                        recvTab.imageNum.Text = pVioReq.imagNum.ToString();
                                        if (sndTab.syncMethod.SelectedIndex == 1)
                                        {
                                            int setByteOrder = sndTab.ByteOrder.SelectedIndex;
                                            sndTab.VioNumber = pVioReq.imagNum;

                                            byte[] cvtVioNum = BitConverter.GetBytes((short)sndTab.VioNumber);

                                            if (setByteOrder == 1)         // Big endian
                                            {
                                                cvtVioNum        = cvtVioNum.Reverse().ToArray();
                                                sndTab.VioNumber = BitConverter.ToInt16(cvtVioNum, 0);
                                            }
                                        }

                                        //위반확인자동응답 체크 시 전송을 수행함.
                                        if (othTab.autoVioSendCheck.IsChecked == true)
                                        {
                                            int maxLoop      = sndTab.pcComboBox.SelectedIndex + 1;
                                            uint saveProcNum = sndTab.ProcNumber1;
                                            for (sndTab.cycleNum = 1; sndTab.cycleNum <= maxLoop; sndTab.cycleNum++)
                                            {
                                                if (othTab.cnfResTypeComboBox.SelectedIndex == 0)
                                                {
                                                    if (sndTab.MakeFrame(Code.VIO_CONFIRM_RES, out byte[] data, ((MainWindow)System.Windows.Application.Current.MainWindow).comm, ref pItem) == true)
                                                    {
                                                        ((MainWindow)System.Windows.Application.Current.MainWindow).SendData(data, data.Length);
                                                    }
                                                }
                                                else
                                                {
                                                    //신규응답으로 보낸다.
                                                    if (sndTab.MakeFrame(Code.VIO_CONFIRM_RES_N, out byte[] data, ((MainWindow)System.Windows.Application.Current.MainWindow).comm, ref pItem) == true)