www.pudn.com > 200589952618.rar > MYTYPE.PAS


unit mytype; 
 
interface 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls, ComCtrls, IdBaseComponent, IdComponent, IdTCPServer, 
  DB, ADODB; 
 
type 
  Drawbox = array[0..9, 0..19] of Byte; 
 
  {定义命令型变量用于与客户端传送命令,command是发送的命令} 
  Rcommandtext = record 
    command: integer; 
    num: integer; 
  end; 
 
  {用户信息类型} 
  Ruserdata = record 
    username: string[20]; 
    passwd: string[20]; 
    email: string[30]; 
    face: integer; 
    IP: string[15]; 
    post: integer; 
    lastsendtime: Tdatetime; 
    whatdoing: integer; 
    Rbg: Drawbox; 
    Rfg: Drawbox; 
    AThread: TIdPeerThread 
  end; 
 
{聊天信息数据类型} 
  Rtextdata = record 
    fromwho: string[20]; 
    towho: string[20]; 
    text: string[100]; 
    size: integer; 
    color: Tcolor; 
  end; 
 
 
  {管理服务端类} 
  Tctrl = class 
  public 
    cs: TRTLCriticalSection; 
    onlineview: TListView; 
    online: array of Ruserdata; 
    IdTCPServer: TIdTCPServer; 
    adoquery: TADOQuery; 
 
    function reg(user_data: Ruserdata): string; //注册功能 
    function login(var user_data: Ruserdata): string; //登陆功能 
    procedure addonline(user_data: Ruserdata); //增加在线人数功能 
    procedure deleonline(user_data: Ruserdata); overload; //离线功能 
    procedure deleonline(AThread: TIdPeerThread); overload; 
    procedure sendallonline(AThread: TIdPeerThread); //发送所以在线的用户 
    procedure sendtext(ctext: Rcommandtext; textdata: Rtextdata); 
    procedure modiuserstate(userdata: Ruserdata); 
    procedure modiwhatdoing(ctext: Rcommandtext; userdata: Ruserdata); 
  end; 
 
 
implementation 
 
uses myconst; 
 
{ Tctrl } 
 
function Tctrl.login(var user_data: Ruserdata): string; 
begin 
  EnterCriticalSection(cs); 
  adoquery.Close; 
  adoquery.SQL.text := 'select * from userdata where username="' + user_data.username + '" and passwd="' + user_data.passwd + '"'; 
  adoquery.Open; 
  if not adoquery.Eof then 
  begin 
    user_data.face := adoquery.fieldbyname('face').AsInteger; 
    user_data.email := adoquery.fieldbyname('email').AsString; 
    user_data.passwd := ''; 
    user_data.whatdoing := 0; 
    user_data.lastsendtime := now; 
    result := inttostr(user_data.face); 
  end 
  else result := '用户名或密码不符。'; 
  adoquery.Close; 
  Leavecriticalsection(cs); 
end; 
 
function Tctrl.reg(user_data: Ruserdata): string; 
begin 
  EnterCriticalSection(cs); 
  adoquery.Close; 
  adoquery.SQL.text := 'select * from userdata where username="' + user_data.username + '"'; 
  adoquery.Open; 
  if adoquery.Eof then 
  begin 
    adoquery.Append; 
    adoquery.fieldbyname('username').AsString := user_data.username; 
    adoquery.fieldbyname('passwd').AsString := user_data.passwd; 
    adoquery.fieldbyname('email').AsString := user_data.email; 
    adoquery.fieldbyname('face').AsInteger := user_data.face; 
    adoquery.post; 
    adoquery.Close; 
    result := '注册成功,请重新登陆'; 
  end 
  else result := '用户名已存在。'; 
  Leavecriticalsection(cs); 
end; 
 
procedure Tctrl.addonline(user_data: Ruserdata); 
var 
  i: integer; 
  ctext: Rcommandtext; 
begin 
  EnterCriticalSection(cs); 
  ctext.command := Caddonline; //先向已经登陆的人发送增加的在线用户的命令和信息 
  for i := 0 to high(online) do 
  begin 
    online[i].AThread.Connection.WriteBuffer(ctext, sizeof(ctext)); 
    online[i].AThread.Connection.WriteBuffer(user_data, sizeof(user_data)); 
  end; 
  setlength(online, high(online) + 2); //然后增加一个节点。 
  online[high(online)] := user_data; 
  with onlineview.Items.add do 
  begin 
    caption := user_data.username; 
    subitems.add(user_data.IP); 
    subitems.add(datetimetostr(user_data.lastsendtime)); 
    subitems.add(inttostr(user_data.post)); 
  end; 
  Leavecriticalsection(cs); 
