private StatusOrString InitializeSubject(byte[] bytes) { using (var packet = new StringPacket(bytes)) { return((StatusOrString)packet.Consume()); } }
public Status PushInput(string text) { int timestamp = System.Environment.TickCount & System.Int32.MaxValue; var packet = new StringPacket(text, timestamp); return(graph.AddPacketToInputStream(inputStream, packet)); }
public Status StartRun() { outputStreamPoller = calculatorGraph.AddOutputStreamPoller <string>(outputStreamName, true).Value(); outputPacket = new StringPacket(); return(calculatorGraph.StartRun()); }
public static void Handle(Entities.GameClient client, StringPacket strings) { string Name = strings.Strings[0]; Entities.GameClient reqClient; if (Core.Kernel.Clients.TrySelect(Name, out reqClient)) { string toAdd = reqClient.EntityUID + " "; toAdd += reqClient.Level + " "; toAdd += reqClient.Level + " "; //battle power toAdd += "# "; //unknown toAdd += "# "; //unknown toAdd += reqClient.SpouseName + " "; toAdd += 0 + " "; //unknown if (reqClient.Mesh % 10 < 3) { toAdd += "1 "; } else { toAdd += "0 "; } using (var newstrings = new StringPacket(new StringPacker(Name, toAdd))) { newstrings.Action = Enums.StringAction.WhisperWindowInfo; client.Send(newstrings); } } }
public static void Handle(Entities.GameClient client, StringPacket strings) { string Name = strings.Strings[0]; Entities.GameClient reqClient; if (Core.Kernel.Clients.TrySelect(Name, out reqClient)) { string toAdd = reqClient.EntityUID + " "; toAdd += reqClient.Level + " "; toAdd += reqClient.Level + " ";//battle power toAdd += "# ";//unknown toAdd += "# ";//unknown toAdd += reqClient.SpouseName + " "; toAdd += 0 + " ";//unknown if (reqClient.Mesh % 10 < 3) toAdd += "1 "; else toAdd += "0 "; using (var newstrings = new StringPacket(new StringPacker(Name, toAdd))) { newstrings.Action = Enums.StringAction.WhisperWindowInfo; client.Send(newstrings); } } }
public void IsDisposed_ShouldReturnFalse_When_NotDisposedYet() { using (var packet = new StringPacket()) { Assert.False(packet.isDisposed); } }
static void Main(string[] args) { SlavePeer slave = new SlavePeer(); slave.Start(IPAddress.Any, 48361, new SlavePeerListener()); MasterPeer master = new MasterPeer(); master.Start(IPAddress.Any, 48360, new MasterPeerListener()); var peer = new Peer() { EndPoint = new IPEndPoint(IPAddress.Parse("10.20.208.27"), 48361) }; master.Connect(peer); Console.ReadKey(); StringPacket sp = new StringPacket("hehe"); master.Send(peer, sp.Encode()); Console.ReadKey(); // 传输3M的数据 FileBlockBeginPacket begin = new FileBlockBeginPacket(); master.Send(peer, begin.Encode()); byte[] b3k = new byte[1024 * 3]; for (int i = 0; i < 1024; ++i) { FileBlockDataPacket d = new FileBlockDataPacket(b3k); master.Send(peer, d.Encode()); } FileBlockEndPacket end = new FileBlockEndPacket(); master.Send(peer, end.Encode()); Console.ReadKey(); }
public Status StartRun() { outputStreamPoller = graph.AddOutputStreamPoller <string>(outputStream).Value(); outputPacket = new StringPacket(); return(graph.StartRun()); }
public void OnMessage(byte[] msg) { PacketTest t = new PacketTest(msg, 0); switch (t.PacketId) { case 1: StringPacket packet = new StringPacket(); packet.Decode(msg, 0, msg.Length); Console.WriteLine("OnMessage - " + packet.data); break; case 10001: FileBlockBeginPacket begin = new FileBlockBeginPacket(); Console.WriteLine("OnMessage - FileBlockBeginPacket"); break; case 10002: //Console.WriteLine("OnMessage - FileBlockDataPacket"); break; case 10003: Console.WriteLine("OnMessage - FileBlockEndPacket"); break; } }
public Status StartRun(SidePacket sidePacket) { outputStreamPoller = graph.AddOutputStreamPoller <string>(outputStream).ConsumeValue(); outputPacket = new StringPacket(); return(graph.StartRun(sidePacket)); }
public void DebugTypeName_ShouldReturnString_When_ValueIsSet() { var packet = new StringPacket("test"); var regex = new Regex("string"); Assert.True(regex.IsMatch(packet.DebugTypeName())); }
public void isDisposed_ShouldReturnTrue_When_AlreadyDisposed() { var packet = new StringPacket(); packet.Dispose(); Assert.True(packet.isDisposed); }
public void Ctor_ShouldInstantiatePacket_When_CalledWithString() { var packet = new StringPacket("test"); Assert.True(packet.ValidateAsType().ok); Assert.AreEqual(packet.Get(), "test"); Assert.AreEqual(packet.Timestamp(), Timestamp.Unset()); }
public void GetByteArray_ShouldReturnByteArray() { byte[] bytes = new byte[] { (byte)'a', (byte)'b', 0, (byte)'c' }; var packet = new StringPacket(bytes); Assert.AreEqual(packet.GetByteArray(), bytes); Assert.AreEqual(packet.Get(), "ab"); }
public void Ctor_ShouldInstantiatePacket_When_CalledWithNoArguments() { var packet = new StringPacket(); Assert.AreEqual(packet.ValidateAsType().code, Status.StatusCode.Internal); Assert.Throws <MediaPipeException>(() => { packet.Get(); }); Assert.AreEqual(packet.Timestamp(), Timestamp.Unset()); }
public void SendMessage(string message, int objectId) { StringPacket packet = new StringPacket(); packet.payload.message = message; // PacketManager pManager = new PacketManager(); PacketManager.Instance.SendPacket(packet, objectId); }
public void Ctor_ShouldInstantiatePacket_When_CalledWithByteArray() { byte[] bytes = new byte[] { (byte)'t', (byte)'e', (byte)'s', (byte)'t' }; var packet = new StringPacket(bytes); Assert.True(packet.ValidateAsType().ok); Assert.AreEqual(packet.Get(), "test"); Assert.AreEqual(packet.Timestamp(), Timestamp.Unset()); }
public void Ctor_ShouldInstantiatePacket_When_CalledWithValueAndTimestamp() { var timestamp = new Timestamp(1); var packet = new StringPacket("test", timestamp); Assert.True(packet.ValidateAsType().ok); Assert.AreEqual(packet.Get(), "test"); Assert.AreEqual(packet.Timestamp(), timestamp); }
public void GetByteArray_ShouldReturnByteArray() { var bytes = new byte[] { (byte)'a', (byte)'b', 0, (byte)'c' }; using (var packet = new StringPacket(bytes)) { Assert.AreEqual(bytes, packet.GetByteArray()); Assert.AreEqual("ab", packet.Get()); } }
private void _adapter_ReceiveCompleted(object sender, SocketHelper.Events.ReceiveEventArgs args) { if (args.ByteLenght >= 4) { int code = BitConverter.ToInt32(args.ReceivedBytes, 0); switch (code) { case MessageCode.StartServer: { GatewayServer.StartServer(); } break; case MessageCode.StopServer: { GatewayServer.StopServer(); } break; case MessageCode.StartRegister: { StringPacket sp = StringPacket.DeserializeObject(args.ReceivedBytes); GatewayServer.StartRegister(sp.Content); } break; case MessageCode.StopRegister: { StringPacket sp = StringPacket.DeserializeObject(args.ReceivedBytes); GatewayServer.StopRegister(sp.Content); } break; case MessageCode.IsServerStarted: { send(new BoolPacket(code, GatewayServer.IsServerStarted())); } break; case MessageCode.IsSuperiorOnline: { StringPacket sp = StringPacket.DeserializeObject(args.ReceivedBytes); send(new BoolPacket(code, GatewayServer.IsSuperOnline(sp.Content))); } break; case MessageCode.ShareDevice: { StringPacket sp = StringPacket.DeserializeObject(args.ReceivedBytes); GatewayServer.ShareToPlatform(sp.Content); } break; } } }
public void Consume_ShouldReturnStatusOrString_When_PacketIsEmpty() { using (var packet = new StringPacket()) { using (var statusOrString = packet.Consume()) { Assert.False(statusOrString.Ok()); Assert.AreEqual(Status.StatusCode.Internal, statusOrString.status.Code()); } } }
public void Ctor_ShouldInstantiatePacket_When_CalledWithNoArguments() { using (var packet = new StringPacket()) { #pragma warning disable IDE0058 Assert.AreEqual(Status.StatusCode.Internal, packet.ValidateAsType().Code()); Assert.Throws <MediaPipeException>(() => { packet.Get(); }); Assert.AreEqual(Timestamp.Unset(), packet.Timestamp()); #pragma warning restore IDE0058 } }
public void Ctor_ShouldInstantiatePacket_When_CalledWithByteArray() { var bytes = new byte[] { (byte)'t', (byte)'e', (byte)'s', (byte)'t' }; using (var packet = new StringPacket(bytes)) { Assert.True(packet.ValidateAsType().Ok()); Assert.AreEqual("test", packet.Get()); Assert.AreEqual(Timestamp.Unset(), packet.Timestamp()); } }
public static void Handle(Entities.GameClient client, StringPacket strings) { Entities.GameClient reqClient; if (Core.Kernel.Clients.TrySelect(strings.Data, out reqClient)) { using (var newstrings = new StringPacket(new StringPacker(reqClient.SpouseName))) { newstrings.Action = Enums.StringAction.QueryMate; client.Send(newstrings); } } }
public void Consume_ShouldReturnStatusOrString_When_PacketIsNotEmpty() { using (var packet = new StringPacket("abc")) { using (var statusOrString = packet.Consume()) { Assert.True(statusOrString.Ok()); Assert.AreEqual("abc", statusOrString.Value()); } Assert.True(packet.IsEmpty()); } }
public void Ctor_ShouldInstantiatePacket_When_CalledWithStringAndTimestamp() { using (var timestamp = new Timestamp(1)) { using (var packet = new StringPacket("test", timestamp)) { Assert.True(packet.ValidateAsType().Ok()); Assert.AreEqual("test", packet.Get()); Assert.AreEqual(timestamp, packet.Timestamp()); } } }
void OnReceivePacket(int id, UserPacketType type, Stream stream) { Debug.Log("ElChatScreenRecibe---: " + type); if (type == (byte)UserPacketType.Message) { Debug.Log("---Y es un mensaje"); StringPacket packet = new StringPacket(); packet.Deserialize(stream); if (NetworkManager.Instance.isServer) { // MessageManager manager = new MessageManager(); MessageManager.Instance.SendMessage(packet.payload.message, 0); } messages.text += packet.payload.message + System.Environment.NewLine; } }
public void RecieveClientMessage(ServerConnection client, BasePacket recievedPacket) { // The only packets we should look for recieving here are disconnect or exit messages. Or perhaps info like round trip time or ping time switch ((MessageType)recievedPacket.MessageType) { case MessageType.Client_Disconnect: client.StopAll(); break; case MessageType.Client_SendPlayerName: StringPacket namePacket = (StringPacket)recievedPacket; client.SetPlayerName(namePacket.String); Logger.Instance.Info("New player connected : " + client.Name); break; } }
static IntPtr OutputCallback(IntPtr graphPtr, IntPtr packetPtr) { try { var isFound = TryGetGraph(graphPtr, out var graph); if (!isFound) { return(Status.FailedPrecondition("Graph runner is not found").mpPtr); } using (var packet = new StringPacket(packetPtr, false)) { var value = packet.IsEmpty() ? null : packet.Get(); (graph as HelloWorldGraph).OnOutput.Invoke(value); } return(Status.Ok().mpPtr); } catch (Exception e) { return(Status.FailedPrecondition(e.ToString()).mpPtr); } }
public void At_ShouldReturnNewPacketWithTimestamp() { using (var timestamp = new Timestamp(1)) { var str = "str"; var packet = new StringPacket(str).At(timestamp); Assert.AreEqual(str, packet.Get()); Assert.AreEqual(timestamp, packet.Timestamp()); using (var newTimestamp = new Timestamp(2)) { var newPacket = packet.At(newTimestamp); Assert.AreEqual(str, newPacket.Get()); Assert.AreEqual(newTimestamp, newPacket.Timestamp()); } Assert.AreEqual(timestamp, packet.Timestamp()); } }
public void OnMessage(byte[] msg) { Array.Copy(msg, 0, sbuff_, sbufflen_, msg.Length); sbufflen_ += msg.Length; PacketTest t = new PacketTest(sbuff_, 0); if (t.PacketLength > sbufflen_) { // string s = "拆包"; ok } else if (t.PacketLength < sbufflen_) { string s = "粘包"; } else if (t.PacketLength == sbufflen_) { switch (t.PacketId) { case 1: StringPacket packet = new StringPacket(); packet.Decode(msg, 0, msg.Length); Console.WriteLine("OnMessage - " + packet.data); break; case 10004: FileBlockTask task = new FileBlockTask(); task.Decode(msg, 0, msg.Length); task_ = task.task_; Console.WriteLine("OnMessage - FileBlockTask"); autoResetEvent_.Set(); new Thread(new ThreadStart(Download)).Start(); break; case 10001: autoResetEvent_.Set(); break; } sbufflen_ = 0; } }
/// <summary> /// Upgrades the connection to the particular protocol. Handles the handshake. /// </summary> /// <param name="context"><see cref="ProcessingContext"/> for the current connection.</param> /// <param name="httpContext"><see cref="HttpContext"/> for the current connection.</param> /// <returns>The handlers that have been inserted in the pipeline.</returns> public WebSocketPipeline Upgrade(ProcessingContext context, HttpContext httpContext) { // Compute the handshake response key var inputKey = httpContext.Request.Headers["Sec-WebSocket-Key"]; var responseKey = System.Convert.ToBase64String((inputKey.Trim() + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11") .GetSHA1Bytes()); var request = httpContext.Request; var builder = new StringBuilder(); builder.Append("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: " + responseKey + "\r\n"); if (request.Headers["Sec-WebSocket-Protocol"] != null) { builder.AppendFormat("Sec-WebSocket-Protocol: {0}\r\n", request.Headers["Sec-WebSocket-Protocol"]); } builder.Append("\r\n"); var response = new StringPacket(Encoding.ASCII, builder.ToString()); // Prepare the response packet /*var response = new StringPacket(Encoding.ASCII, * "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: " + responseKey + "\r\n\r\n" * );*/ // Get the channel var channel = httpContext.Connection; // Send the handshake response channel.Send(response); // Set the encoder & the decoder for this websocket handler channel.Encoding.PipelineAddLast(Encode.WebSocketHybi13); channel.Decoding.PipelineAddFirst(Decode.WebSocketHybi13); // Trace a websocket event NetTrace.WriteLine("Upgraded to Hybi13 ", channel, NetTraceCategory.WebSocket); return(new WebSocketPipeline(Encode.WebSocketHybi13, Decode.WebSocketHybi13)); }