public override int Send(RTRequest request) { MemoryStream ms = PooledObjects.MemoryStreamPool.Pop(); try { ms.Position = 0; Packet p = request.ToPacket(session, true); try { Packet.SerializeLengthDelimited(ms, p); } finally { PooledObjects.PacketPool.Push(p); } try { #if __WINDOWS__ Task t = Task.Run(async() => { byte[] newArray = ms.ToArray(); Array.Resize <byte>(ref newArray, (int)ms.Position); DataWriter writer = new DataWriter(client.OutputStream); writer.WriteBytes(newArray); await writer.StoreAsync(); await writer.FlushAsync(); writer.DetachStream(); writer.Dispose(); }); t.Wait(); //client.OutputStream.AsStreamForWrite().WriteAsync(newArray, 0, (int)ms.Position); #else client.Send(ms.GetBuffer(), (int)ms.Position); #endif } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e); session.Log("fastConnection", GameSparksRT.LogLevel.INFO, "Exception sending UDP {0}", e.Message); } return((int)ms.Position); } finally { PooledObjects.MemoryStreamPool.Push(ms); } }
public override int Send(RTRequest request) { if (socket != null && socket.State == GameSparksWebSocketState.Open) { Packet p = request.ToPacket(session, false); MemoryStream ms = PooledObjects.MemoryStreamPool.Pop(); ms.Position = 0; try { Packet.SerializeLengthDelimited(ms, p); int ret = (int)ms.Position; socket.SendBinary(ms.GetBuffer(), 0, ret); return(ret); } catch (Exception e) { if (session != null && !stopped) { session.ConnectState = GameSparksRT.ConnectState.Disconnected; session.Log("ReliableWSConnection", GameSparksRT.LogLevel.DEBUG, e.Message); try { session.OnReady(false); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } } return(0); } finally { PooledObjects.PacketPool.Push(p); PooledObjects.MemoryStreamPool.Push(ms); } } return(-1); }
/*private string GetCertificateInformation( * Certificate serverCert, * IReadOnlyList<Certificate> intermediateCertificates) * { * StringBuilder sb = new StringBuilder(); * * sb.AppendLine("\tFriendly Name: " + serverCert.FriendlyName); * sb.AppendLine("\tSubject: " + serverCert.Subject); * sb.AppendLine("\tIssuer: " + serverCert.Issuer); * sb.AppendLine("\tValidity: " + serverCert.ValidFrom + " - " + serverCert.ValidTo); * * // Enumerate the entire certificate chain. * if (intermediateCertificates.Count > 0) * { * sb.AppendLine("\tCertificate chain: "); * foreach (var cert in intermediateCertificates) * { * sb.AppendLine("\t\tIntermediate Certificate Subject: " + cert.Subject); * } * } * else * { * sb.AppendLine("\tNo certificates within the intermediate chain."); * } * * return sb.ToString(); * }*/ #endif public override int Send(RTRequest request) { #if __WINDOWS__ if (client != null && connected) #else if (client.Connected) #endif { Packet p = request.ToPacket(session, false); try { #if __WINDOWS__ MemoryStream stream = new MemoryStream(); int result = Packet.SerializeLengthDelimited(stream, p); Task t = Task.Run(async() => { DataWriter writer = new DataWriter(client.OutputStream); writer.WriteBytes(stream.ToArray()); await writer.StoreAsync(); await writer.FlushAsync(); writer.DetachStream(); writer.Dispose(); }); t.Wait(); return(result); #else int ret = Packet.SerializeLengthDelimited(clientStream, p); clientStream.Flush(); return(ret); #endif } catch (Exception e) { if (session != null && !stopped) { session.ConnectState = GameSparksRT.ConnectState.Disconnected; session.Log("ReliableConnection", GameSparksRT.LogLevel.DEBUG, e.Message); try { session.OnReady(false); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } } return(0); } finally { PooledObjects.PacketPool.Push(p); } } return(-1); }