public Task <SecsMessage> SendDataMessage(SecsMessage msg) { int systembyte = NewSystemId; 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; SocketClient.Send(eap.BufferList.ToArray(), SocketFlags.None); //if (!sockClient.SendAsync(eap)) // SendDataMessageCompleteHandler(sockClient, eap); return(token.Task); }
/// <summary> /// Each PrimaryMessageWrapper can invoke Reply method once. /// If the message already replied, will return false. /// </summary> /// <param name="replyMessage"></param> /// <returns>true, if reply message sent.</returns> public Task <bool> ReplyAsync(SecsMessage replyMessage) { if (Interlocked.Exchange(ref _isReplied, 1) == 1) { return(ReplyAsyncFalseCache); } if (!Message.ReplyExpected || !_secsGem.TryGetTarget(out var secsGem)) { return(ReplyAsyncTrueCache); } replyMessage = replyMessage ?? new SecsMessage(9, 7, false, "Unknown Message", Item.B(_header.EncodeTo(new byte[10]))); replyMessage.ReplyExpected = false; return(secsGem.SendDataMessageAsync(replyMessage, replyMessage.S == 9 ? secsGem.NewSystemId : _header.SystemBytes) .ContinueWith(ContinueWithFunc)); }