private bool ConnectDoDirect(TcpClient client, bool watch, Base.VW.IVI vi) { NetworkStream tcpStream = client.GetStream(); if (!watch) Base.VW.WHelper.SentByteLine(tcpStream, "C2CO,0," + name + "," + avatar + "," + hopeTeam); else Base.VW.WHelper.SentByteLine(tcpStream, "C2QI,0," + name); IDictionary<ushort, IchiPlayer> uidict = new Dictionary<ushort, IchiPlayer>(); while (true) { string line = Base.VW.WHelper.ReadByteLine(tcpStream); if (line == null) return false; else if (line.StartsWith("C2CN,") && !watch) { ushort ut = ushort.Parse(line.Substring("C2CN,".Length)); if (ut == 0) return false; Uid = ut; } else if (line.StartsWith("C2QJ,") && watch) { ushort ut = ushort.Parse(line.Substring("C2QJ,".Length)); if (ut == 0) return false; Uid = ut; } else if (line.StartsWith("C2RM,")) { string[] blocks = line.Split(','); for (int i = 1; i < blocks.Length; i += 3) { ushort ouid = ushort.Parse(blocks[i]); IchiPlayer ip = new IchiPlayer() { Name = blocks[i + 1], Avatar = int.Parse(blocks[i + 2]), Uid = ouid }; uidict.Add(ouid, ip); } vi.Cout(Uid, "Current other gamers: {0}", string.Join(",", uidict.Values.Select(p => p.Name))); } else if (line.StartsWith("C2NW,")) { int idx = line.IndexOf(','); int jdx = line.IndexOf(',', idx + 1); int kdx = line.LastIndexOf(','); ushort ouid = ushort.Parse(Base.Utils.Algo.Substring(line, idx + 1, jdx)); IchiPlayer ip = new IchiPlayer() { Name = Base.Utils.Algo.Substring(line, jdx + 1, kdx), Avatar = int.Parse(line.Substring(kdx + 1)), Uid = ouid }; uidict.Add(ouid, ip); vi.Cout(Uid, "Newcomer: {0}", Base.Utils.Algo.Substring(line, jdx + 1, kdx)); } else if (line.StartsWith("C2SA,")) { stream = tcpStream; StartListenTask(KeepOnListenRecv); if (!watch) StartListenTask(KeepOnListenSend); return true; } else if (line.StartsWith("C")) return false; } }
public void StartHall() { VW.Cyvi cyvi = new VW.Cyvi(AD); VI = cyvi; VI.Init(); VI.SetInGame(false); TcpClient client = null; try { client = new TcpClient(server, port); } catch (SocketException) { cyvi.ReportNoServer(server); return; } NetworkStream tcpStream = client.GetStream(); string trainerjoin = (this.trainer != null && trainer.Length > 0) ? ("," + string.Join(",", trainer)) : ""; int version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Revision; Base.VW.WHelper.SentByteLine(tcpStream, "C0CO," + name + "," + avatar + "," + teamCode + "," + selCode + "," + levelCode + "," + version + trainerjoin); Thread msgThread = new Thread(delegate() { try { string readLine = VI.RequestTalk(uid); while (readLine != null) { lock (tcpStream) Base.VW.WHelper.SentByteLine(tcpStream, "C0TK," + uid + "," + readLine); readLine = VI.RequestTalk(uid); } } catch (System.IO.IOException) { } }); bool done = false; while (!done) { string line = Base.VW.WHelper.ReadByteLine(tcpStream); if (line.StartsWith("C0XV,")) { cyvi.ReportWrongVersion(line.Substring("C0XV,".Length)); return; } if (line.StartsWith("C0CN,")) { uid = ushort.Parse(line.Substring("C1CO,".Length)); cyvi.SetNick(1, name, avatar); } else if (line.StartsWith("C1RM,")) { string[] splits = line.Split(','); room = int.Parse(splits[1]); int pos = 2; for (int i = 2; i < splits.Length; i += 3) { IchiPlayer ip = new IchiPlayer() { Uid = ushort.Parse(splits[i]), Name = splits[i + 1], Avatar = int.Parse(splits[i + 2]) }; roomMates.Add(ip); if (ip.Uid != uid) cyvi.SetNick(pos++, ip.Name, ip.Avatar); } cyvi.SetRoom(room); msgThread.Start(); } else if (line.StartsWith("C1NW,")) { string[] splits = line.Split(','); IchiPlayer ip = new IchiPlayer() { Uid = ushort.Parse(splits[1]), Name = splits[2], Avatar = int.Parse(splits[3]) }; roomMates.Add(ip); if (ip.Uid != uid) cyvi.SetNick(roomMates.Count, ip.Name, ip.Avatar); } else if (line.StartsWith("C1LV,")) { ushort ut = ushort.Parse(line.Substring("C1LV,".Length)); for (int idx = 0; idx < roomMates.Count; ++idx) { IchiPlayer ip = roomMates[idx]; if (ip.Uid == ut) { roomMates.RemoveAt(idx); break; } } int pos = 2; foreach (IchiPlayer ip in roomMates) { if (ip.Uid != uid) cyvi.SetNick(pos++, ip.Name, ip.Avatar); } while (pos <= 6) cyvi.SetNick(pos++, "", 0); } else if (line.StartsWith("C1SA,")) { Base.VW.WHelper.SentByteLine(tcpStream, "C1ST," + uid); VI.SetInGame(true); XV = new XIVisi(uid, name, teamCode, VI, server, room, record, msglog, false, AD); XV.RunAsync(); //client.Close(); done = true; } else if (line.StartsWith("C1TK,")) { int idx = line.IndexOf(',', "C1TK,".Length); string nick = Algo.Substring(line, "C1TK,".Length, idx); string content = Algo.Substring(line, idx + 1, -1); VI.Chat(content, nick); } } if (msgThread != null && msgThread.IsAlive) msgThread.Abort(); }
public void StartHall() { VW.Ayvi ayvi = new VW.Ayvi(); VI = ayvi; VI.Init(); VI.SetInGame(false); TcpClient client = new TcpClient(server, port); NetworkStream tcpStream = client.GetStream(); int version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Revision; string trainerjoin = (this.trainer != null && trainer.Length > 0) ? ("," + string.Join(",", trainer)) : ""; Base.VW.WHelper.SentByteLine(tcpStream, "C0CO," + name + "," + avatar + "," + teamCode + "," + selCode + "," + levelCode + "," + version + trainerjoin); Thread msgThread = new Thread(delegate() { try { //string readLine = Console.ReadLine(); string readLine = VI.RequestTalk(uid); while (readLine != null) { lock (tcpStream) Base.VW.WHelper.SentByteLine(tcpStream, "C0TK," + uid + "," + readLine); readLine = VI.RequestTalk(uid); //readLine = Console.ReadLine(); } } catch (System.IO.IOException) { } }); bool done = false; while (!done) { string line = Base.VW.WHelper.ReadByteLine(tcpStream); if (line.StartsWith("G0XV,")) { string expectVersion = line.Substring("C0XV,".Length); VI.Cout(uid, "Version Missmatch. Expect " + expectVersion + ", please get updated.", uid); } else if (line.StartsWith("C0CN,")) { uid = ushort.Parse(line.Substring("C1CO,".Length)); VI.Cout(uid, "Allocated with uid {0}", uid); } else if (line.StartsWith("C1RM,")) { string[] splits = line.Split(','); room = int.Parse(splits[1]); for (int i = 2; i < splits.Length; i += 3) roomMates.Add(new IchiPlayer() { Uid = ushort.Parse(splits[i]), Name = splits[i + 1], Avatar = int.Parse(splits[i + 2]) }); if (roomMates.Count > 0) VI.Cout(uid, "您进入{0}#房间,其它成员为:{1}", room, string.Join(",", roomMates.Select(p => "[" + p.Uid + "]" + p.Name))); else VI.Cout(uid, "您进入{0}#房间并成为房主。", room); msgThread.Start(); } else if (line.StartsWith("C1NW,")) { string[] splits = line.Split(','); IchiPlayer ip = new IchiPlayer() { Uid = ushort.Parse(splits[1]), Name = splits[2], Avatar = int.Parse(splits[3]) }; roomMates.Add(ip); VI.Cout(uid, "新成员{0}加入房间{1}#。", "[" + ip.Uid + "]" + ip.Name, room); } else if (line.StartsWith("C1LV,")) { ushort ut = ushort.Parse(line.Substring("C1LV,".Length)); foreach (IchiPlayer ip in roomMates) { if (ip.Uid == ut) { roomMates.Remove(ip); VI.Cout(uid, "{0}离开房间。", "[" + ip.Uid + "]" + ip.Name); break; } } } else if (line.StartsWith("C1SA,")) { Base.VW.WHelper.SentByteLine(tcpStream, "C1ST," + uid); Console.WriteLine("Start XIClient"); //VI.Init(); VI.SetInGame(true); XIClient xic = new XIClient(uid, name, teamCode, VI, server, room, record, msglog, false); xic.RunAsync(); //client.Close(); done = true; } else if (line.StartsWith("C1TK,")) // Talk case { int idx = line.IndexOf(',', "C1TK,".Length); string nick = Algo.Substring(line, "C1TK,".Length, idx); string content = Algo.Substring(line, idx + 1, -1); VI.Chat(content, nick); } } if (msgThread != null && msgThread.IsAlive) msgThread.Abort(); }
private bool ConnectDoDirect(TcpClient client, bool watch, Base.VW.IVI vi) { NetworkStream tcpStream = client.GetStream(); VW.Cyvi cvi = vi as VW.Cyvi; if (!watch) Base.VW.WHelper.SentByteLine(tcpStream, "C2CO,0," + name + "," + avatar + "," + hopeTeam); else Base.VW.WHelper.SentByteLine(tcpStream, "C2QI,0," + name); IDictionary<ushort, IchiPlayer> uidict = new Dictionary<ushort, IchiPlayer>(); while (true) { string line = Base.VW.WHelper.ReadByteLine(tcpStream); if (line.StartsWith("C2CN,") && !watch) { ushort ut = ushort.Parse(line.Substring("C2CN,".Length)); if (ut == 0) return false; Uid = ut; cvi.SetNick(1, name, avatar); } else if (line.StartsWith("C2QJ,") && watch) { ushort ut = ushort.Parse(line.Substring("C2QJ,".Length)); if (ut == 0) return false; Uid = ut; } else if (line.StartsWith("C2RM,")) { string[] blocks = line.Split(','); for (int i = 1; i < blocks.Length; i += 3) { ushort ouid = ushort.Parse(blocks[i]); IchiPlayer ip = new IchiPlayer() { Name = blocks[i + 1], Avatar = int.Parse(blocks[i + 2]), Uid = ouid }; uidict.Add(ouid, ip); cvi.SetNick(1 + uidict.Count, ip.Name, ip.Avatar); } } else if (line.StartsWith("C2NW,")) { int idx = line.IndexOf(','); int jdx = line.IndexOf(',', idx + 1); int kdx = line.LastIndexOf(','); ushort ouid = ushort.Parse(Algo.Substring(line, idx + 1, jdx)); IchiPlayer ip = new IchiPlayer() { Name = Algo.Substring(line, jdx + 1, kdx), Avatar = int.Parse(line.Substring(kdx + 1)), Uid = ouid }; uidict.Add(ouid, ip); cvi.SetNick(1 + uidict.Count, ip.Name, ip.Avatar); vi.Cout(Uid, "Newcomer: {0}", Algo.Substring(line, jdx + 1, kdx)); } else if (line.StartsWith("C2SA,")) { stream = tcpStream; recvThread = new Thread(() => ZI.SafeExecute(() => KeepOnListenRecv(), delegate(Exception e) { Log.Logg(e.ToString()); })); recvThread.Start(); if (!watch) { sendThread = new Thread(() => ZI.SafeExecute(() => KeepOnListenSend(), delegate(Exception e) { Log.Logg(e.ToString()); })); sendThread.Start(); } return true; } else if (line.StartsWith("C")) return false; } }