示例#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
        internal void Handle(ReadData rdata)
        {
            if (rdata.proto == 0)
            {
                return;
            }
            if (rdata.retcode != 0)
            {
                Debug.Log("Process protocol 0x" + Convert.ToString(rdata.proto, 16) + " failed. retcode=" + rdata.retcode);
                SysmsgManager.Execute(rdata.retcode);
                return;
            }

            IProtoHandler iph;

            Debug.Log("network log receive : S->C msgid = 0x" + Convert.ToString(rdata.proto, 16) + ", retcode=" + rdata.retcode);
            bool b;

            b = sdict.TryGetValue(rdata.proto, out iph);
            if (b)
            {
                iph.Handle(rdata.stream);
            }
            rdata.Release();
            //            throw new NotImplementedException();
        }
示例#3
0
        private void EchoProto <T>(int i, T msg)
        {
            ReadData rd = readpool.Get();

            rd.proto = i;
            rd.stream.Seek(0, System.IO.SeekOrigin.Begin);
            Serializer.Serialize <T>(rd.stream, msg);
            readdata.Add(rd);
        }
示例#4
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();
            }
        }
示例#5
0
        private void EchoProto_DEF <T>(int i) where T : new()
        {
            T        msg = new T();
            ReadData rd  = readpool.Get();

            rd.proto = i;
            rd.stream.Seek(0, System.IO.SeekOrigin.Begin);
            Serializer.Serialize <T>(rd.stream, msg);
            readdata.Add(rd);
        }
        private void ReadEnd(IAsyncResult ar)
        {
            ReadData rd     = ar.AsyncState as ReadData;
            Socket   socket = rd.socket;

            try
            {
                int n = rd.socket.EndReceive(ar);
                rd.read += n;
                if (rd.read == rd.total)
                {
                    if (rd.state == ReadState.Header)
                    {
                        int l  = BitConverter.ToInt16(rd.buffer, 0);
                        int id = BitConverter.ToInt16(rd.buffer, 2);
                        rd.proto = id;
                        rd.read  = 0;
                        rd.total = l;
                        rd.state = ReadState.Content;
                    }
                    else
                    {
                        rd.stream.Seek(0, SeekOrigin.Begin);
                        rd.stream.Write(rd.buffer, 0, rd.total);
                        inqueue.Enqueue(rd);
                        rd        = readpool.Get();
                        rd.socket = socket;
                    }
                }
                socket.BeginReceive(rd.buffer, rd.read, rd.total - rd.read, SocketFlags.None, ReadEnd, rd);
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
                status = SocketStatus.Errored;
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
                socket = null;
            }
        }
示例#7
0
        internal void LuaHandle(ReadData rdata)
        {
            if (rdata.proto == 0)
            {
                return;
            }
            if (rdata.retcode != 0)
            {
                Debug.Log("Process protocol 0x" + Convert.ToString(rdata.proto, 16) + " failed. retcode=" + rdata.retcode);
//                SysmsgManager.Execute(rdata.retcode);
                return;
            }

            LuaFunction handler;
            bool        b;

            b = sLuaHandlers.TryGetValue(rdata.proto, out handler);
            if (b)
            {
                Debug.Log("network log receive: S->C msgid = 0x" + Convert.ToString(rdata.proto, 16) + " by lua process");
                Util.PushBufferToLua(handler, rdata.stream.ToArray());
//				rdata.Release();
            }
        }
示例#8
0
        public void Update()
        {
            if (status == SocketStatus.Ok)
            {
                frametemp.Clear();
                if (!socket.Connected)
                {
                    sDisconnect.Dispatch();
                    status = SocketStatus.Errored;
                }
                else
                {
                    try
                    {
                        int avail = socket.Available;
                        while (avail > 0)
                        {
                            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 l  = BitConverter.ToUInt16(rdata.buffer, 0);
                                    int id = BitConverter.ToUInt16(rdata.buffer, 2);
                                    rdata.proto = id;
                                    rdata.read  = 0;
                                    rdata.total = l;
                                    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.LogException(ex);
                        Reconnect(endp);
                    }
                }
                foreach (var d in frametemp)
                {
                    handler(d);
                }
            }
        }