/// <summary> /// /// </summary> /// <param name="isReplay">Set true if this is client is connected to a replayFile</param> /// <param name="replayStream"></param> /// <exception cref="System.ArgumentOutOfRangeException" /> /// <exception cref="System.Net.Sockets.SocketException" /> public void Start(Stream recordStream = null, LoginToken? token = null) { IPEndPoint ep = new IPEndPoint(IPAddress.Parse(IpString), PortNumber); TcpClient client = new TcpClient(); client.Connect(ep); NetworkStream stream = client.GetStream(); receiver = new ItemReceiver(stream, recordStream); sender = new ItemSender(stream); if (token != null) { sender.Send((LoginToken)token); sender.Flush(); } commId = 0; }
void ReconnectionListener() { Trace.TraceInformation("Reconnection listener Started on {0} : {1}", ipAddress.ToString(), IpPort); while (true) { try { var client = listener.AcceptTcpClient(); Trace.TraceInformation("Client connected"); var stream = client.GetStream(); bool spectatorJoining = false; stream.ReadTimeout = 2000; Account theAccount = null; if (game.Configuration != null) { object item; try { item = (new ItemReceiver(stream)).Receive(); } catch (Exception) { item = null; } if (!(item is LoginToken)) { client.Close(); continue; } if (!game.Configuration.AccountIds.Any(id => id.TokenString == ((LoginToken)item).TokenString)) { spectatorJoining = true; } else { int index; for (index = 0; index < game.Configuration.AccountIds.Count; index++) { if (game.Configuration.AccountIds[index].TokenString == ((LoginToken)item).TokenString) { theAccount = game.Configuration.Accounts[index]; } } } } stream.ReadTimeout = Timeout.Infinite; int indexC = game.Settings.Accounts.IndexOf(theAccount); if (spectatorJoining) indexC = numberOfGamers; if (indexC < 0) { client.Close(); continue; } lock (handlers[indexC].queueIn) lock (handlers[indexC].queueOut) lock (handlers[indexC].sender) lock (handlers[indexC].receiver) { handlers[indexC].sender.Flush(); if (spectatorJoining) { ReplaySplitterStream rpstream = handlers[indexC].stream as ReplaySplitterStream; var tempSender = new ItemSender(stream); tempSender.Send(new CommandItem() { command = Command.Detach, type = ItemType.Int, data = 0 }); tempSender.Flush(); rpstream.DumpTo(stream); stream.Flush(); tempSender.Send(new CommandItem() { command = Command.Attach, type = ItemType.Int, data = 0 }); tempSender.Flush(); rpstream.AddStream(stream); } else { var newRCStream = new RecordTakingOutputStream(stream); var tempSender = new ItemSender(newRCStream); tempSender.Send(new CommandItem() { command = Command.Detach, type = ItemType.Int, data = 0 }); tempSender.Flush(); (handlers[indexC].stream as RecordTakingOutputStream).DumpTo(newRCStream); handlers[indexC].disconnected = false; newRCStream.Flush(); tempSender.Send(new CommandItem() { command = Command.Attach, type = ItemType.Int, data = 0 }); tempSender.Flush(); handlers[indexC].stream = newRCStream; handlers[indexC].sender = new ItemSender(handlers[indexC].stream); handlers[indexC].receiver = new ItemReceiver(handlers[indexC].stream); } } } catch (Exception) { return; } } }
private void ClientThread(ServerHandler handler) { ItemSender r = new ItemSender(handler.stream); game.RegisterCurrentThread(); while (true) { handler.semOut.WaitOne(); object o; handler.semAccess.WaitOne(); o = handler.queueOut.Dequeue(); handler.semAccess.Release(1); if (o is FlushObject) { r.Flush(); } else if (!r.Send(o)) { Trace.TraceError("Network failure @ send"); } } }
private void ClientThread(Stream stream, Semaphore semWake, Semaphore semAccess, Queue<object> queueOut) { ItemSender r = new ItemSender(stream); game.RegisterCurrentThread(); while (true) { semWake.WaitOne(); object o; semAccess.WaitOne(); o = queueOut.Dequeue(); semAccess.Release(1); if (o is FlushObject) { r.Flush(); } else if (!r.Send(o)) { Trace.TraceError("Network failure @ send"); } } }