Example #1
0
 protected override void OnMessage(NetworkData data, IConnection responseChannel)
 {
     if (data.Length > 0)
     {
         ChannelLocalActor.Notify(responseChannel, new InboundPayload(FromData(data)));
     }
 }
 protected override void ReceivedData(NetworkData availableData, ReactorResponseChannel responseChannel)
 {
     if (EventLoop.Receive != null)
     {
         EventLoop.Receive(availableData, responseChannel);
     }
 }
Example #3
0
            private void TempServer_OnReceive(Helios.Net.NetworkData incomingData, IConnection connection)
            {
                ThreadHelper.TryLock(this.lockobj,
                                     () => {
                    Request msg       = default(Request);
                    Response response = default(Response);
                    msg = SP.Deserialize <Request>(incomingData.Buffer);

                    Func <Request, Response> clientRequestAction = null;
                    bool hasRequestAction = this._lisenter.ReceiveEvents.TryGetValue(msg.MessageType, out clientRequestAction);
                    if (hasRequestAction)
                    {
                        response = clientRequestAction.Invoke(msg);
                    }
                    var sendBuffer = SP.Serialize(response);        // serialize response message and send it to client
                    connection.Send(new Helios.Net.NetworkData
                    {
                        Buffer     = sendBuffer,
                        Length     = sendBuffer.Length,
                        RemoteHost = connection.RemoteHost
                    });
                }, (error) => {
                    throw error;
                });
            }
Example #4
0
 private static void ReceivedCallback(NetworkData data, IConnection responseChannel)
 {
     var timeStr = Encoding.UTF8.GetString(data.Buffer);
     if (ThreadLocalRandom.Current.Next(0, 1000) == 1)
     {
         Console.WriteLine("Received: {0}", timeStr);
     }
 }
Example #5
0
 private void ReceivedDataCallback(NetworkData incomingData, IConnection responseChannel)
 {
     Invoke((Action)(() =>
     {
         AppendStatusText(string.Format("Received {0} bytes from {1}", incomingData.Length,
             incomingData.RemoteHost));
         AppendStatusText(Encoding.UTF8.GetString(incomingData.Buffer));
     }));
 }
Example #6
0
 /// <summary>
 /// Converts a <see cref="ByteString"/> structure into a Helios <see cref="NetworkData"/> structure
 /// </summary>
 /// <param name="byteString">The data to send over the network</param>
 /// <param name="address">The address that we received data from / are sending data to</param>
 /// <returns>a new <see cref="NetworkData"/> struct</returns>
 public static NetworkData ToData(ByteString byteString, Address address)
 {
     var data = new NetworkData()
     {
         Buffer = byteString.ToByteArray(),
         RemoteHost = HeliosTransport.AddressToNode(address)
     };
     data.Length = data.Buffer.Length;
     return data;
 }
Example #7
0
        public static void ReceiveData(NetworkData data, IConnection connection)
        {
            var node = connection.RemoteHost;

            ServerPrint(connection.RemoteHost, string.Format("recieved {0} bytes", data.Length));
            var str = Encoding.UTF8.GetString(data.Buffer).Trim();
            if (str.Trim().Equals("close"))
            {
                connection.Close();
                return;
            }
            ServerPrint(connection.RemoteHost, string.Format("recieved \"{0}\"", str));
            ServerPrint(connection.RemoteHost,
                string.Format("sending \"{0}\" back to {1}:{2}", str, node.Host, node.Port));
            var sendBytes = Encoding.UTF8.GetBytes(str + Environment.NewLine);
            connection.Send(new NetworkData() {Buffer = sendBytes, Length = sendBytes.Length, RemoteHost = node});
        }
