示例#1
0
        void ReceiveFromServer()
        {
            try
            {
                byte[] buf4 = new byte[4];
                while (true)
                {
                    clientSocket.Receive(buf4, 4, SocketFlags.None);
                    int    protocolLength = (buf4[0] - '0') * 1000 + (buf4[1] - '0') * 100 + (buf4[2] - '0') * 10 + (buf4[3] - '0') * 1;
                    byte[] buf            = new byte[protocolLength];
                    int    byteNumber     = clientSocket.Receive(buf, protocolLength, SocketFlags.None);

                    string   s  = Encoding.UTF8.GetString(buf, 0, byteNumber);
                    string[] ss = s.Split(' ');

                    if (legalProtocolMap.ContainsKey(ss[0]))
                    {
                        ClientProtocol protocol = legalProtocolMap[ss[0]].GetInstance();
                        protocol.LoadContentFromWString(s);
                        if (ProcessProtocolEvent != null)
                        {
                            ProcessProtocolEvent(protocol);
                        }
                    }
                    else
                    {
                        Debug.LogError("非法协议!" + ss[0]);
                    }
                }
            }
            catch (Exception)
            {
                if (DisconnectEvent != null)
                {
                    DisconnectEvent.Invoke(clientSocket);
                }
            }
            finally
            {
                clientSocket.Close();
            }
        }
示例#2
0
 public void AddLegalProtocol(ClientProtocol protocol)
 {
     legalProtocolMap.Add(protocol.GetName(), protocol);
 }