static void ReceiveMessage(object o) { byte[] result = new byte[1024]; MsgStruct d = (MsgStruct)o; Socket myClientSocket = d.client; while (true) { try { if (d.server.disposed) { return; } //通过clientSocket接收数据 int receiveNumber = myClientSocket.Receive(result); if (receiveNumber <= 0) { //myClientSocket.Shutdown(SocketShutdown.Both); myClientSocket.Close(); return; } string content = BytesToString(result, receiveNumber); Debug.Log(content); HandCommand(content); } catch (Exception ex) { Debug.Log(ex.Message); myClientSocket.Shutdown(SocketShutdown.Both); myClientSocket.Close(); break; } } }
private void QueueAction(MsgType type, string message) { MsgStruct ms = new MsgStruct(); ms.type = type; ms.Message = message; incomingActionQueue.Enqueue(ms); }
public void SendMessage(MsgStruct msg) { var outMessage = netClient.CreateMessage(); outMessage.Write((byte)msg.type); outMessage.Write(msg.Message); netClient.SendMessage(outMessage, NetDeliveryMethod.ReliableOrdered); }
public void SendActionToServer() { if (outActionQueue.Count > 0) { prevAction = outActionQueue.Dequeue(); } else { prevAction = new MsgStruct { type = MsgType.NoAction, Message = noMessage }; } SendMessage(prevAction); }
private void HandleErrorMsg() { int count = 0; string[] subItemsString = null; List <string[]> tempList = new List <string[]>(); MsgStruct msg = new MsgStruct(); msg.imageIndex = "1"; foreach (string[] temp in this.errorMsg) { msg.index = (count++).ToString(); msg.context = temp[1]; if (temp.Length == 3) { msg.lineNum = temp[0]; msg.pageName = null; if (temp[0] != null) { AddWrongSentence(temp[1], Convert.ToInt32(temp[0]), true, null); tempList.Add(subItemsString); } else { AddWrongSentence(temp[1], true, null); } tempList.Add(subItemsString); } else { if (temp[0] != null) { AddWrongSentence(temp[1], Convert.ToInt32(temp[0]), true, temp[3].ToUpper()); } else if (temp[3] != null) { AddWrongSentence(temp[1], true, temp[3].ToUpper()); } else { AddWrongSentence(temp[1], true, ""); } } } }
static void ListenClientConnect(object o) { FSocketServer p = (FSocketServer)o; while (!p.disposed) { Socket clientSocket = p.serverSocket.Accept(); p.SendString(clientSocket, "hello client"); //获取客户端的IP和端口 string ip = clientSocket.RemoteEndPoint.ToString(); Debug.Log("New Connect " + ip); MsgStruct sdata = new MsgStruct(); sdata.client = clientSocket; sdata.server = p; Thread receiveThread = new Thread(ReceiveMessage); receiveThread.Start(sdata); } }
private void ExecuteAction(MsgStruct msg) { int teamColor; //Parse actions switch (msg.type) { case MsgType.PlayCard: Debug.WriteLine("Play Card: " + msg.Message); string[] args = msg.Message.Split(';'); if (args.Length > 1) { string cardName = args[0]; int.TryParse(args[1], out teamColor); GodClass.PlayCard(cardName, new int[] { teamColor }); } break; default: break; } }
private void HandleErrorMsg() { int count = 0; string[] subItemsString = null; List<string[]> tempList = new List<string[]>(); MsgStruct msg = new MsgStruct(); msg.imageIndex = "1"; foreach (string[] temp in this.errorMsg) { msg.index = (count++).ToString(); msg.context = temp[1]; if (temp.Length == 3) { msg.lineNum = temp[0]; msg.pageName = null; if (temp[0] != null) { AddWrongSentence(temp[1], Convert.ToInt32(temp[0]), true, null); tempList.Add(subItemsString); } else { AddWrongSentence(temp[1], true, null); } tempList.Add(subItemsString); } else { if (temp[0] != null) { AddWrongSentence(temp[1], Convert.ToInt32(temp[0]), true, temp[3].ToUpper()); } else if (temp[3] != null) { AddWrongSentence(temp[1], true, temp[3].ToUpper()); } else { AddWrongSentence(temp[1], true, ""); } } } }