Example #8
0
        public void Encode(NetworkData data, out List<NetworkData> encoded)
        {
            var length = data.Length + _lengthAdjustment;
            if (_lengthIncludesLenghtFieldLength)
            {
                length += _lengthFieldLength;
            }

            encoded = new List<NetworkData>();
            if (length < 0)
                throw new ArgumentException(string.Format("Adjusted frame length ({0}) is less than zero", length));

            var newData = new byte[0];
            using (var memoryStream = new MemoryStream())
            {
                switch (_lengthFieldLength)
                {
                    case 1:
                        if (length >= 256)
                            throw new ArgumentException("length of object does not fit into one byte: " + length);
                        memoryStream.Write(BitConverter.GetBytes((byte) length), 0, 1);
                        break;
                    case 2:
                        if (length >= 65536)
                            throw new ArgumentException("length of object does not fit into a short integer: " + length);
                        memoryStream.Write(BitConverter.GetBytes((ushort) length), 0, 2);
                        break;
                    case 4:
                        memoryStream.Write(BitConverter.GetBytes((uint) length), 0, 4);
                        break;
                    case 8:
                        memoryStream.Write(BitConverter.GetBytes((long) length), 0, 8);
                        break;
                    default:
                        throw new Exception("Unknown lenght field length");
                }

                memoryStream.Write(data.Buffer, 0, data.Length);
                newData = memoryStream.GetBuffer();
            }

            var networkData = NetworkData.Create(data.RemoteHost, newData,
                _lengthIncludesLenghtFieldLength ? length : length + _lengthFieldLength);
            encoded.Add(networkData);
            HeliosTrace.Instance.EncodeSuccess();
        }
Example #9
0
        public static void Receive(NetworkData data, IConnection channel)
        {
            var command = Encoding.UTF8.GetString(data.Buffer);

            //Console.WriteLine("Received: {0}", command);
            if (command.ToLowerInvariant() == "gettime")
            {
                var time = Encoding.UTF8.GetBytes(DateTime.Now.ToLongTimeString());
                channel.Send(new NetworkData() { Buffer = time, Length = time.Length, RemoteHost = channel.RemoteHost });
                //Console.WriteLine("Sent time to {0}", channel.Node);
            }
            else
            {
                Console.WriteLine("Invalid command: {0}", command);
                var invalid = Encoding.UTF8.GetBytes("Unrecognized command");
                channel.Send(new NetworkData() { Buffer = invalid, Length = invalid.Length, RemoteHost = channel.RemoteHost });
            }
        }
Example #10
0
 public void Send(NetworkData payload)
 {
     UnderlyingConnection.Send(payload);
 }
 public virtual void Send(NetworkData data)
 {
     _reactor.Send(data);
 }
Example #12
0
 public static Stream ToStream(this NetworkData nd)
 {
     return(new MemoryStream(nd.Buffer, 0, nd.Length));
 }
Example #13
0
 public Task SendAsync(NetworkData payload)
 {
     return TaskRunner.Run(() => Send(payload));
 }
Example #14
0
        protected void Init(IConnection channel, INode remoteSocketAddress, Address remoteAddress, NetworkData msg,
            out AssociationHandle op)
        {
            var localAddress = HeliosTransport.NodeToAddress(channel.Local, WrappedTransport.SchemeIdentifier,
                WrappedTransport.System.Name, WrappedTransport.Settings.Hostname);

            if (localAddress != null)
            {
                var handle = CreateHandle(channel, localAddress, remoteAddress);
                handle.ReadHandlerSource.Task.ContinueWith(s =>
                {
                    var listener = s.Result;
                    RegisterListener(channel, listener, msg, remoteSocketAddress);
                }, TaskContinuationOptions.AttachedToParent & TaskContinuationOptions.ExecuteSynchronously & TaskContinuationOptions.NotOnCanceled & TaskContinuationOptions.NotOnFaulted);
                op = handle;
            }
            else
            {
                op = null;
                channel.Close();
            }
        }
Example #15
0
 protected void InvokeReceiveIfNotNull(NetworkData data)
 {
     if (Receive != null)
     {
         Receive(data, this);
     }
 }
