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); }
public void SendMessage <T>(T m) { string s = typeof(T).FullName; int t; bool b = cdict.TryGetValue(s, out t); if (b) { Stream str = new MemoryStream(); Serializer.Serialize <T>(str, m); string tname = typeof(T).Name; switch (tname[0]) { case 'C': socketManager.SendProtoMessage(SocketId.Main, t, str); break; case 'R': socketManager.SendProtoMessage(SocketId.Chat, t, str); break; default: break; } HDebugger.Log(HDebuggerModule.Proto, "SendMessage all network log : C --> S : " + s); } }
public void SendMessageEmpty <T>() { string s = typeof(T).FullName; int t; bool b = cdict.TryGetValue(s, out t); if (b) { string tname = typeof(T).Name; switch (tname[0]) { case 'C': socketManager.SendEmptyMessage(SocketId.Main, t); HDebugger.Log(HDebuggerModule.Proto, "C --> S : " + s); break; case 'R': socketManager.SendEmptyMessage(SocketId.Chat, t); break; default: break; } } }
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(); } }
public void SendMessage <T>(SocketId socketId, T m) { string s = typeof(T).FullName; int t; bool b = cdict.TryGetValue(s, out t); if (b) { Stream str = new MemoryStream(); Serializer.Serialize <T>(str, m); string tname = typeof(T).Name; socketManager.SendProtoMessage(socketId, t, str); HDebugger.Log(HDebuggerModule.Proto, "SendMessage network log : C --> S : " + s); } }
protected bool Send(byte[] buffer, int offset, int size) { if (socket.Connected) { SocketError sr; int i = socket.Send(buffer, offset, size, SocketFlags.None, out sr); if (i == -1 || sr != SocketError.Success) { HDebugger.Log(HDebuggerModule.Proto, sr.ToString()); Reconnect(); return(false); } return(true); } return(false); }