/// <summary> /// Launch Client /// </summary> public void StartAsClient(string ip, int port) { try { skt.BeginConnect(new IPEndPoint(IPAddress.Parse(ip), port), new AsyncCallback(ServerConnectCB), skt); IYTool.LogMsg("\nClient Start Success!\nConnecting To Server......", LogLevel.Info); } catch (Exception e) { IYTool.LogMsg(e.Message, LogLevel.Error); } }
void ServerConnectCB(IAsyncResult ar) { try { skt.EndConnect(ar); session = new T(); session.StartRcvData(skt, null); } catch (Exception e) { IYTool.LogMsg(e.Message, LogLevel.Error); } }
/// <summary> /// Launch Server /// </summary> public void StartAsServer(string ip, int port) { try { skt.Bind(new IPEndPoint(IPAddress.Parse(ip), port)); skt.Listen(backlog); skt.BeginAccept(new AsyncCallback(ClientConnectCB), skt); IYTool.LogMsg("\nServer Start Success!\nWaiting for Connecting......", LogLevel.Info); } catch (Exception e) { IYTool.LogMsg(e.Message, LogLevel.Error); } }
private void SendCB(IAsyncResult ar) { NetworkStream ns = (NetworkStream)ar.AsyncState; try { ns.EndWrite(ar); ns.Flush(); ns.Close(); } catch (Exception e) { IYTool.LogMsg("SndMsgError:" + e.Message, LogLevel.Error); } }
private void RcvHeadData(IAsyncResult ar) { try { IYPkg pack = (IYPkg)ar.AsyncState; if (skt.Available == 0) { OnDisConnected(); Clear(); return; } int len = skt.EndReceive(ar); if (len > 0) { pack.headIndex += len; if (pack.headIndex < pack.headLen) { skt.BeginReceive( pack.headBuff, pack.headIndex, pack.headLen - pack.headIndex, SocketFlags.None, new AsyncCallback(RcvHeadData), pack); } else { pack.InitBodyBuff(); skt.BeginReceive(pack.bodyBuff, 0, pack.bodyLen, SocketFlags.None, new AsyncCallback(RcvBodyData), pack); } } else { OnDisConnected(); Clear(); } } catch (Exception e) { IYTool.LogMsg("RcvHeadError:" + e.Message, LogLevel.Error); } }
private void RcvBodyData(IAsyncResult ar) { try { IYPkg pack = (IYPkg)ar.AsyncState; int len = skt.EndReceive(ar); if (len > 0) { pack.bodyIndex += len; if (pack.bodyIndex < pack.bodyLen) { skt.BeginReceive(pack.bodyBuff, pack.bodyIndex, pack.bodyLen - pack.bodyIndex, SocketFlags.None, new AsyncCallback(RcvBodyData), pack); } else { T msg = IYTool.DeSerialize <T>(pack.bodyBuff); OnReciveMsg(msg); //loop recive pack.ResetData(); skt.BeginReceive( pack.headBuff, 0, pack.headLen, SocketFlags.None, new AsyncCallback(RcvHeadData), pack); } } else { OnDisConnected(); Clear(); } } catch (Exception e) { IYTool.LogMsg("RcvBodyError:" + e.Message, LogLevel.Error); } }
void ClientConnectCB(IAsyncResult ar) { try { Socket clientSkt = skt.EndAccept(ar); T session = new T(); sessionLst.Add(session); session.StartRcvData(clientSkt, () => { if (sessionLst.Contains(session)) { sessionLst.Remove(session); } }); } catch (Exception e) { IYTool.LogMsg(e.Message, LogLevel.Error); } skt.BeginAccept(new AsyncCallback(ClientConnectCB), skt); }
/// <summary> /// Send binary data /// </summary> public void SendMsg(byte[] data) { NetworkStream ns = null; try { ns = new NetworkStream(skt); if (ns.CanWrite) { ns.BeginWrite( data, 0, data.Length, new AsyncCallback(SendCB), ns); } } catch (Exception e) { IYTool.LogMsg("SndMsgError:" + e.Message, LogLevel.Error); } }
public void StartRcvData(Socket skt, Action closeCB) { try { this.skt = skt; this.closeCB = closeCB; OnConnected(); IYPkg pack = new IYPkg(); skt.BeginReceive( pack.headBuff, 0, pack.headLen, SocketFlags.None, new AsyncCallback(RcvHeadData), pack); } catch (Exception e) { IYTool.LogMsg("StartRcvData:" + e.Message, LogLevel.Error); } }
/// <summary> /// Disconnect network /// </summary> protected virtual void OnDisConnected() { IYTool.LogMsg("Session Disconnected.", LogLevel.Info); }
/// <summary> /// Receive network message /// </summary> protected virtual void OnReciveMsg(T msg) { IYTool.LogMsg("Receive Network Message.", LogLevel.Info); }
/// <summary> /// Connect network /// </summary> protected virtual void OnConnected() { IYTool.LogMsg("New Seesion Connected.", LogLevel.Info); }
/// <summary> /// Send message data /// </summary> public void SendMsg(T msg) { byte[] data = IYTool.PackLenInfo(IYTool.Serialize <T>(msg)); SendMsg(data); }