Example #16
0
 protected override void OnMessage(NetworkData data, IConnection responseChannel)
 {
     var protoMessage = _protobufDecoder.Decode(data.Buffer);
     var testkitMesage = _msgDecoder.Decode(protoMessage);
     _handler.OnMessage(testkitMesage, responseChannel);
 }
 public override void Send(NetworkData data)
 {
     HeliosTrace.Instance.TcpInboundSendQueued();
     SendInternal(data.Buffer, 0, data.Length, data.RemoteHost);
 }
Example #18
0
 protected abstract void RegisterListener(IConnection channel, IHandleEventListener listener, NetworkData msg,
     INode remoteAddress);
Example #19
0
 public void Send(NetworkData data)
 {
     throw new NotImplementedException();
 }
Example #20
0
 protected abstract void OnMessage(NetworkData data, IConnection responseChannel);
Example #21
0
 private static void ReceivedDataCallback(NetworkData incomingData, IConnection responseChannel)
 {
     Console.WriteLine("Incomming Data ");
     HexPrint(incomingData.Buffer);
 }
Example #22
0
 /// <summary>
 /// Converts a <see cref="NetworkData"/> structure into a <see cref="ByteString"/>
 /// </summary>
 /// <param name="data">The data we received from the network</param>
 /// <returns>A populated <see cref="ByteString"/> instance</returns>
 public static ByteString FromData(NetworkData data)
 {
     return ByteString.CopyFrom(data.Buffer, 0, data.Length);
 }
Example #23
0
 private static void KafkaClient_Receive(NetworkData incomingData, IConnection responseChannel)
 {
     Console.WriteLine("Data Received ... ");
     HexPrint(incomingData.Buffer);
 }
Example #24
0
 public void Send(NetworkData data)
 {
     Send(data.Buffer, 0, data.Length, data.RemoteHost);
 }
Example #25
0
        protected void InitInbound(IConnection connection, INode remoteSocketAddress, NetworkData msg)
        {
            _associationListenerTask.ContinueWith(r =>
            {
                var listener = r.Result;
                var remoteAddress = HeliosTransport.NodeToAddress(remoteSocketAddress, WrappedTransport.SchemeIdentifier,
                    WrappedTransport.System.Name);

                if (remoteAddress == null) throw new HeliosNodeException("Unknown inbound remote address type {0}", remoteSocketAddress);
                AssociationHandle handle;
                Init(connection, remoteSocketAddress, remoteAddress, msg, out handle);
                listener.Notify(new InboundAssociation(handle));

            }, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.NotOnCanceled | TaskContinuationOptions.NotOnFaulted);
        }
Example #26
0
 protected void InitOutbound(IConnection channel, INode remoteSocketAddress, NetworkData msg)
 {
     AssociationHandle handle;
     Init(channel, remoteSocketAddress, RemoteAddress, msg, out handle);
     StatusPromise.SetResult(handle);
 }
Example #27
0
 protected override void RegisterListener(IConnection channel, IHandleEventListener listener, NetworkData msg, INode remoteAddress)
 {
     ChannelLocalActor.Set(channel, listener);
     BindEvents(channel);
 }
        /// <summary>
        /// Method is called directly by the <see cref="ReactorBase"/> implementation to send data to this <see cref="IConnection"/>.
        /// 
        /// Can also be called by the socket itself if this reactor doesn't use <see cref="ReactorProxyResponseChannel"/>.
        /// </summary>
        /// <param name="data">The data to pass directly to the recipient</param>

        internal virtual void OnReceive(NetworkData data)
        {
            if (NetworkEventLoop.Receive != null)
            {
                NetworkEventLoop.Receive(data, this);
            }
            else
            {
                UnreadMessages.Enqueue(data);
            }
        }
Example #29
0
 protected override void ReceivedData(NetworkData availableData, ReactorResponseChannel responseChannel)
 {
     responseChannel.OnReceive(availableData);
 }
 public void InvokeReceiveIfNotNull(NetworkData data)
 {
     OnReceive(data);
 }
Example #31
0
 public Task SendAsync(NetworkData payload)
 {
     return UnderlyingConnection.SendAsync(payload);
 }