end; 
 
procedure Tctrl.deleonline(user_data: Ruserdata); 
var 
  i: integer; 
  ctext: Rcommandtext; 
begin 
  ctext.command := Cdeleonline; 
  EnterCriticalSection(cs); 
  for i := 0 to high(online) do 
  begin 
    if online[i].username = user_data.username then //删除联机节点 
    begin 
      online[i] := online[high(online)]; 
      setlength(online, high(online)); 
    end 
    else 
    begin 
      online[i].AThread.Connection.WriteBuffer(ctext, sizeof(ctext)); 
      online[i].AThread.Connection.WriteBuffer(user_data, sizeof(user_data)); 
    end; 
    if onlineview.Items.Item[i].caption = user_data.username then //删除listview中显示的节点 
      onlineview.Items.Item[i].Delete; 
  end; 
  Leavecriticalsection(cs); 
end; 
 
procedure Tctrl.deleonline(AThread: TIdPeerThread); 
var 
  userdata: Ruserdata; 
  ctext: Rcommandtext; 
  i: integer; 
begin 
  ctext.command := Cdeleonline; 
  EnterCriticalSection(cs); 
  for i := 0 to high(online) do 
  begin 
    if online[i].AThread = AThread then 
    begin 
      userdata := online[i]; 
      online[i] := online[high(online)]; 
      setlength(online, high(online)); 
      break; 
    end; 
  end; 
  for i := 0 to high(online) do 
  begin 
    online[i].AThread.Connection.WriteBuffer(ctext, sizeof(ctext)); 
    online[i].AThread.Connection.WriteBuffer(userdata, sizeof(userdata)); 
    if onlineview.Items.Item[i].caption = userdata.username then 
      onlineview.Items.Item[i].Delete; 
  end; 
  if (i = onlineview.Items.Count - 1) and (onlineview.Items.Item[i].caption = userdata.username) then 
    onlineview.Items.Item[i].Delete; 
  Leavecriticalsection(cs); 
end; 
 
procedure Tctrl.sendallonline(AThread: TIdPeerThread); 
var 
  i: integer; 
  ctext: Rcommandtext; 
begin 
  ctext.command := Caddonline; 
  EnterCriticalSection(cs); 
  for i := 0 to high(online) do 
  begin 
    AThread.Connection.WriteBuffer(ctext, sizeof(ctext)); 
    AThread.Connection.WriteBuffer(online[i], sizeof(online[i])); 
  end; 
  Leavecriticalsection(cs); 
end; 
 
procedure Tctrl.sendtext(ctext: Rcommandtext; textdata: Rtextdata); 
var 
  i: integer; 
begin 
  EnterCriticalSection(cs); 
  for i := 0 to high(online) do 
  begin 
    online[i].AThread.Connection.WriteBuffer(ctext, sizeof(ctext)); 
    online[i].AThread.Connection.WriteBuffer(textdata, sizeof(textdata)); 
  end; 
  Leavecriticalsection(cs); 
end; 
 
procedure Tctrl.modiuserstate(userdata: Ruserdata); 
var 
  i: integer; 
  ctext: Rcommandtext; 
begin 
  ctext.command := Cmodiuserstate; 
  for i := 0 to high(online) do 
  begin 
    online[i].AThread.Connection.WriteBuffer(ctext, sizeof(ctext)); 
    online[i].AThread.Connection.WriteBuffer(userdata, sizeof(userdata)); 
  end; 
end; 
 
procedure Tctrl.modiwhatdoing(ctext: Rcommandtext; userdata: Ruserdata); 
var 
  i: integer; 
begin 
  EnterCriticalSection(cs); 
  for i := 0 to high(online) do 
  begin 
    if online[i].username = userdata.username then 
    begin 
      online[i].whatdoing := ctext.num; 
      modiuserstate(online[i]); 
      break; 
    end; 
  end; 
  Leavecriticalsection(cs); 
end; 
 
end.