// Update is called once per frame void Update() { while (recvMsgQueue != null && recvMsgQueue.Count > 0) { RecvMessage message = recvMsgQueue.Dequeue(); HandleMsg(message); } }
private void ReceiveCallback(IAsyncResult ar) { byte[] b1 = new byte[268]; Socket s = (Socket)ar.AsyncState; int i = 0; try { i = s.EndReceive(ar); } catch (Exception) { ; } if (i > 0) { Array.Copy(b, b1, b1.Length); SenderInfo sri = (SenderInfo)S_B.BytesToStruct(b1, (new SenderInfo()).GetType()); if (sri.State == 0) { link = false; s.Close(); s = null; return; } byte[] b2 = new byte[sri.StrNum]; Array.Copy(b, b1.Length, b2, 0, sri.StrNum); string send = System.Text.Encoding.UTF8.GetString(b2); DateTime now = DateTime.Now; string name = sri.Name + " " + now; if (sri.State == 2 || sri.State == 3) { ReceiveFile(send, sri.State); } //Console.WriteLine(sri.Name + " " + now); //Console.WriteLine(send); if (RecvMessage != null && sri.State == 1) { foreach (EventHandler <MessageEventArgs> item in RecvMessage.GetInvocationList()) { item.BeginInvoke(this, new MessageEventArgs() { Name = name, Message = send }, null, null); } } try { s.BeginReceive(b, 0, b.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), s); } catch (Exception) { ; } } }
void HandleMsg(RecvMessage msg) { // handle message and refect to corresponding function if (commandMap.ContainsKey(msg.command)) { MessageHandler messageHandler = commandMap[msg.command]; messageHandler(msg.args); } }
public void recvMsg(string data) { // receive message from server if (data[0] != '{' || data[data.Length - 1] != '}') { Debug.Log("invalid message"); return; } RecvMessage msg = RecvMessage.StrToMsg(data); recvMsgQueue.Enqueue(msg); }
private static extern IntPtr CreateChromium(string p_Title, int p_Parent, string p_Content, ref CreateInfo p_CreateInfo, RecvMessage p_RecvMsg);
public static string MsgToStr(RecvMessage msg) { return(JsonUtility.ToJson(msg)); }