public ServerGamer() { semaPakArrival = new Semaphore(0, Int32.MaxValue); semaPakToSend = new Semaphore(0, Int32.MaxValue); receiverLock = new object(); senderLock = new object(); DataStream = new RecordTakingOutputStream(); sendQueue = new BlockingCollection <GameDataPacket>(new ConcurrentQueue <GameDataPacket>()); }
public void AddStream(Stream newStream) { if (DataStream == null) { DataStream = new RecordTakingOutputStream(); DataStream.AddStream(newStream, true); return; } if (sendThread != null) { while (sendQueue.Count > 0) { Thread.Sleep(10); } } lock (receiverLock) { lock (senderLock) { try { var uiDetach = new UIStatusHint() { IsDetached = true }; var uiAttach = new UIStatusHint() { IsDetached = false }; Serializer.SerializeWithLengthPrefix <GameDataPacket>(newStream, uiDetach, PrefixStyle.Base128); DataStream.AddStream(newStream, true); Serializer.SerializeWithLengthPrefix <GameDataPacket>(newStream, uiAttach, PrefixStyle.Base128); newStream.Flush(); } catch (Exception) { } } } }