public async Task SendMessage(ProtoMessage message) { CheckIsDisposed(); try { var messageByte = message.ToByteArray(); var length = BitConverter.GetBytes(messageByte.Length).Reverse().ToArray(); LastSentMessageTime = DateTime.Now; await Write(messageByte, length); } catch (OperationCanceledException) { } catch (Exception ex) { if (!Streams.SenderExceptionStream.Observers.Any()) { throw; } Streams.OnSenderException(ex); } }
private void Transmit(ProtoMessage msg) { var msgByteArray = msg.ToByteArray(); byte[] length = BitConverter.GetBytes(msgByteArray.Length).Reverse().ToArray(); _apiSocket.Write(length); _apiSocket.Write(msgByteArray); }
private void Transmit(ProtoMessage msg, bool log = true) { if (log) { txtMessages.Text += "Send: " + OpenApiMessagesPresentation.ToString(msg); txtMessages.Text += Environment.NewLine; } var msgByteArray = msg.ToByteArray(); byte[] length = BitConverter.GetBytes(msgByteArray.Length).Reverse().ToArray(); _apiSocket.Write(length); _apiSocket.Write(msgByteArray); }
private void Transmit() { while (!isShutdown) { if (_trasmitQueue.Count() > 0) { //get the next message to submit ProtoMessage msg = _trasmitQueue.Dequeue(); //Sends the Proto message var msgByteArray = msg.ToByteArray(); byte[] length = BitConverter.GetBytes(msgByteArray.Length).Reverse().ToArray(); _apiSocket.Write(length); _apiSocket.Write(msgByteArray); switch ((ProtoOAPayloadType)msg.PayloadType) { case ProtoOAPayloadType.PROTO_OA_APPLICATION_AUTH_REQ: MessageHandler?.Invoke("Authorising App."); break; case ProtoOAPayloadType.PROTO_OA_ACCOUNT_AUTH_REQ: var accAuth = ProtoOAAccountAuthReq.CreateBuilder().MergeFrom(msg.Payload).Build(); MessageHandler?.Invoke("Authorising account " + Users[accAuth.AccessToken].AccountId); break; case ProtoOAPayloadType.PROTO_OA_SUBSCRIBE_SPOTS_REQ: var spotReq = ProtoOASubscribeSpotsReq.CreateBuilder().MergeFrom(msg.Payload).Build(); //get the associated user UserConfig config = Users.Where(x => x.Value.AccountId == spotReq.CtidTraderAccountId).Select(x => x.Value).FirstOrDefault(); //get the associated symbol Symbol symbol = config.Symbols.Where(x => x.Id == spotReq.GetSymbolId(0)).FirstOrDefault(); //Notify the spot request has been sent SymbolTickRequestHandler?.Invoke(symbol); break; } } else if (!_heartbeatTimer.Enabled) { //start the heartbeat timer _heartbeatTimer.Enabled = true; _heartbeatTimer.Start(); } //Wait 2.01 seconds between each message as to not exceed the 30 messages per minute restriction Thread.Sleep(2010); } }
public async Task SendMessage(ProtoMessage message) { try { var messageByte = message.ToByteArray(); var length = BitConverter.GetBytes(messageByte.Length); Array.Reverse(length); LastSentMessageTime = DateTime.Now; await Write(messageByte, length); } catch (Exception ex) { OnError(ex); } }
public void SendMessage(ProtoMessage message) { Debug.WriteLine("request type: {0}", message.MsgCase); Debug.WriteLine(message); byte[] bufferForMessage = message.ToByteArray(); int messageLength = bufferForMessage.Length; byte[] bufferForLength = BitConverter.GetBytes(messageLength); byte[] temp = new byte[4]; for (int i = 0; i < 4; i++) { temp[3 - i] = bufferForLength[i]; } bufferForLength = temp; byte[] package = new byte[bufferForLength.Length + bufferForMessage.Length]; Debug.WriteLine(messageLength); Debug.WriteLine(bufferForLength.Length); System.Buffer.BlockCopy(bufferForLength, 0, package, 0, bufferForLength.Length); System.Buffer.BlockCopy(bufferForMessage, 0, package, bufferForLength.Length, bufferForMessage.Length); client.BeginSend(package, 0, package.Length, 0, new AsyncCallback(sendCallBack), client); }