示例#1
0
        public IEnumerator ConnectFrame(Socket sock, IPEndPoint ep)
        {
            bool conn = false;

            while (!conn)
            {
                try
                {
                    Debug.Log(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff") + " ConnectFrame sock.Connect(ep) pre ReconnSpan=" + ReconnSpan);
                    sock.Connect(ep);
                    conn = true;
                    Debug.Log(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff") + " ConnectFrame conn=" + conn);
                    break;
                }
                catch (Exception ex)
                {
                    Debug.Log(ex);
                    HDebugger.LogException(HDebuggerModule.Proto, ex);
                    conn = false;
                }

                yield return(new WaitForSeconds(ReconnSpan));
            }
            status       = SocketStatus.Ok;
            rdata        = readpool.Get();
            rdata.socket = sock;
            Debug.Log("CommonSocketServer connect status=" + status);
            sConnect.Dispatch(sid);
        }
示例#2
0
        private void ReadData()
        {
            try
            {
                int avail = socket.Available;
                while (avail > 0)
                {
                    lastSendTime = -1;

                    // Debug.Log("Receiveable" + avail + " bytes from server");
                    int byt = socket.Receive(rdata.buffer, rdata.read, rdata.total - rdata.read, SocketFlags.None);
                    avail      -= byt;
                    rdata.read += byt;
                    if (rdata.read == rdata.total)
                    {
                        if (rdata.state == ReadState.Header)
                        {
                            int len       = BitConverter.ToInt32(rdata.buffer, 0);
                            int checkcode = BitConverter.ToInt32(rdata.buffer, 4);
                            int msgid     = BitConverter.ToInt16(rdata.buffer, 8);
                            int retcode   = BitConverter.ToInt16(rdata.buffer, 10);
                            //int roleId = BitConverter.ToInt32(rdata.buffer, 12);
                            //Debug.Log("len=" + len + ", checkcode=" + checkcode + ", msgid=" + msgid + ", retcode=" + retcode );

                            rdata.proto   = msgid;
                            rdata.retcode = retcode;
                            rdata.read    = 0;
                            rdata.total   = len - 12; // minus 16 bytes head length
                            rdata.state   = ReadState.Content;

                            if (rdata.total == 0)
                            {
                                rdata.stream.SetLength(0);
                                frametemp.Add(rdata);
                                rdata        = readpool.Get();
                                rdata.socket = socket;
                            }
                            //print("id  " + id.ToString() + "len  " + l.ToString());
                        }
                        else
                        {
                            rdata.stream.Seek(0, SeekOrigin.Begin);
                            rdata.stream.Write(rdata.buffer, 0, rdata.total);
                            frametemp.Add(rdata);
                            rdata        = readpool.Get();
                            rdata.socket = socket;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //Debug.Log("================================exp====");
                HDebugger.LogException(HDebuggerModule.Proto, ex);
                Reconnect();
            }
        }