コード例 #1
0
        void SendControlMessage(MessageType msgType, int systembyte)
        {
            var token = new TaskCompletionSourceToken(ControlMessage, systembyte, msgType);

            if ((byte)msgType % 2 == 1 && msgType != MessageType.SeperateRequest)
            {
                _replyExpectedMsgs[systembyte] = token;
            }

            var eap = new SocketAsyncEventArgs
            {
                BufferList = new List <ArraySegment <byte> >(2)
                {
                    ControlMessageLengthBytes,
                    new ArraySegment <byte>(new MessageHeader {
                        DeviceId    = 0xFFFF,
                        MessageType = msgType,
                        SystemBytes = systembyte
                    }.Bytes)
                },
                UserToken = token,
            };

            eap.Completed += _sendControlMessageCompleteHandler;
            if (!_socket.SendAsync(eap))
            {
                SendControlMessageCompleteHandler(_socket, eap);
            }
        }
コード例 #2
0
        private Task <SecsMessage> SendDataMessageAsync(SecsMessage msg, int systembyte)
        {
            var token = new TaskCompletionSourceToken(msg, systembyte, MessageType.DataMessage);

            if (msg.ReplyExpected)
            {
                _replyExpectedMsgs[systembyte] = token;
            }

            var header = new MessageHeader
            {
                S             = msg.S,
                F             = msg.F,
                ReplyExpected = msg.ReplyExpected,
                DeviceId      = DeviceId,
                SystemBytes   = systembyte
            };

            var bufferList = msg.RawDatas.Value;

            bufferList[1] = new ArraySegment <byte>(header.EncodeTo(new byte[10]));
            var eap = new SocketAsyncEventArgs
            {
                BufferList = bufferList,
                UserToken  = token,
            };

            eap.Completed += _sendDataMessageCompleteHandler;
            sockClient.Send(eap.BufferList.ToArray(), SocketFlags.None);

            //if (!sockClient.SendAsync(eap))
            //    SendDataMessageCompleteHandler(sockClient, eap);

            return(token.Task);
        }
コード例 #3
0
        internal Task <SecsMessage> SendDataMessageAsync(SecsMessage msg, int systembyte)
        {
            if (State != ConnectionState.Selected)
            {
                throw new SecsException("Device is not selected");
            }

            var token = new TaskCompletionSourceToken(msg, systembyte);

            if (msg.ReplyExpected)
            {
                _replyExpectedMsgs[systembyte] = token;
            }

            var header = new MessageHeader
            {
                S             = msg.S,
                F             = msg.F,
                ReplyExpected = msg.ReplyExpected,
                DeviceId      = DeviceId,
                SystemBytes   = systembyte
            };

            var bufferList = msg.RawDatas.Value;

            bufferList[1] = new ArraySegment <byte>(header.Bytes);
            var eap = new SocketAsyncEventArgs
            {
                BufferList = bufferList,
                UserToken  = token,
            };

            eap.Completed += _sendDataMessageCompleteHandler;
            if (!_socket.SendAsync(eap))
            {
                SendDataMessageCompleteHandler(_socket, eap);
            }

            return(token.Task);
        }