// n1 is a temporary version of neayers (uid not rearranged), netchers is usable private void ConnectDo(Socket socket, List<ushort> valids, IDictionary<ushort, Neayer> n1) { NetworkStream ns = new NetworkStream(socket); string data = Base.VW.WHelper.ReadByteLine(ns); //string addr = (socket.RemoteEndPoint as IPEndPoint).Address.ToString(); if (data == null) { return; } else if (data.StartsWith("C2CO,")) { string[] blocks = data.Split(','); ushort ut = ushort.Parse(blocks[1]); string uname = blocks[2]; ushort uava = ushort.Parse(blocks[3]); int uHope = int.Parse(blocks[4]); //if (uHope == Base.Rules.RuleCode.HOPE_AKA) // uHope = 1; //else if (uHope == Base.Rules.RuleCode.HOPE_AO) // uHope = 2; //else if (uHope == Base.Rules.RuleCode.HOPE_IP) //{ // if (addrDict.ContainsKey(addr)) // uHope = addrDict[addr]; // else // addrDict.Add(addr, uHope = addrRank++); //} //else // uHope = 0; if (n1 == null) Base.VW.WHelper.SentByteLine(ns, "C2CN,0"); if (valids != null) { if (valids.Contains(ut) && GetAliveNeayersCount() < playerCapacity) { Neayer ny = new Neayer(uname, uava) { Uid = ut, // actual Uid isn't set yet AUid = ut, HopeTeam = uHope, Tunnel = socket }; n1.Add(ut, ny); vi.Cout(0, "[{0}]{1} joined.", ny.AUid, uname); Base.VW.WHelper.SentByteLine(ns, "C2CN," + ny.AUid); } else Base.VW.WHelper.SentByteLine(ns, "C2CN,0"); } else // In Direct mode, exit isn't allowed, AUid isn't useful. { ushort allocUid = (ushort)(curCount++); Neayer ny = new Neayer(uname, uava) { Uid = allocUid, AUid = allocUid, HopeTeam = uHope, Tunnel = socket }; string c2rm = ""; string c2nw = "C2NW," + ny.Uid + "," + ny.Name + "," + ny.Avatar; foreach (Neayer nyr in n1.Values) { c2rm += "," + nyr.Uid + "," + nyr.Name + "," + nyr.Avatar; Base.VW.WHelper.SentByteLine(new NetworkStream(nyr.Tunnel), c2nw); } n1.Add(ny.Uid, ny); vi.Cout(0, "[{0}]{1} joined.", ny.Uid, uname); Base.VW.WHelper.SentByteLine(ns, "C2CN," + ny.Uid); if (c2rm.Length > 0) Base.VW.WHelper.SentByteLine(ns, "C2RM" + c2rm); } } else if (data.StartsWith("C2QI,")) { string[] blocks = data.Split(','); ushort ut = (valids != null) ? ushort.Parse(blocks[1]) : watchCount++; string uname = blocks[2]; while (netchers.ContainsKey(ut)) ++ut; Netcher nc = new Netcher(uname, ut) { Tunnel = socket }; netchers.Add(ut, nc); Base.VW.WHelper.SentByteLine(ns, "C2QJ," + ut); } }
// Catch new room comer, containing watcher and reconnector public ushort CatchNewRoomComer() { if (listener == null) { return 0; } Socket socket; try { socket = listener.AcceptSocket(); } catch (SocketException) { return 0; } NetworkStream ns = new NetworkStream(socket); string data = Base.VW.WHelper.ReadByteLine(ns); if (data == null) { return 0; } else if (data.StartsWith("C2QI,")) // Watcher case { string[] blocks = data.Split(','); ushort ut = ushort.Parse(blocks[1]); if (ut == 0) ut = watchCount++; string uname = blocks[2]; while (netchers.ContainsKey(ut)) ++ut; Netcher nc = new Netcher(uname, ut) { Tunnel = socket }; netchers.Add(ut, nc); Base.VW.WHelper.SentByteLine(ns, "C2QJ," + ut); Base.VW.WHelper.SentByteLine(ns, "C2SA,0"); return ut; } else if (data.StartsWith("C4CR,")) // Reconnect case { // C4CR,u,i,A,cd string[] blocks = data.Split(','); ushort newUt = ushort.Parse(blocks[1]); ushort oldUt = ushort.Parse(blocks[2]); string uname = blocks[3]; string roomPwd = blocks[4]; ushort gameUt = neayers.Values.First(p => p.AUid == oldUt).Uid; Neayer ny = new Neayer(uname, 0) // set avatar = 0, not care { AUid = newUt, Uid = gameUt, HopeTeam = 0, Tunnel = socket, Alive = false }; neayers[ny.Uid] = ny; StartListenTask(() => KeepOnListenRecv(ny)); Base.VW.WHelper.SentByteLine(ns, "C4CS," + ny.Uid); ny.Alive = true; WakeTunnelInWaiting(ny.AUid, ny.Uid); return ny.Uid; } else { Base.VW.WHelper.SentByteLine(ns, "C2CN,0"); return 0; } }