123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using HPSocketCS;
- namespace TcpPullServer
- {
- public enum AppState
- {
- Starting, Started, Stoping, Stoped, Error
- }
- public partial class frmServer : Form
- {
- private AppState appState = AppState.Stoped;
- private delegate void ShowMsg(string msg);
- private ShowMsg AddMsgDelegate;
- HPSocketCS.TcpPullServer server = new HPSocketCS.TcpPullServer();
- HPSocketCS.Extra<ClientInfo> extra = new HPSocketCS.Extra<ClientInfo>();
- // 包头大小
- int pkgHeaderSize = Marshal.SizeOf(new PkgHeader());
- private string title = "Echo-TcpPullServer [ 'C' - clear list box ]";
- public frmServer()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- try
- {
- this.Text = title;
- // 本机测试没必要改地址,有需求请注释或删除
- this.txtIpAddress.ReadOnly = true;
- // 加个委托显示msg,因为on系列都是在工作线程中调用的,ui不允许直接操作
- AddMsgDelegate = new ShowMsg(AddMsg);
- // 设置服务器事件
- server.OnPrepareListen += new TcpServerEvent.OnPrepareListenEventHandler(OnPrepareListen);
- server.OnAccept += new TcpServerEvent.OnAcceptEventHandler(OnAccept);
- server.OnSend += new TcpServerEvent.OnSendEventHandler(OnSend);
- server.OnReceive += new TcpPullServerEvent.OnReceiveEventHandler(OnReceive);
- server.OnClose += new TcpServerEvent.OnCloseEventHandler(OnClose);
- server.OnShutdown += new TcpServerEvent.OnShutdownEventHandler(OnShutdown);
- SetAppState(AppState.Stoped);
- }
- catch (Exception ex)
- {
- SetAppState(AppState.Error);
- AddMsg(ex.Message);
- }
- }
- private void btnStart_Click(object sender, EventArgs e)
- {
- try
- {
- String ip = this.txtIpAddress.Text.Trim();
- ushort port = ushort.Parse(this.txtPort.Text.Trim());
- // 写在这个位置是上面可能会异常
- SetAppState(AppState.Starting);
- server.IpAddress = ip;
- server.Port = port;
- // 启动服务
- if (server.Start())
- {
- this.Text = string.Format("{2} - ({0}:{1})", ip, port, title);
- SetAppState(AppState.Started);
- AddMsg(string.Format("$Server Start OK -> ({0}:{1})", ip, port));
- }
- else
- {
- SetAppState(AppState.Stoped);
- throw new Exception(string.Format("$Server Start Error -> {0}({1})", server.ErrorMessage, server.ErrorCode));
- }
- }
- catch (Exception ex)
- {
- AddMsg(ex.Message);
- }
- }
- private void btnStop_Click(object sender, EventArgs e)
- {
- SetAppState(AppState.Stoping);
- // 停止服务
- AddMsg("$Server Stop");
- if (server.Stop())
- {
- this.Text = title;
- SetAppState(AppState.Stoped);
- }
- else
- {
- AddMsg(string.Format("$Stop Error -> {0}({1})", server.ErrorMessage, server.ErrorCode));
- }
- }
- private void btnDisconn_Click(object sender, EventArgs e)
- {
- try
- {
- IntPtr connId = (IntPtr)Convert.ToUInt32(this.txtDisConn.Text.Trim());
- // 断开指定客户
- if (server.Disconnect(connId, true))
- {
- AddMsg(string.Format("$({0}) Disconnect OK", connId));
- }
- else
- {
- throw new Exception(string.Format("Disconnect({0}) Error", connId));
- }
- }
- catch (Exception ex)
- {
- AddMsg(ex.Message);
- }
- }
- HandleResult OnPrepareListen(IntPtr soListen)
- {
- // 监听事件到达了,一般没什么用吧?
- return HandleResult.Ok;
- }
- HandleResult OnAccept(IntPtr connId, IntPtr pClient)
- {
- // 客户进入了
- // 获取客户端ip和端口
- string ip = string.Empty;
- ushort port = 0;
- if (server.GetRemoteAddress(connId, ref ip, ref port))
- {
- AddMsg(string.Format(" > [{0},OnAccept] -> PASS({1}:{2})", connId, ip.ToString(), port));
- }
- else
- {
- AddMsg(string.Format(" > [{0},OnAccept] -> Server_GetClientAddress() Error", connId));
- }
- // 设置附加数据
- ClientInfo clientInfo = new ClientInfo();
- clientInfo.ConnId = connId;
- clientInfo.IpAddress = ip;
- clientInfo.Port = port;
- clientInfo.PkgInfo = new PkgInfo()
- {
- IsHeader = true,
- Length = pkgHeaderSize,
- };
- if (extra.Set(connId, clientInfo) == false)
- {
- AddMsg(string.Format(" > [{0},OnAccept] -> SetConnectionExtra fail", connId));
- }
- return HandleResult.Ok;
- }
- HandleResult OnSend(IntPtr connId, byte[] bytes)
- {
- // 服务器发数据了
- AddMsg(string.Format(" > [{0},OnSend] -> ({1} bytes)", connId, bytes.Length));
- return HandleResult.Ok;
- }
- HandleResult OnReceive(IntPtr connId, int length)
- {
- // 数据到达了
- // clientInfo 就是accept里传入的附加数据了
- ClientInfo clientInfo = extra.Get(connId);
- if (clientInfo == null)
- {
- return HandleResult.Error;
- }
- PkgInfo pkgInfo = clientInfo.PkgInfo;
- // 需要长度
- int required = pkgInfo.Length;
- // 剩余大小
- int remain = length;
- while (remain >= required)
- {
- IntPtr bufferPtr = IntPtr.Zero;
- try
- {
- remain -= required;
- bufferPtr = Marshal.AllocHGlobal(required); ;
- if (server.Fetch(connId, bufferPtr, required) == FetchResult.Ok)
- {
- if (pkgInfo.IsHeader == true)
- {
- PkgHeader header = (PkgHeader)Marshal.PtrToStructure(bufferPtr, typeof(PkgHeader));
- // 调试信息
- Console.WriteLine("[Server] head -> Id: {0}, BodySize: {1}\r\n", header.Id, header.BodySize);
- required = header.BodySize;
- }
- else
- {
- // 调试信息
- string recvString = Marshal.PtrToStringAnsi(bufferPtr, required);
- Console.WriteLine("[Server] body -> text: {0}\r\n", recvString);
- // intptr转string
- //Marshal.PtrToStringAnsi(bufferPtr, required);
- //intptr转byte[]
- // byte[] bytes = new byte[required];
- // Marshal.Copy(bufferPtr, bytes, 0, required);
- required = pkgHeaderSize;
- }
- AddMsg(string.Format(" > [{0},OnReceive] -> ({1} bytes)", connId, pkgInfo.Length));
- // 回发数据
- byte[] sendBytes = new byte[pkgInfo.Length];
- Marshal.Copy(bufferPtr, sendBytes, 0, sendBytes.Length);
- if (server.Send(connId, sendBytes, sendBytes.Length) == false)
- {
- throw new Exception("server.Send() == false");
- }
- // 在后面赋值,因为前面需要用到pkgInfo.Length
- pkgInfo.IsHeader = !pkgInfo.IsHeader;
- pkgInfo.Length = required;
- if (extra.Set(connId, clientInfo) == false)
- {
- return HandleResult.Error;
- }
- }
- }
- catch
- {
- return HandleResult.Error;
- }
- finally
- {
- if (bufferPtr != IntPtr.Zero)
- {
- Marshal.FreeHGlobal(bufferPtr);
- bufferPtr = IntPtr.Zero;
- }
- }
- }
- return HandleResult.Ok;
- }
- HandleResult OnClose(IntPtr connId, SocketOperation enOperation, int errorCode)
- {
- if(errorCode == 0)
- // 客户离开了
- AddMsg(string.Format(" > [{0},OnClose]", connId));
- else
- // 客户出错了
- AddMsg(string.Format(" > [{0},OnError] -> OP:{1},CODE:{2}", connId, enOperation, errorCode));
- // return HPSocketSdk.HandleResult.Ok;
- // 释放附加数据
- if (extra.Remove(connId) == false)
- {
- AddMsg(string.Format(" > [{0},OnClose] -> SetConnectionExtra({0}, null) fail", connId));
- }
- return HandleResult.Ok;
- }
- HandleResult OnShutdown()
- {
- // 服务关闭了
- AddMsg(" > [OnShutdown]");
- return HandleResult.Ok;
- }
- /// <summary>
- /// 设置程序状态
- /// </summary>
- /// <param name="state"></param>
- void SetAppState(AppState state)
- {
- appState = state;
- this.btnStart.Enabled = (appState == AppState.Stoped);
- this.btnStop.Enabled = (appState == AppState.Started);
- this.txtIpAddress.Enabled = (appState == AppState.Stoped);
- this.txtPort.Enabled = (appState == AppState.Stoped);
- this.txtDisConn.Enabled = (appState == AppState.Started);
- this.btnDisconn.Enabled = (appState == AppState.Started && this.txtDisConn.Text.Length > 0);
- }
- /// <summary>
- /// 往listbox加一条项目
- /// </summary>
- /// <param name="msg"></param>
- void AddMsg(string msg)
- {
- if (this.lbxMsg.InvokeRequired)
- {
- // 很帅的调自己
- this.lbxMsg.Invoke(AddMsgDelegate, msg);
- }
- else
- {
- if (this.lbxMsg.Items.Count > 100)
- {
- this.lbxMsg.Items.RemoveAt(0);
- }
- this.lbxMsg.Items.Add(msg);
- this.lbxMsg.TopIndex = this.lbxMsg.Items.Count - (int)(this.lbxMsg.Height / this.lbxMsg.ItemHeight);
- }
- }
- private void txtDisConn_TextChanged(object sender, EventArgs e)
- {
- // CONNID框被改变事件
- this.btnDisconn.Enabled = (appState == AppState.Started && this.txtDisConn.Text.Length > 0);
- }
- private void lbxMsg_KeyPress(object sender, KeyPressEventArgs e)
- {
- // 清理listbox
- if (e.KeyChar == 'c' || e.KeyChar == 'C')
- {
- this.lbxMsg.Items.Clear();
- }
- }
- private void frmServer_FormClosed(object sender, FormClosedEventArgs e)
- {
- server.Destroy();
- }
- }
- [StructLayout(LayoutKind.Sequential)]
- public class ClientInfo
- {
- public IntPtr ConnId { get; set; }
- public string IpAddress { get; set; }
- public ushort Port { get; set; }
- public PkgInfo PkgInfo { get; set; }
- }
- [StructLayout(LayoutKind.Sequential)]
- public class PkgHeader
- {
- public int Id;
- public int BodySize;
- }
- [StructLayout(LayoutKind.Sequential)]
- public class PkgInfo
- {
- public bool IsHeader;
- public int Length;
- }
- }
|